-
Notifications
You must be signed in to change notification settings - Fork 614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add type hints to Starlette instrumentation #3045
Open
Kludex
wants to merge
6
commits into
open-telemetry:main
Choose a base branch
from
Kludex:add-type-hints-to-starlette
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+44
−26
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
45989bb
Add type hints to Starlette instrumentation
Kludex 92d122a
format
Kludex c88ac3f
Add changelog
Kludex 12f2017
Add changelog
Kludex b3fc84d
Merge branch 'main' into add-type-hints-to-starlette
xrmx 0f52d0a
Remove pyright ignore
Kludex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,8 +169,11 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A | |
API | ||
--- | ||
""" | ||
# pyright: reportPrivateUsage=false | ||
Kludex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
from typing import Collection | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any, Collection, cast | ||
|
||
from starlette import applications | ||
from starlette.routing import Match | ||
|
@@ -184,18 +187,29 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A | |
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor | ||
from opentelemetry.instrumentation.starlette.package import _instruments | ||
from opentelemetry.instrumentation.starlette.version import __version__ | ||
from opentelemetry.metrics import get_meter | ||
from opentelemetry.metrics import MeterProvider, get_meter | ||
from opentelemetry.semconv.trace import SpanAttributes | ||
from opentelemetry.trace import get_tracer | ||
from opentelemetry.trace import TracerProvider, get_tracer | ||
from opentelemetry.util.http import get_excluded_urls | ||
|
||
if TYPE_CHECKING: | ||
from typing import NotRequired, TypedDict, Unpack | ||
|
||
class InstrumentKwargs(TypedDict): | ||
Kludex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tracer_provider: NotRequired[TracerProvider] | ||
meter_provider: NotRequired[MeterProvider] | ||
emdneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
server_request_hook: NotRequired[ServerRequestHook] | ||
client_request_hook: NotRequired[ClientRequestHook] | ||
client_response_hook: NotRequired[ClientResponseHook] | ||
|
||
|
||
_excluded_urls = get_excluded_urls("STARLETTE") | ||
|
||
|
||
class StarletteInstrumentor(BaseInstrumentor): | ||
"""An instrumentor for starlette | ||
"""An instrumentor for Starlette. | ||
|
||
See `BaseInstrumentor` | ||
See `BaseInstrumentor`. | ||
""" | ||
|
||
_original_starlette = None | ||
|
@@ -206,8 +220,8 @@ def instrument_app( | |
server_request_hook: ServerRequestHook = None, | ||
client_request_hook: ClientRequestHook = None, | ||
client_response_hook: ClientResponseHook = None, | ||
meter_provider=None, | ||
tracer_provider=None, | ||
meter_provider: MeterProvider | None = None, | ||
tracer_provider: TracerProvider | None = None, | ||
): | ||
"""Instrument an uninstrumented Starlette application.""" | ||
tracer = get_tracer( | ||
|
@@ -253,7 +267,7 @@ def uninstrument_app(app: applications.Starlette): | |
def instrumentation_dependencies(self) -> Collection[str]: | ||
return _instruments | ||
|
||
def _instrument(self, **kwargs): | ||
def _instrument(self, **kwargs: Unpack[InstrumentKwargs]): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The base class needs the proper type hints for us to remove this |
||
self._original_starlette = applications.Starlette | ||
_InstrumentedStarlette._tracer_provider = kwargs.get("tracer_provider") | ||
_InstrumentedStarlette._server_request_hook = kwargs.get( | ||
|
@@ -269,7 +283,7 @@ def _instrument(self, **kwargs): | |
|
||
applications.Starlette = _InstrumentedStarlette | ||
|
||
def _uninstrument(self, **kwargs): | ||
def _uninstrument(self, **kwargs: Any): | ||
"""uninstrumenting all created apps by user""" | ||
for instance in _InstrumentedStarlette._instrumented_starlette_apps: | ||
self.uninstrument_app(instance) | ||
|
@@ -278,14 +292,14 @@ def _uninstrument(self, **kwargs): | |
|
||
|
||
class _InstrumentedStarlette(applications.Starlette): | ||
_tracer_provider = None | ||
_meter_provider = None | ||
_tracer_provider: TracerProvider | None = None | ||
_meter_provider: MeterProvider | None = None | ||
_server_request_hook: ServerRequestHook = None | ||
_client_request_hook: ClientRequestHook = None | ||
_client_response_hook: ClientResponseHook = None | ||
_instrumented_starlette_apps = set() | ||
_instrumented_starlette_apps: set[applications.Starlette] = set() | ||
|
||
def __init__(self, *args, **kwargs): | ||
def __init__(self, *args: Any, **kwargs: Any): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to find something better here: python/typing#1889 |
||
super().__init__(*args, **kwargs) | ||
tracer = get_tracer( | ||
__name__, | ||
|
@@ -318,21 +332,22 @@ def __del__(self): | |
_InstrumentedStarlette._instrumented_starlette_apps.remove(self) | ||
|
||
|
||
def _get_route_details(scope): | ||
def _get_route_details(scope: dict[str, Any]) -> str | None: | ||
""" | ||
Function to retrieve Starlette route from scope. | ||
Function to retrieve Starlette route from ASGI scope. | ||
|
||
TODO: there is currently no way to retrieve http.route from | ||
a starlette application from scope. | ||
See: https://github.com/encode/starlette/pull/804 | ||
|
||
Args: | ||
scope: A Starlette scope | ||
scope: The ASGI scope that contains the Starlette application in the "app" key. | ||
|
||
Returns: | ||
A string containing the route or None | ||
The path to the route if found, otherwise None. | ||
""" | ||
app = scope["app"] | ||
route = None | ||
app = cast(applications.Starlette, scope["app"]) | ||
route: str | None = None | ||
|
||
for starlette_route in app.routes: | ||
match, _ = starlette_route.matches(scope) | ||
|
@@ -344,18 +359,20 @@ def _get_route_details(scope): | |
return route | ||
|
||
|
||
def _get_default_span_details(scope): | ||
""" | ||
Callback to retrieve span name and attributes from scope. | ||
def _get_default_span_details( | ||
scope: dict[str, Any], | ||
) -> tuple[str, dict[str, Any]]: | ||
Kludex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Callback to retrieve span name and attributes from ASGI scope. | ||
|
||
Args: | ||
scope: A Starlette scope | ||
scope: The ASGI scope that contains the Starlette application in the "app" key. | ||
|
||
Returns: | ||
A tuple of span name and attributes | ||
A tuple of span name and attributes. | ||
""" | ||
route = _get_route_details(scope) | ||
method = scope.get("method", "") | ||
attributes = {} | ||
method: str = scope.get("method", "") | ||
attributes: dict[str, Any] = {} | ||
if route: | ||
attributes[SpanAttributes.HTTP_ROUTE] = route | ||
if method and route: # http | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tells the type checkers that they can trust this one.