Skip to content

Conversation

@krnr
Copy link
Contributor

@krnr krnr commented Oct 25, 2025

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

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

add a new unit test:

  • test_url_params_instrumentation

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@krnr krnr requested a review from a team as a code owner October 25, 2025 05:15
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 25, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: krnr / name: yuri (bb73657)

@krnr krnr force-pushed the main branch 5 times, most recently from 1ba9a1c to 8689cfc Compare October 25, 2025 05:29
Copy link

@bisgaard-itis bisgaard-itis left a 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

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:

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?

Copy link
Contributor Author

@krnr krnr Oct 27, 2025

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)

Copy link
Contributor

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.

Copy link
Contributor Author

@krnr krnr Nov 6, 2025

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.

Copy link
Contributor Author

@krnr krnr Nov 6, 2025

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.

Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

@krnr krnr Nov 22, 2025

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.

Copy link
Contributor

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.

@xrmx xrmx moved this to Reviewed PRs that need fixes in @xrmx's Python PR digest Oct 31, 2025
@krnr krnr force-pushed the aiohttp-server branch 2 times, most recently from c05f041 to 3bb8b84 Compare November 5, 2025 20:14
@krnr krnr force-pushed the aiohttp-server branch 2 times, most recently from 5f4116a to 32ccac9 Compare November 8, 2025 07:46
@tammy-baylis-swi tammy-baylis-swi requested a review from a team November 10, 2025 17:23
Copy link
Contributor

@tammy-baylis-swi tammy-baylis-swi left a 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>
@xrmx xrmx moved this from Ready for review to Reviewed PRs that need fixes in @xrmx's Python PR digest Nov 20, 2025
@xrmx xrmx self-requested a review November 20, 2025 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

5 participants