Skip to content

Commit 7e6b140

Browse files
authored
Document Requests kwargs (#679)
1 parent 2ef9725 commit 7e6b140

File tree

6 files changed

+65
-47
lines changed

6 files changed

+65
-47
lines changed

logfire-api/logfire_api/_internal/integrations/celery.pyi

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
from logfire import Logfire as Logfire
2-
from typing_extensions import TypedDict, Unpack
1+
from typing import Any
32

4-
class CeleryInstrumentKwargs(TypedDict, total=False):
5-
skip_dep_check: bool
6-
7-
def instrument_celery(logfire_instance: Logfire, **kwargs: Unpack[CeleryInstrumentKwargs]) -> None:
3+
def instrument_celery(**kwargs: Any) -> None:
84
"""Instrument the `celery` module so that spans are automatically created for each task.
95
106
See the `Logfire.instrument_celery` method for details.

logfire-api/logfire_api/_internal/integrations/requests.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from logfire import Logfire as Logfire
2-
from typing import Any
1+
import requests
2+
from opentelemetry.sdk.trace import Span as Span
3+
from typing import Any, Callable
34

4-
def instrument_requests(logfire_instance: Logfire, excluded_urls: str | None = None, **kwargs: Any):
5+
def instrument_requests(excluded_urls: str | None = None, request_hook: Callable[[Span, requests.PreparedRequest], None] | None = None, response_hook: Callable[[Span, requests.PreparedRequest, requests.Response], None] | None = None, **kwargs: Any) -> None:
56
"""Instrument the `requests` module so that spans are automatically created for each request.
67
78
See the `Logfire.instrument_requests` method for details.

logfire-api/logfire_api/_internal/main.pyi

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import anthropic
22
import httpx
33
import openai
44
import opentelemetry.trace as trace_api
5+
import requests
56
from . import async_ as async_
67
from ..version import VERSION as VERSION
78
from .auto_trace import AutoTraceModule as AutoTraceModule, install_auto_tracing as install_auto_tracing
@@ -13,7 +14,6 @@ from .instrument import instrument as instrument
1314
from .integrations.asgi import ASGIApp as ASGIApp, ASGIInstrumentKwargs as ASGIInstrumentKwargs
1415
from .integrations.asyncpg import AsyncPGInstrumentKwargs as AsyncPGInstrumentKwargs
1516
from .integrations.aws_lambda import AwsLambdaInstrumentKwargs as AwsLambdaInstrumentKwargs, LambdaHandler as LambdaHandler
16-
from .integrations.celery import CeleryInstrumentKwargs as CeleryInstrumentKwargs
1717
from .integrations.flask import FlaskInstrumentKwargs as FlaskInstrumentKwargs
1818
from .integrations.httpx import AsyncClientKwargs as AsyncClientKwargs, ClientKwargs as ClientKwargs, HTTPXInstrumentKwargs as HTTPXInstrumentKwargs
1919
from .integrations.mysql import MySQLConnection as MySQLConnection, MySQLInstrumentKwargs as MySQLInstrumentKwargs
@@ -556,12 +556,15 @@ class Logfire:
556556
def instrument_httpx(self, client: httpx.AsyncClient, capture_request_headers: bool = False, capture_response_headers: bool = False, **kwargs: Unpack[AsyncClientKwargs]) -> None: ...
557557
@overload
558558
def instrument_httpx(self, client: None = None, capture_request_headers: bool = False, capture_response_headers: bool = False, **kwargs: Unpack[HTTPXInstrumentKwargs]) -> None: ...
559-
def instrument_celery(self, **kwargs: Unpack[CeleryInstrumentKwargs]) -> None:
559+
def instrument_celery(self, **kwargs: Any) -> None:
560560
"""Instrument `celery` so that spans are automatically created for each task.
561561
562562
Uses the
563563
[OpenTelemetry Celery Instrumentation](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/celery/celery.html)
564564
library.
565+
566+
Args:
567+
**kwargs: Additional keyword arguments to pass to the OpenTelemetry `instrument` method, for future compatibility.
565568
"""
566569
def instrument_django(self, capture_headers: bool = False, is_sql_commentor_enabled: bool | None = None, request_hook: Callable[[Span, HttpRequest], None] | None = None, response_hook: Callable[[Span, HttpRequest, HttpResponse], None] | None = None, excluded_urls: str | None = None, **kwargs: Any) -> None:
567570
"""Instrument `django` so that spans are automatically created for each web request.
@@ -594,13 +597,14 @@ class Logfire:
594597
for future compatibility.
595598
596599
"""
597-
def instrument_requests(self, excluded_urls: str | None = None, **kwargs: Any) -> None:
600+
def instrument_requests(self, excluded_urls: str | None = None, request_hook: Callable[[Span, requests.PreparedRequest], None] | None = None, response_hook: Callable[[Span, requests.PreparedRequest, requests.Response], None] | None = None, **kwargs: Any) -> None:
598601
"""Instrument the `requests` module so that spans are automatically created for each request.
599602
600603
Args:
601604
excluded_urls: A string containing a comma-delimited list of regexes used to exclude URLs from tracking
602-
**kwargs: Additional keyword arguments to pass to the OpenTelemetry `instrument` methods,
603-
particularly `request_hook` and `response_hook`.
605+
request_hook: A function called right after a span is created for a request.
606+
response_hook: A function called right before a span is finished for the response.
607+
**kwargs: Additional keyword arguments to pass to the OpenTelemetry `instrument` methods, for future compatibility.
604608
"""
605609
def instrument_psycopg(self, conn_or_module: Any = None, **kwargs: Unpack[PsycopgInstrumentKwargs]) -> None:
606610
"""Instrument a `psycopg` connection or module so that spans are automatically created for each query.
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
3+
from typing import Any
44

55
try:
66
from opentelemetry.instrumentation.celery import CeleryInstrumentor
@@ -11,24 +11,10 @@
1111
" pip install 'logfire[celery]'"
1212
)
1313

14-
from logfire import Logfire
1514

16-
if TYPE_CHECKING:
17-
from typing_extensions import TypedDict, Unpack
18-
19-
class CeleryInstrumentKwargs(TypedDict, total=False):
20-
skip_dep_check: bool
21-
22-
23-
def instrument_celery(logfire_instance: Logfire, **kwargs: Unpack[CeleryInstrumentKwargs]) -> None:
15+
def instrument_celery(**kwargs: Any) -> None:
2416
"""Instrument the `celery` module so that spans are automatically created for each task.
2517
2618
See the `Logfire.instrument_celery` method for details.
2719
"""
28-
return CeleryInstrumentor().instrument(
29-
**{
30-
'tracer_provider': logfire_instance.config.get_tracer_provider(),
31-
'meter_provider': logfire_instance.config.get_meter_provider(),
32-
**kwargs,
33-
}
34-
)
20+
return CeleryInstrumentor().instrument(**kwargs)
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
from typing import Any, Optional
1+
from __future__ import annotations
2+
3+
from typing import Any, Callable
4+
5+
import requests
6+
from opentelemetry.sdk.trace import Span
27

38
try:
49
from opentelemetry.instrumentation.requests import RequestsInstrumentor
@@ -9,19 +14,20 @@
914
" pip install 'logfire[requests]'"
1015
)
1116

12-
from logfire import Logfire
13-
1417

15-
def instrument_requests(logfire_instance: Logfire, excluded_urls: Optional[str] = None, **kwargs: Any):
18+
def instrument_requests(
19+
excluded_urls: str | None = None,
20+
request_hook: Callable[[Span, requests.PreparedRequest], None] | None = None,
21+
response_hook: Callable[[Span, requests.PreparedRequest, requests.Response], None] | None = None,
22+
**kwargs: Any,
23+
) -> None:
1624
"""Instrument the `requests` module so that spans are automatically created for each request.
1725
1826
See the `Logfire.instrument_requests` method for details.
1927
"""
2028
RequestsInstrumentor().instrument(
2129
excluded_urls=excluded_urls,
22-
**{
23-
'tracer_provider': logfire_instance.config.get_tracer_provider(),
24-
'meter_provider': logfire_instance.config.get_meter_provider(),
25-
**kwargs,
26-
},
30+
request_hook=request_hook,
31+
response_hook=response_hook,
32+
**kwargs,
2733
)

logfire/_internal/main.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import anthropic
7373
import httpx
7474
import openai
75+
import requests
7576
from django.http import HttpRequest, HttpResponse
7677
from fastapi import FastAPI
7778
from flask.app import Flask
@@ -84,7 +85,6 @@
8485
from .integrations.asgi import ASGIApp, ASGIInstrumentKwargs
8586
from .integrations.asyncpg import AsyncPGInstrumentKwargs
8687
from .integrations.aws_lambda import AwsLambdaInstrumentKwargs, LambdaHandler
87-
from .integrations.celery import CeleryInstrumentKwargs
8888
from .integrations.flask import FlaskInstrumentKwargs
8989
from .integrations.httpx import AsyncClientKwargs, ClientKwargs, HTTPXInstrumentKwargs
9090
from .integrations.mysql import MySQLConnection, MySQLInstrumentKwargs
@@ -1224,17 +1224,26 @@ def instrument_httpx(
12241224
**kwargs,
12251225
)
12261226

1227-
def instrument_celery(self, **kwargs: Unpack[CeleryInstrumentKwargs]) -> None:
1227+
def instrument_celery(self, **kwargs: Any) -> None:
12281228
"""Instrument `celery` so that spans are automatically created for each task.
12291229
12301230
Uses the
12311231
[OpenTelemetry Celery Instrumentation](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/celery/celery.html)
12321232
library.
1233+
1234+
Args:
1235+
**kwargs: Additional keyword arguments to pass to the OpenTelemetry `instrument` method, for future compatibility.
12331236
"""
12341237
from .integrations.celery import instrument_celery
12351238

12361239
self._warn_if_not_initialized_for_instrumentation()
1237-
return instrument_celery(self, **kwargs)
1240+
return instrument_celery(
1241+
**{
1242+
'tracer_provider': self._config.get_tracer_provider(),
1243+
'meter_provider': self._config.get_meter_provider(),
1244+
**kwargs,
1245+
},
1246+
)
12381247

12391248
def instrument_django(
12401249
self,
@@ -1288,18 +1297,34 @@ def instrument_django(
12881297
**kwargs,
12891298
)
12901299

1291-
def instrument_requests(self, excluded_urls: str | None = None, **kwargs: Any) -> None:
1300+
def instrument_requests(
1301+
self,
1302+
excluded_urls: str | None = None,
1303+
request_hook: Callable[[Span, requests.PreparedRequest], None] | None = None,
1304+
response_hook: Callable[[Span, requests.PreparedRequest, requests.Response], None] | None = None,
1305+
**kwargs: Any,
1306+
) -> None:
12921307
"""Instrument the `requests` module so that spans are automatically created for each request.
12931308
12941309
Args:
12951310
excluded_urls: A string containing a comma-delimited list of regexes used to exclude URLs from tracking
1296-
**kwargs: Additional keyword arguments to pass to the OpenTelemetry `instrument` methods,
1297-
particularly `request_hook` and `response_hook`.
1311+
request_hook: A function called right after a span is created for a request.
1312+
response_hook: A function called right before a span is finished for the response.
1313+
**kwargs: Additional keyword arguments to pass to the OpenTelemetry `instrument` methods, for future compatibility.
12981314
"""
12991315
from .integrations.requests import instrument_requests
13001316

13011317
self._warn_if_not_initialized_for_instrumentation()
1302-
return instrument_requests(self, excluded_urls=excluded_urls, **kwargs)
1318+
return instrument_requests(
1319+
excluded_urls=excluded_urls,
1320+
request_hook=request_hook,
1321+
response_hook=response_hook,
1322+
**{
1323+
'tracer_provider': self._config.get_tracer_provider(),
1324+
'meter_provider': self._config.get_meter_provider(),
1325+
**kwargs,
1326+
},
1327+
)
13031328

13041329
def instrument_psycopg(self, conn_or_module: Any = None, **kwargs: Unpack[PsycopgInstrumentKwargs]) -> None:
13051330
"""Instrument a `psycopg` connection or module so that spans are automatically created for each query.

0 commit comments

Comments
 (0)