-
Notifications
You must be signed in to change notification settings - Fork 807
Support passing tracer provider in aiohttp server instrumentation lib #3819
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
Open
bisgaard-itis
wants to merge
16
commits into
open-telemetry:main
Choose a base branch
from
bisgaard-itis:3801-support-passing-TracerProvider-in-aiohttp-server-instrumentation-lib
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.
+148
−69
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a54fdd3
allow to pass tracer_provider when instrumenting aiohttp server
bisgaard-itis 8772240
minor fix
bisgaard-itis 8021f70
add note to indicate backwards compatibility
bisgaard-itis 09c4164
small fix
bisgaard-itis 7d1be5f
add test
bisgaard-itis 07f833f
update changelog
bisgaard-itis d5be4a4
Only decode JSON input buffer in Anthropic Claude streaming (#3875)
thpierce 54cf1b2
aiohttp-client: add support for url exclusions (#3850)
xrmx a7b4a88
update changelog
bisgaard-itis a89c17f
@tammy-baylis-swi add link to PR
bisgaard-itis b679ee1
create factory for instrumented application
bisgaard-itis 4a57b3e
run precommit hooks
bisgaard-itis 35cce28
update readthedocs
bisgaard-itis 6ffc359
cleanup changelog after merge conflicts
bisgaard-itis c0d6ec7
Merge remote-tracking branch 'otel/main' into 3801-support-passing-Tr…
bisgaard-itis 9460dde
make pre-commit happy
bisgaard-itis 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
This file contains hidden or 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
This file contains hidden or 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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,8 +25,13 @@ | |||||||
| from opentelemetry.instrumentation.aiohttp_server import ( | ||||||||
| AioHttpServerInstrumentor | ||||||||
| ) | ||||||||
| from opentelemetry.sdk.resources import Resource | ||||||||
| from opentelemetry.sdk.trace import TracerProvider | ||||||||
| from opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBased | ||||||||
|
|
||||||||
| AioHttpServerInstrumentor().instrument() | ||||||||
| resource = Resource(attributes={"service.name": "my-aiohttp-service"}) | ||||||||
|
Contributor
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. Great! I would also add a comment so newer/quickstart users know that these are optional:
Suggested change
|
||||||||
| sampler = ParentBased(root=TraceIdRatioBased(rate=0.25)) # sample 25% of traces | ||||||||
| AioHttpServerInstrumentor().instrument(tracer_provider=TracerProvider(resource=resource, sampler=sampler)) | ||||||||
|
|
||||||||
| async def hello(request): | ||||||||
| return web.Response(text="Hello, world") | ||||||||
|
|
@@ -248,63 +253,87 @@ def keys(self, carrier: Dict) -> List: | |||||||
| getter = AiohttpGetter() | ||||||||
|
|
||||||||
|
|
||||||||
| @web.middleware | ||||||||
| async def middleware(request, handler): | ||||||||
| """Middleware for aiohttp implementing tracing logic""" | ||||||||
| if not is_http_instrumentation_enabled() or _excluded_urls.url_disabled( | ||||||||
| request.url.path | ||||||||
| ): | ||||||||
| return await handler(request) | ||||||||
| def create_aiohttp_middleware( | ||||||||
| tracer_provider: trace.TracerProvider | None = None, | ||||||||
| ): | ||||||||
| _tracer = ( | ||||||||
| tracer_provider.get_tracer(__name__, __version__) | ||||||||
| if tracer_provider | ||||||||
| else tracer | ||||||||
| ) | ||||||||
|
|
||||||||
| span_name, additional_attributes = get_default_span_details(request) | ||||||||
| @web.middleware | ||||||||
| async def _middleware(request, handler): | ||||||||
| """Middleware for aiohttp implementing tracing logic""" | ||||||||
| if ( | ||||||||
| not is_http_instrumentation_enabled() | ||||||||
| or _excluded_urls.url_disabled(request.url.path) | ||||||||
| ): | ||||||||
| return await handler(request) | ||||||||
|
|
||||||||
| span_name, additional_attributes = get_default_span_details(request) | ||||||||
|
|
||||||||
| req_attrs = collect_request_attributes(request) | ||||||||
| duration_attrs = _parse_duration_attrs(req_attrs) | ||||||||
| active_requests_count_attrs = _parse_active_request_count_attrs( | ||||||||
| req_attrs | ||||||||
| ) | ||||||||
|
|
||||||||
| req_attrs = collect_request_attributes(request) | ||||||||
| duration_attrs = _parse_duration_attrs(req_attrs) | ||||||||
| active_requests_count_attrs = _parse_active_request_count_attrs(req_attrs) | ||||||||
| duration_histogram = meter.create_histogram( | ||||||||
| name=MetricInstruments.HTTP_SERVER_DURATION, | ||||||||
| unit="ms", | ||||||||
| description="Measures the duration of inbound HTTP requests.", | ||||||||
| ) | ||||||||
|
|
||||||||
| duration_histogram = meter.create_histogram( | ||||||||
| name=MetricInstruments.HTTP_SERVER_DURATION, | ||||||||
| unit="ms", | ||||||||
| description="Measures the duration of inbound HTTP requests.", | ||||||||
| ) | ||||||||
| active_requests_counter = meter.create_up_down_counter( | ||||||||
| name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS, | ||||||||
| unit="requests", | ||||||||
| description="measures the number of concurrent HTTP requests those are currently in flight", | ||||||||
| ) | ||||||||
|
|
||||||||
| with _tracer.start_as_current_span( | ||||||||
| span_name, | ||||||||
| context=extract(request, getter=getter), | ||||||||
| kind=trace.SpanKind.SERVER, | ||||||||
| ) as span: | ||||||||
| attributes = collect_request_attributes(request) | ||||||||
| attributes.update(additional_attributes) | ||||||||
| span.set_attributes(attributes) | ||||||||
| start = default_timer() | ||||||||
| active_requests_counter.add(1, active_requests_count_attrs) | ||||||||
| try: | ||||||||
| resp = await handler(request) | ||||||||
| set_status_code(span, resp.status) | ||||||||
| except web.HTTPException as ex: | ||||||||
| set_status_code(span, ex.status_code) | ||||||||
| raise | ||||||||
| finally: | ||||||||
| duration = max((default_timer() - start) * 1000, 0) | ||||||||
| duration_histogram.record(duration, duration_attrs) | ||||||||
| active_requests_counter.add(-1, active_requests_count_attrs) | ||||||||
| return resp | ||||||||
|
|
||||||||
| return _middleware | ||||||||
|
|
||||||||
| active_requests_counter = meter.create_up_down_counter( | ||||||||
| name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS, | ||||||||
| unit="requests", | ||||||||
| description="measures the number of concurrent HTTP requests those are currently in flight", | ||||||||
| ) | ||||||||
|
|
||||||||
| with tracer.start_as_current_span( | ||||||||
| span_name, | ||||||||
| context=extract(request, getter=getter), | ||||||||
| kind=trace.SpanKind.SERVER, | ||||||||
| ) as span: | ||||||||
| attributes = collect_request_attributes(request) | ||||||||
| attributes.update(additional_attributes) | ||||||||
| span.set_attributes(attributes) | ||||||||
| start = default_timer() | ||||||||
| active_requests_counter.add(1, active_requests_count_attrs) | ||||||||
| try: | ||||||||
| resp = await handler(request) | ||||||||
| set_status_code(span, resp.status) | ||||||||
| except web.HTTPException as ex: | ||||||||
| set_status_code(span, ex.status_code) | ||||||||
| raise | ||||||||
| finally: | ||||||||
| duration = max((default_timer() - start) * 1000, 0) | ||||||||
| duration_histogram.record(duration, duration_attrs) | ||||||||
| active_requests_counter.add(-1, active_requests_count_attrs) | ||||||||
| return resp | ||||||||
|
|
||||||||
|
|
||||||||
| class _InstrumentedApplication(web.Application): | ||||||||
| """Insert tracing middleware""" | ||||||||
|
|
||||||||
| def __init__(self, *args, **kwargs): | ||||||||
| middlewares = kwargs.pop("middlewares", []) | ||||||||
| middlewares.insert(0, middleware) | ||||||||
| kwargs["middlewares"] = middlewares | ||||||||
| super().__init__(*args, **kwargs) | ||||||||
| middleware = create_aiohttp_middleware() # for backwards compatibility | ||||||||
|
|
||||||||
|
|
||||||||
| def create_instrumented_application( | ||||||||
| tracer_provider: trace.TracerProvider | None = None, | ||||||||
| ): | ||||||||
| _middleware = create_aiohttp_middleware(tracer_provider=tracer_provider) | ||||||||
|
|
||||||||
| class _InstrumentedApplication(web.Application): | ||||||||
| """Insert tracing middleware""" | ||||||||
|
|
||||||||
| def __init__(self, *args, **kwargs): | ||||||||
| middlewares = kwargs.pop("middlewares", []) | ||||||||
| middlewares.insert(0, _middleware) | ||||||||
| kwargs["middlewares"] = middlewares | ||||||||
| super().__init__(*args, **kwargs) | ||||||||
|
|
||||||||
| return _InstrumentedApplication | ||||||||
|
|
||||||||
|
|
||||||||
| class AioHttpServerInstrumentor(BaseInstrumentor): | ||||||||
|
|
@@ -315,6 +344,10 @@ class AioHttpServerInstrumentor(BaseInstrumentor): | |||||||
| """ | ||||||||
|
|
||||||||
| def _instrument(self, **kwargs): | ||||||||
| tracer_provider = kwargs.get("tracer_provider", None) | ||||||||
bisgaard-itis marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| assert tracer_provider is None or isinstance( | ||||||||
| tracer_provider, trace.TracerProvider | ||||||||
| ) | ||||||||
| # update global values at instrument time so we can test them | ||||||||
| global _excluded_urls # pylint: disable=global-statement | ||||||||
| _excluded_urls = get_excluded_urls("AIOHTTP_SERVER") | ||||||||
|
|
@@ -326,6 +359,10 @@ def _instrument(self, **kwargs): | |||||||
| meter = metrics.get_meter(__name__, __version__) | ||||||||
|
|
||||||||
| self._original_app = web.Application | ||||||||
|
|
||||||||
| _InstrumentedApplication = create_instrumented_application( | ||||||||
| tracer_provider=tracer_provider | ||||||||
| ) | ||||||||
| setattr(web, "Application", _InstrumentedApplication) | ||||||||
|
|
||||||||
| def _uninstrument(self, **kwargs): | ||||||||
|
|
||||||||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.