Add ASGI middleware#716
Conversation
Signed-off-by: Emil Madsen <sovende@gmail.com>
…TP METHOD Signed-off-by: Emil Madsen <sovende@gmail.com>
toumorokoshi
left a comment
There was a problem hiding this comment.
Thanks for picking this up! looks like some changes are needed still, I copied over relevant comments from #402.
ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/version.py
Outdated
Show resolved
Hide resolved
ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/__init__.py
Outdated
Show resolved
Hide resolved
ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/__init__.py
Outdated
Show resolved
Hide resolved
ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/version.py
Outdated
Show resolved
Hide resolved
Co-authored-by: alrex <alrex.boten@gmail.com>
…telemetry-python into majorgreys/feature/ext_asgi
ca868c1 to
3ac4620
Compare
cb9f3cb to
9e00976
Compare
toumorokoshi
left a comment
There was a problem hiding this comment.
Great! Thanks for addressing feedback. LGTM.
florimondmanca
left a comment
There was a problem hiding this comment.
👋 Hey all, popping by as an ASGI nerd here. Overall this is fantastic work! Left some suggestions, mostly addressing nits wrt the ASGI spec. The core middleware functionality itself looks top notch.
| http_url = ( | ||
| scope.get("scheme") + "://" + server_host + scope.get("path", "") | ||
| if scope.get("scheme") and server_host and scope.get("path") | ||
| else None | ||
| ) |
There was a problem hiding this comment.
There is a root_path field in the ASGI HTTP scope that I don't think gets taken into account here.
This field is non-empty when an application is mounted as a route, which can be a problem if OpenTelemetryMiddleware is applied only to a sub-app instead of the entire application.
For example if a user has their web API built as a separate application, and they only want to trace that part of their app, they could do:
api = ...
api = OpenTelemetryMiddleware(api)
app = ...
app.mount("/api", api)In that case if a request hits /api/v1/users, then path will be "/v1/users" and root_path will be "/api" — but currently http_url would be wrong, e.g. https://myserver.io/v1/users.
I think concatenating root_path and path should be enough:
full_path = scope.get("root_path", "") + scope.get("path", "")
http_url = (
scheme + "://" + server_host + full_path
...
)
ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/__init__.py
Outdated
Show resolved
Hide resolved
ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/__init__.py
Outdated
Show resolved
Hide resolved
ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/__init__.py
Outdated
Show resolved
Hide resolved
ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/__init__.py
Outdated
Show resolved
Hide resolved
| if payload["type"] == "http.response.start": | ||
| status_code = payload["status"] | ||
| set_status_code(send_span, status_code) |
There was a problem hiding this comment.
Does OpenTelemetry support storing response headers? If so this is a good place to do it; otherwise please ignore. :-)
There was a problem hiding this comment.
Not that I'm aware of and do not see it in the wsgi instrumentation.
| send_span.set_attribute("type", payload["type"]) | ||
| await send(payload) | ||
|
|
||
| await self.app(scope, wrapped_receive, wrapped_send) |
There was a problem hiding this comment.
If OpenTelemetry supports some kind of "attach a traceback to a trace", then we could also try/except around the app call; otherwise, please ignore.
There was a problem hiding this comment.
The Python OpenTelemetry client does not store the traceback.
There was a problem hiding this comment.
I think this is still up for debate in the standard. But it's a good point, and something I know is highly valuable around existing DD integration. @majorgreys thoughts? should we raise this in the spec?
codeboten
left a comment
There was a problem hiding this comment.
Not an ASGI expert by any means, but it looks like @florimondmanca has that already covered :) The code looks good, just some nits around the copyright headers and setup.cfg versioning. Thanks for taking over this work!
ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/__init__.py
Outdated
Show resolved
Hide resolved
ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/version.py
Outdated
Show resolved
Hide resolved
| ] | ||
|
|
||
|
|
||
| def http_status_to_canonical_code(code: int, allow_redirect: bool = True): |
There was a problem hiding this comment.
Added an issue to track refactoring this code as it appears in at least 2 other places #735
Co-authored-by: Florimond Manca <florimond.manca@gmail.com> Co-authored-by: alrex <alrex.boten@gmail.com>
70f229a to
3849da1
Compare
|
@majorgreys Looks like you addressed all comments and have enough approvals. I'll merge this in the morning unless you have any objections. Thanks! |
|
thanks! |
Updating #402 which had been inactive for some time to get remaining to resolve merge conflicts and address any remaining issues.