-
Notifications
You must be signed in to change notification settings - Fork 822
Use canonical name for aiohttp request span name #3896
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
base: main
Are you sure you want to change the base?
Conversation
|
|
1ba9a1c to
8689cfc
Compare
bisgaard-itis
left a comment
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.
Looks good. Thanks a lot for the effort! 👍🏻
| for request_path, span in zip( | ||
| example_paths, memory_exporter.get_finished_spans() | ||
| ): | ||
| assert url == span.name |
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.
👍🏻
| a tuple of the span name, and any attributes to attach to the span. | ||
| """ | ||
| span_name = request.path.strip() or f"HTTP {request.method}" | ||
| if request.match_info and request.match_info.route.resource: |
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.
Is match_info.route guaranteed to be not None when match_info is not None?
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.
thank you for the good point. i think this can be the proof: https://github.com/aio-libs/aiohttp/blob/v3.13.1/aiohttp/web_urldispatcher.py#L271
(in fact, even match_info can't be None: https://github.com/aio-libs/aiohttp/blob/v3.13.1/aiohttp/web_request.py#L890 but it may raise issues with type hints)
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.
I'm not super familiar with aiohttp, but it could we be extra careful and wrap this with a try-except AttributeError? So it's like _get_view_func below.
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.
i am pretty sure that the attributes always exist: https://github.com/aio-libs/aiohttp/blob/v3.5.0/aiohttp/web_request.py#L704 https://github.com/aio-libs/aiohttp/blob/v3.5.0/aiohttp/web_urldispatcher.py#L161 (at least since v3.5.0)
but we definitely can add this "extra-safe" layer. i am just not sure yet what should the name be in case of AttributeError - the default one from request.path or some sentinel because "we-found-smth-unexpected"... having thought about it for a while i decided that the first option is better. let me know if you have any other ideas.
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.
in fact, there's another possibility of "high cardinality abuse": https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3896/files#diff-682c3e961f134b947f243e707c7a75856ea5640e0dfb8f32dad7ffd1b3fa8c9eR145
every non existent name is a span name by itself. so, you can generate an endless amount of 404 and each of them will be a different span. this logic should be (should it?) fixed in a separate PR, because i can't decide right on the spot what shall we do with 404 span names. should they all be "GET <Not Found>" if there's no match_info (i think so, and only get the path from the span attributes), or some users rely on the existing behavior and collect an "inventory" of such calls using span names? i don't know.
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.
Hi again @krnr , sorry for a bit of scope creep but I think it's important since this will be a breaking change (though a needed one!)
I cc'd you on a very similar PR for the ASGI instrumentor. The change for span name cardinality+semconv will need to be behind an opt-in flag for users. Would it be reasonable to implement something similar here, or in a separate PR? cc @aabmass
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.
(now it's my turn for being sorry to be late) well, as they say "better be safe than sorry". i agree with a flag idea, however, it's beyond my mind to find a good name for that. OTEL_PYTHON_AIOHTTP_SERVER_CANONICAL_PATH_SPAN_NAME ? too verbose?
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.
There is a shared util at opentelemetry.instrumentation._semconv, which reads OTEL_SEMCONV_STABILITY_OPT_IN for us. This might require a middleware update to accept something like sem_conv_opt_in_mode, like what the AsgiInstrumentor does.
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.
i apologize again for a late response @tammy-baylis-swi . today i checked the semconv module and it seems i misunderstood the concept a bit. the semconv is about the names of attributes (of smth - span, metric, whatever). not about the values of the attributes. and here we're talking about the value of span.name, "should it be 'fooooooooooo' or 'foo...'" and if that must be decided by a feature-flag, that is out of the scope of semconv feature flags in my opinion.
moreover, introducing semconv leads to a lot of changes unrelated to the topic of this PR. i'm happy to continue working on opt-in, but in a separate branch and a separate PR. i've already made a draft commit ( cc882b4 ) in a separate branch to illustrate how much is being changed and - more importantly - how many new discussion points emerge. which in turn will draw the attention from the span name even further.
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.
Hi @krnr no problem at all! I had some chat with others last week and this PR you've already done will be fine as it is. ✅ We've approved and we'll merge it for an upcoming release.
I'd say don't worry about the opt-in for span name, i.e. don't do what I asked here. It is indeed confusing. The semconv does have Must/Should for span name. You're also right in that the Python implementation so far is focused on the names of attributes.
If you do want to put in a separate PR at some point for names of attributes, indeed it would be a lot of change. You're welcome to work on it if you're interested! Otherwise I'd say this much-needed fix is done.
...-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py
Outdated
Show resolved
Hide resolved
c05f041 to
3bb8b84
Compare
Co-authored-by: Tammy Baylis <96076570+tammy-baylis-swi@users.noreply.github.com>
5f4116a to
32ccac9
Compare
...-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py
Outdated
Show resolved
Hide resolved
tammy-baylis-swi
left a comment
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.
Thank you @krnr for this. Lgtm though I want some other Approvers to have a look at this change.
If you wanted to bring this up more synchronously, please feel welcome to join the Python SIG meeting on Zoom on Thursdays -- Calendar with links here. Else this PR should still be visible on the PR digest board.
…rc/opentelemetry/instrumentation/aiohttp_server/__init__.py Co-authored-by: Tammy Baylis <96076570+tammy-baylis-swi@users.noreply.github.com>
Description
Current implementation doesn't use canonical attribute of a
Resource, which leads to the situation when generated spans all have unique names. It creates high cardinality of spans. Other frameworks that use path params create spans with "canonical" paths in names (e.g. fastapi [1] )Type of change
How Has This Been Tested?
add a new unit test:
test_url_params_instrumentationDoes This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.