Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Aiohttp server instrumentation #942

Closed

Conversation

dmanchon
Copy link
Contributor

@dmanchon dmanchon commented Mar 3, 2022

Description

Instrument aiohttp server. Right now there is only instrumentation for the client.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Mar 3, 2022

CLA Not Signed

):
return await handler(request)

token = context.attach(extract(request, getter=getter))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context.attach here is incorrect. The request being handled is from an external service, and the context contained within that request is a representation of the state of the external service. It doesn't make sense to set the external service's context as the current context for the service running this middleware.

The context should still be extracted from the request and supplied to tracer.start_as_current_span, but the attach and detach can be removed.

span_name,
kind=trace.SpanKind.SERVER,
) as span:
if span.is_recording():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this if statement should be removed. In the case that this is a non-recording span, set_attribute is a no-op. Additionally, this middleware should always call the request handler (line 158).

Comment on lines 155 to 156
for key, value in attributes.items():
span.set_attribute(key, value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with span.setattributes(attributes).

Comment on lines 157 to 161
try:
resp = await handler(request)
set_status_code(span, resp.status)
finally:
context.detach(token)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
try:
resp = await handler(request)
set_status_code(span, resp.status)
finally:
context.detach(token)
resp = await handler(request)
set_status_code(span, resp.status)

if (
context.get_value("suppress_instrumentation")
or context.get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY)
or _excluded_urls.url_disabled(request.url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request.url is not a str. This should be request.url.path.

@bmbouter
Copy link

@dmanchon I'm interested in testing and maybe helping to finish this PR? How can I test this PR after it's checked out? I have an aiohttp based app to test with. Also what do you know that needs finishing? The reviewer comments look all pretty straightforward, so after that what else is left?

Comment on lines 42 to 46
install_requires =
opentelemetry-api ~= 1.3
opentelemetry-semantic-conventions == 0.28b1
opentelemetry-instrumentation == 0.28b1
opentelemetry-util-http == 0.28b1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These requirements should be updated to the latest version


[options.extras_require]
test =
opentelemetry-test-utils == 0.28b1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to latest version

@dmanchon
Copy link
Contributor Author

Sorry @kennytrytek-wf @bmbouter for late response, i completely forgot about this. We currently have a vendored version of this in our codebase and given the lack of response here and in the official slack channel i undertood that there were not interest.
Dont have much time lately, but i will try to keep up, thanks @kennytrytek-wf for your comments and review, and sure @bmbouter if you have time to complete the PR with the suggestions and tests that will be amazing, i think testing with a basic aiohtttp server app will be enough.
I remember one of my concerns was that aiohttp is both server and client, given that client already exists i wonder if this should live in the same module as the client or as an independent module, i guess if we want autoinstrumentation based on the installed packages it will end up installing both server and client modules.

@dralley
Copy link

dralley commented Feb 20, 2023

I remember one of my concerns was that aiohttp is both server and client, given that client already exists i wonder if this should live in the same module as the client or as an independent module, i guess if we want autoinstrumentation based on the installed packages it will end up installing both server and client modules.

Both server and client in one package is probably the best option by virtue of being simple + not real upsides of separating it. It's ultimately not that much extra code.

@decko
Copy link
Contributor

decko commented Apr 5, 2023

@dmanchon correct me if I'm wrong, but I didn't see any code for metrics. Is that right? Would be interesting to have it in this PR?
Also, would be possible for contributing to this PR specifically? Or we need to open a PR to your fork for then to me merged into this one?

@dralley
Copy link

dralley commented Apr 5, 2023

@decko I would just open a new PR from your own branch which has these commits

@decko
Copy link
Contributor

decko commented Apr 5, 2023

@decko I would just open a new PR from your own branch which has these commits

@dralley what if we open it if we don't get a reply in two days?
In any case, I'm continuing the work from where @dmanchon left.

) as span:
attributes = collect_request_attributes(request)
attributes.update(additional_attributes)
span.setattributes(attributes)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

little misspelling

Suggested change
span.setattributes(attributes)
span.set_attributes(attributes)

@lzchen
Copy link
Contributor

lzchen commented Mar 7, 2024

Duplicate of #1800

@lzchen lzchen marked this as a duplicate of #1800 Mar 7, 2024
@lzchen lzchen closed this Mar 7, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants