-
Notifications
You must be signed in to change notification settings - Fork 858
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
Refactor ServerSpanNaming
(in preparation for http.route
)
#4852
Changes from all 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 |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.tracer.ServerSpan; | ||
import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge; | ||
import io.opentelemetry.javaagent.bootstrap.servlet.MappingResolver; | ||
import io.opentelemetry.javaagent.instrumentation.api.CallDepth; | ||
|
@@ -39,52 +38,41 @@ public static void onEnter( | |
if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) { | ||
return; | ||
} | ||
HttpServletRequest httpServletRequest = (HttpServletRequest) request; | ||
|
||
callDepth = CallDepth.forClass(AppServerBridge.getCallDepthKey()); | ||
callDepth.getAndIncrement(); | ||
|
||
HttpServletRequest httpServletRequest = (HttpServletRequest) request; | ||
|
||
Context currentContext = Java8BytecodeBridge.currentContext(); | ||
Context attachedContext = helper().getServerContext(httpServletRequest); | ||
if (attachedContext != null && helper().needsRescoping(currentContext, attachedContext)) { | ||
MappingResolver mappingResolver = Servlet3Singletons.getMappingResolver(servletOrFilter); | ||
boolean servlet = servletOrFilter instanceof Servlet; | ||
attachedContext = | ||
helper().updateContext(attachedContext, httpServletRequest, mappingResolver, servlet); | ||
scope = attachedContext.makeCurrent(); | ||
// We are inside nested servlet/filter/app-server span, don't create new span | ||
return; | ||
} | ||
|
||
if (attachedContext != null || ServerSpan.fromContextOrNull(currentContext) != null) { | ||
// Update context with info from current request to ensure that server span gets the best | ||
// possible name. | ||
// In case server span was created by app server instrumentations calling updateContext | ||
// returns a new context that contains servlet context path that is used in other | ||
// instrumentations for naming server span. | ||
MappingResolver mappingResolver = Servlet3Singletons.getMappingResolver(servletOrFilter); | ||
boolean servlet = servletOrFilter instanceof Servlet; | ||
Context updatedContext = | ||
helper().updateContext(currentContext, httpServletRequest, mappingResolver, servlet); | ||
if (currentContext != updatedContext) { | ||
// updateContext updated context, need to re-scope | ||
scope = updatedContext.makeCurrent(); | ||
} | ||
// We are inside nested servlet/filter/app-server span, don't create new span | ||
return; | ||
} | ||
Context contextToUpdate; | ||
|
||
requestContext = new ServletRequestContext<>(httpServletRequest, servletOrFilter); | ||
|
||
if (!helper().shouldStart(currentContext, requestContext)) { | ||
return; | ||
if (attachedContext == null && helper().shouldStart(currentContext, requestContext)) { | ||
context = helper().start(currentContext, requestContext); | ||
helper().setAsyncListenerResponse(httpServletRequest, (HttpServletResponse) response); | ||
|
||
contextToUpdate = context; | ||
} else if (attachedContext != null | ||
&& helper().needsRescoping(currentContext, attachedContext)) { | ||
// Given request already has a context associated with it. | ||
// see the needsRescoping() javadoc for more explanation | ||
contextToUpdate = attachedContext; | ||
} else { | ||
// We are inside nested servlet/filter/app-server span, don't create new span | ||
contextToUpdate = currentContext; | ||
} | ||
|
||
context = helper().start(currentContext, requestContext); | ||
scope = context.makeCurrent(); | ||
|
||
helper().setAsyncListenerResponse(httpServletRequest, (HttpServletResponse) response); | ||
// Update context with info from current request to ensure that server span gets the best | ||
// possible name. | ||
// In case server span was created by app server instrumentations calling updateContext | ||
// returns a new context that contains servlet context path that is used in other | ||
// instrumentations for naming server span. | ||
MappingResolver mappingResolver = Servlet3Singletons.getMappingResolver(servletOrFilter); | ||
boolean servlet = servletOrFilter instanceof Servlet; | ||
contextToUpdate = | ||
helper().updateContext(contextToUpdate, httpServletRequest, mappingResolver, servlet); | ||
Comment on lines
+73
to
+74
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. the previous behavior was nice of setting everything up correctly in though I guess it doesn't matter in practice most of the time since we instrument so many servlet containers that we probably don't 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.
Honestly I found setting the Also, the only 2 instrumentations I found that have access to the route in the beginning are armeria and servlets; and servlet span name will usually be updated by something like spring or jax-rs. Which implies that for most cases, the route should be extracted on |
||
scope = contextToUpdate.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
|
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.
should
ServerSpanNaming.get()
context customizer be added here? I notice it's missing in some cases and included in others, but not clear to me whyThere 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 actually is added, via the
ServletInstrumenterBuilder
class.All server instrumentations should add
ServerSpanNaming.get()
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.
oh, indeed!