Skip to content

Commit aec8e1f

Browse files
authored
Add better type hints for HTTPX and AsyncPG (#342)
1 parent 73ba675 commit aec8e1f

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
15
from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor
26

7+
if TYPE_CHECKING:
8+
from typing_extensions import TypedDict, Unpack
9+
10+
class AsyncPGInstrumentKwargs(TypedDict, total=False):
11+
skip_dep_check: bool
12+
313

4-
def instrument_asyncpg():
14+
def instrument_asyncpg(**kwargs: Unpack[AsyncPGInstrumentKwargs]) -> None:
515
"""Instrument the `asyncpg` module so that spans are automatically created for each query.
616
717
See the `Logfire.instrument_asyncpg` method for details.
818
"""
9-
AsyncPGInstrumentor().instrument() # type: ignore[reportUnknownMemberType]
19+
AsyncPGInstrumentor().instrument(**kwargs) # type: ignore[reportUnknownMemberType]

logfire/_internal/integrations/httpx.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
1-
from typing import Any
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
24

35
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
46

7+
if TYPE_CHECKING:
8+
from typing import Awaitable, Callable, TypedDict, Unpack
9+
10+
import httpx
11+
from opentelemetry.trace import Span
12+
13+
RequestHook = Callable[[Span, httpx.Request], None]
14+
ResponseHook = Callable[[Span, httpx.Request, httpx.Response], None]
15+
AsyncRequestHook = Callable[[Span, httpx.Request], Awaitable[None]]
16+
AsyncResponseHook = Callable[[Span, httpx.Request, httpx.Response], Awaitable[None]]
17+
18+
class HTTPXInstrumentKwargs(TypedDict, total=False):
19+
request_hook: RequestHook
20+
response_hook: ResponseHook
21+
async_request_hook: AsyncRequestHook
22+
async_response_hook: AsyncResponseHook
23+
skip_dep_check: bool
24+
525

6-
def instrument_httpx(**kwargs: Any):
26+
def instrument_httpx(**kwargs: Unpack[HTTPXInstrumentKwargs]) -> None:
727
"""Instrument the `httpx` module so that spans are automatically created for each request.
828
929
See the `Logfire.instrument_httpx` method for details.

logfire/_internal/main.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,7 @@
99
from functools import cached_property, partial
1010
from time import time
1111
from types import TracebackType
12-
from typing import (
13-
TYPE_CHECKING,
14-
Any,
15-
Callable,
16-
ContextManager,
17-
Iterable,
18-
Literal,
19-
Sequence,
20-
TypeVar,
21-
Union,
22-
cast,
23-
)
12+
from typing import TYPE_CHECKING, Any, Callable, ContextManager, Iterable, Literal, Sequence, TypeVar, Union, cast
2413

2514
import opentelemetry.context as context_api
2615
import opentelemetry.trace as trace_api
@@ -75,8 +64,10 @@
7564
from starlette.websockets import WebSocket
7665
from typing_extensions import Unpack
7766

67+
from .integrations.asyncpg import AsyncPGInstrumentKwargs
7868
from .integrations.celery import CeleryInstrumentKwargs
7969
from .integrations.flask import FlaskInstrumentKwargs
70+
from .integrations.httpx import HTTPXInstrumentKwargs
8071
from .integrations.psycopg import PsycopgInstrumentKwargs
8172
from .integrations.pymongo import PymongoInstrumentKwargs
8273
from .integrations.redis import RedisInstrumentKwargs
@@ -1033,14 +1024,14 @@ def instrument_anthropic(
10331024
is_async_client,
10341025
)
10351026

1036-
def instrument_asyncpg(self):
1027+
def instrument_asyncpg(self, **kwargs: Unpack[AsyncPGInstrumentKwargs]) -> None:
10371028
"""Instrument the `asyncpg` module so that spans are automatically created for each query."""
10381029
from .integrations.asyncpg import instrument_asyncpg
10391030

10401031
self._warn_if_not_initialized_for_instrumentation()
1041-
return instrument_asyncpg()
1032+
return instrument_asyncpg(**kwargs)
10421033

1043-
def instrument_httpx(self, **kwargs: Any):
1034+
def instrument_httpx(self, **kwargs: Unpack[HTTPXInstrumentKwargs]) -> None:
10441035
"""Instrument the `httpx` module so that spans are automatically created for each request.
10451036
10461037
Uses the

0 commit comments

Comments
 (0)