Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.14.1
rev: v0.16.0
hooks:
# Run the linter.
- id: ruff
args: ["--fix", "--show-fixes"]
# Run the formatter.
- id: ruff-format
- id: ruff-format
name: ruff-format (markdown & towncrier)
types_or: [python, pyi, jupyter, markdown, text]
files: \.(md|added|changed|deprecated|removed|fixed)$
args: ["--preview"]
- repo: https://github.com/astral-sh/uv-pre-commit
# uv version.
rev: 1ce7e7fa8aa6eda60e54755509a9380b0f1c5d08 # frozen: v0.9.30
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ requests==2.33.1
ruamel.yaml==0.17.21
flaky==3.7.0
pre-commit==3.7.0
ruff==0.14.1
ruff==0.16.0
2 changes: 1 addition & 1 deletion gen-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
astor==0.8.1
jinja2==3.1.6
markupsafe==2.0.1
ruff==0.14.1
ruff==0.16.0
requests
tomli
tomli_w
Expand Down
8 changes: 6 additions & 2 deletions instrumentation-genai/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ hook without touching the environment.
from opentelemetry.util.genai.completion_hook import load_completion_hook
from opentelemetry.util.genai.handler import TelemetryHandler


def _instrument(self, **kwargs):
tracer_provider = kwargs.get("tracer_provider")
meter_provider = kwargs.get("meter_provider")
Expand All @@ -63,7 +64,8 @@ def _instrument(self, **kwargs):
tracer_provider=tracer_provider,
meter_provider=meter_provider,
logger_provider=logger_provider,
completion_hook=kwargs.get("completion_hook") or load_completion_hook(),
completion_hook=kwargs.get("completion_hook")
or load_completion_hook(),
)
# pass handler to each patch/wrapper function
```
Expand All @@ -73,7 +75,9 @@ def _instrument(self, **kwargs):
Use `start_*()` and control span lifetime manually:

```python
invocation = handler.start_inference(provider, request_model, server_address=..., server_port=...)
invocation = handler.start_inference(
provider, request_model, server_address=..., server_port=...
)
invocation.temperature = ...
try:
response = client.call(...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ async def main() -> None:
channel = await connection.channel()
queue = await channel.declare_queue("hello")
await channel.default_exchange.publish(
Message(b"Hello World!"),
routing_key=queue.name)
Message(b"Hello World!"), routing_key=queue.name
)
if __name__ == "__main__":
asyncio.run(main())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,28 @@ async def get(url):
.. code-block:: python

def request_hook(span: Span, params: aiohttp.TraceRequestStartParams):
if span and span.is_recording():
span.set_attribute("custom_user_attribute_from_request_hook", "some-value")

def response_hook(span: Span, params: typing.Union[
aiohttp.TraceRequestEndParams,
aiohttp.TraceRequestExceptionParams,
]):
if span and span.is_recording():
span.set_attribute("custom_user_attribute_from_response_hook", "some-value")

AioHttpClientInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
if span and span.is_recording():
span.set_attribute(
"custom_user_attribute_from_request_hook", "some-value"
)


def response_hook(
span: Span,
params: typing.Union[
aiohttp.TraceRequestEndParams,
aiohttp.TraceRequestExceptionParams,
],
):
if span and span.is_recording():
span.set_attribute(
"custom_user_attribute_from_response_hook", "some-value"
)


AioHttpClientInstrumentor().instrument(
request_hook=request_hook, response_hook=response_hook
)

Exclude lists
*************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,36 @@
import asyncio
import aiopg
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor

# Call instrument() to wrap all database connections
AiopgInstrumentor().instrument()

dsn = 'user=user password=password host=127.0.0.1'
dsn = "user=user password=password host=127.0.0.1"


async def connect():
cnx = await aiopg.connect(dsn)
cursor = await cnx.cursor()
await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
await cursor.execute(
"CREATE TABLE IF NOT EXISTS test (testField INTEGER)"
)
await cursor.execute("INSERT INTO test (testField) VALUES (123)")
cursor.close()
cnx.close()


async def create_pool():
pool = await aiopg.create_pool(dsn)
cnx = await pool.acquire()
cursor = await cnx.cursor()
await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
await cursor.execute(
"CREATE TABLE IF NOT EXISTS test (testField INTEGER)"
)
await cursor.execute("INSERT INTO test (testField) VALUES (123)")
cursor.close()
cnx.close()


asyncio.run(connect())
asyncio.run(create_pool())

Expand All @@ -46,18 +54,22 @@ async def create_pool():
import aiopg
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor

dsn = 'user=user password=password host=127.0.0.1'
dsn = "user=user password=password host=127.0.0.1"


# Alternatively, use instrument_connection for an individual connection
async def go():
cnx = await aiopg.connect(dsn)
instrumented_cnx = AiopgInstrumentor().instrument_connection(cnx)
cursor = await instrumented_cnx.cursor()
await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)")
await cursor.execute(
"CREATE TABLE IF NOT EXISTS test (testField INTEGER)"
)
await cursor.execute("INSERT INTO test (testField) VALUES (123)")
cursor.close()
instrumented_cnx.close()


asyncio.run(go())

API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
app = Quart(__name__)
app.asgi_app = OpenTelemetryMiddleware(app.asgi_app)


@app.route("/")
async def hello():
return "Hello!"


if __name__ == "__main__":
app.run(debug=True)

Expand All @@ -36,7 +38,7 @@ async def hello():
from django.core.asgi import get_asgi_application
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'asgi_example.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "asgi_example.settings")

application = get_asgi_application()
application = OpenTelemetryMiddleware(application)
Expand Down Expand Up @@ -75,33 +77,57 @@ async def hello():
from asgiref.typing import Scope, ASGIReceiveEvent, ASGISendEvent
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware

async def application(scope: Scope, receive: ASGIReceiveEvent, send: ASGISendEvent):
await send({
'type': 'http.response.start',
'status': 200,
'headers': [
[b'content-type', b'text/plain'],
],
})

await send({
'type': 'http.response.body',
'body': b'Hello, world!',
})
async def application(
scope: Scope, receive: ASGIReceiveEvent, send: ASGISendEvent
):
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [
[b"content-type", b"text/plain"],
],
}
)

await send(
{
"type": "http.response.body",
"body": b"Hello, world!",
}
)


def server_request_hook(span: Span, scope: Scope):
if span and span.is_recording():
span.set_attribute("custom_user_attribute_from_request_hook", "some-value")
span.set_attribute(
"custom_user_attribute_from_request_hook", "some-value"
)


def client_request_hook(span: Span, scope: Scope, message: dict[str, Any]):
if span and span.is_recording():
span.set_attribute("custom_user_attribute_from_client_request_hook", "some-value")
span.set_attribute(
"custom_user_attribute_from_client_request_hook", "some-value"
)

def client_response_hook(span: Span, scope: Scope, message: dict[str, Any]):

def client_response_hook(
span: Span, scope: Scope, message: dict[str, Any]
):
if span and span.is_recording():
span.set_attribute("custom_user_attribute_from_response_hook", "some-value")
span.set_attribute(
"custom_user_attribute_from_response_hook", "some-value"
)


OpenTelemetryMiddleware(application, server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook)
OpenTelemetryMiddleware(
application,
server_request_hook=server_request_hook,
client_request_hook=client_request_hook,
client_response_hook=client_response_hook,
)

Capture HTTP request and response headers
*****************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@

AsyncClickInstrumentor().instrument()


@asyncclick.command()
async def hello():
asyncclick.echo(f'Hello world!')
asyncclick.echo(f"Hello world!")


if __name__ == "__main__":
asyncio.run(hello())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
# You can optionally pass a custom TracerProvider to AsyncPGInstrumentor.instrument()
AsyncPGInstrumentor().instrument()


async def main():
conn = await asyncpg.connect(user='user', password='password')
conn = await asyncpg.connect(user="user", password="password")

await conn.fetch('''SELECT 42;''')

await conn.close()


asyncio.run(main())

API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

ClickInstrumentor().instrument()


@click.command()
def hello():
click.echo(f'Hello world!')
click.echo(f"Hello world!")


if __name__ == "__main__":
hello()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@

app = falcon.App()


class HelloWorldResource(object):
def on_get(self, req, resp):
resp.text = 'Hello World'
resp.text = "Hello World"


app.add_route('/hello', HelloWorldResource())
app.add_route("/hello", HelloWorldResource())


Request and Response hooks
Expand All @@ -71,13 +73,18 @@ def on_get(self, req, resp):

from opentelemetry.instrumentation.falcon import FalconInstrumentor


def request_hook(span, req):
pass


def response_hook(span, req, resp):
pass

FalconInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)

FalconInstrumentor().instrument(
request_hook=request_hook, response_hook=response_hook
)

Capture HTTP request and response headers
*****************************************
Expand Down
Loading
Loading