Skip to content
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

Fix tomcat async spans #4339

Merged
merged 6 commits into from
Oct 13, 2021
Merged

Fix tomcat async spans #4339

merged 6 commits into from
Oct 13, 2021

Conversation

trask
Copy link
Member

@trask trask commented Oct 10, 2021

Resolves #4334

Adds test for all server instrumentation to verify that server spans end after all of their child spans (with a couple of exceptions).

@anuraaga
Copy link
Contributor

@trask Just curious does this have anything to do with the strict context stuff we talked about?

return null;
}));
ctx -> {
HttpServerTest.controller(QUERY_PARAM, () -> null);
Copy link
Member

Choose a reason for hiding this comment

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

WDYT about removing the second argument of controller? The passed lambda seems to be unneeded now

Copy link
Member Author

Choose a reason for hiding this comment

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

it's still needed in some cases. i'm not super convinced about changing the structure of the controllers the way I did, since the existing structure probably mimic'd user code better. so i reverted those changes, and just suppressed the test in more places for now short of a better idea

@@ -32,7 +33,9 @@ public boolean shouldStart(Context parentContext, Request request) {
}

public Context start(Context parentContext, Request request) {
return instrumenter.start(parentContext, request);
Context context = instrumenter.start(parentContext, request);
request.setAttribute(HttpServerTracer.CONTEXT_ATTRIBUTE, context);
Copy link
Member

Choose a reason for hiding this comment

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

Is this the actual fix?

Copy link
Contributor

Choose a reason for hiding this comment

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

It should be. The problem is in

for async listeners we need to get the context for server span which servlet instrumentation stores in a request attribute. This works because HttpServletRequest implementation on tomcat stores attributes on the underlying coyote request. The only other server that does not use servlet api based instrumenter is undertow where request is always ended from a callback and presumably doesn't need special handling for async requests.
I guess alternatively we could add a field to AppServerBridge to carry context and in ServletHelper try both AppServerBridge and request attribute.

Copy link
Member Author

Choose a reason for hiding this comment

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

this would be nice if it leads to unifying app server behavior a bit, I created #4347 to revisit the idea

@trask
Copy link
Member Author

trask commented Oct 11, 2021

@trask Just curious does this have anything to do with the strict context stuff we talked about?

there's definitely overlap, the new test points out places where the server span can end before the controller span

IIRC one option we discussed previously was introducing some mechanism to delay the span end until the active scope was closed

@trask trask merged commit 53a639b into open-telemetry:main Oct 13, 2021
@trask trask deleted the fix-tomcat-async-spans branch October 13, 2021 20:04
This was referenced Oct 15, 2021
RashmiRam pushed a commit to RashmiRam/opentelemetry-auto-instr-java that referenced this pull request May 23, 2022
* Add test

* Fix tomcat async spans

* Preserve existing test controller behavior

* Comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duration of request is wrong if method returns Mono (spring webflux controller)
4 participants