-
Notifications
You must be signed in to change notification settings - Fork 909
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
Record exception for async servlet invocations #4677
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.helper; | ||
|
||
import javax.servlet.AsyncContext; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.http.HttpServletRequest; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
@SuppressWarnings("unused") | ||
public class Servlet3AsyncContextStartAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void start( | ||
@Advice.This AsyncContext asyncContext, | ||
@Advice.Argument(value = 0, readOnly = false) Runnable runnable) { | ||
ServletRequest request = asyncContext.getRequest(); | ||
if (request instanceof HttpServletRequest) { | ||
runnable = helper().wrapAsyncRunnable((HttpServletRequest) request, runnable); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.servlet.v5_0.async; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.servlet.v5_0.Servlet5Singletons.helper; | ||
|
||
import jakarta.servlet.AsyncContext; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
@SuppressWarnings("unused") | ||
public class AsyncContextStartAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void start( | ||
@Advice.This AsyncContext asyncContext, | ||
@Advice.Argument(value = 0, readOnly = false) Runnable runnable) { | ||
ServletRequest request = asyncContext.getRequest(); | ||
if (request instanceof HttpServletRequest) { | ||
runnable = helper().wrapAsyncRunnable((HttpServletRequest) request, runnable); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,8 @@ public void onComplete(RESPONSE response) { | |
if (responseHandled.compareAndSet(false, true)) { | ||
ServletResponseContext<RESPONSE> responseContext = | ||
new ServletResponseContext<>(response, null); | ||
instrumenter.end(context, requestContext, responseContext, null); | ||
Throwable throwable = servletHelper.getAsyncException(requestContext.request()); | ||
instrumenter.end(context, requestContext, responseContext, throwable); | ||
Comment on lines
+38
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does an exception thrown from a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good question. If the response has already been sent then exception can not turn it back. This behaviour is the same for regular servlets. So it is possible to have requests that were ok from http client's perspective, but failed (got an exception) from server's perspective. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point 👍 |
||
} | ||
} | ||
|
||
|
@@ -46,7 +47,8 @@ public void onTimeout(long timeout) { | |
ServletResponseContext<RESPONSE> responseContext = | ||
new ServletResponseContext<>(response, null); | ||
responseContext.setTimeout(timeout); | ||
instrumenter.end(context, requestContext, responseContext, null); | ||
Throwable throwable = servletHelper.getAsyncException(requestContext.request()); | ||
instrumenter.end(context, requestContext, responseContext, throwable); | ||
} | ||
} | ||
|
||
|
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.
do u know if this is a limitation of jetty, our instrumentation, or our tests?
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.
It is hard to say. My guess is that our test is weird and could very well depend on unspecified behaviour. Test first completes an AsyncContext and then lets exception propagate out of servlet. It might be better to not complete AsyncContext and rely on timeout (if AsyncContext isn't completed container will close it with timeout). In that case the behaviour should be better defined, but timeout has its own issues. There is always the problem that we'd need to make the timeout as short as possible to not waste time but still have it long enough so that test doesn't timeout before the exception is thrown even when running under load.
The tests that are currently failing also exhibit weirdness, apparently on tomcat10 exception test can break the subsequent test.