-
Notifications
You must be signed in to change notification settings - Fork 1k
Library instrumentation for Java Servlet 3.0 Filters. #15188
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
Open
tylerbenson
wants to merge
15
commits into
open-telemetry:main
Choose a base branch
from
tylerbenson:tyler/servlet-filter-inst-lib
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cf0909f
Library instrumentation for Java Servlet 3.0 Filters.
tylerbenson 5852c35
passing unit tests
tylerbenson 133ca09
fix package name
tylerbenson 448e268
spotlessApply
tylerbenson 1ba1b2e
./gradlew generateFossaConfiguration
tylerbenson 7f44178
copy/embed all necessary classes.
tylerbenson 47e278b
Fix dependency declarations.
tylerbenson dab9ab5
Copy `enduser.id` key over and remove dependency.
tylerbenson 803ba35
./gradlew spotlessApply
otelbot[bot] e007089
Fix latestDepTests and add readme.
tylerbenson 6a6cfcf
Fix readme link.
tylerbenson 080c780
fix spelling
tylerbenson 11ef2a1
missing bracket.
tylerbenson 7e3860a
Reduce some test duplication by moving common classes to the main dir…
tylerbenson 7566c15
Use shadowJar to pull in dependencies instead of copy
tylerbenson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...a/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3RequestAdviceScope.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * 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 io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.javaagent.bootstrap.CallDepth; | ||
| import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder; | ||
| import io.opentelemetry.javaagent.bootstrap.servlet.MappingResolver; | ||
| import io.opentelemetry.javaagent.instrumentation.servlet.ServletRequestContext; | ||
| import javax.annotation.Nullable; | ||
| import javax.servlet.Servlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| public class Servlet3RequestAdviceScope { | ||
| private final CallDepth callDepth; | ||
| private final ServletRequestContext<HttpServletRequest> requestContext; | ||
| private final Context context; | ||
| private final Scope scope; | ||
|
|
||
| public Servlet3RequestAdviceScope( | ||
| CallDepth callDepth, | ||
| HttpServletRequest request, | ||
| HttpServletResponse response, | ||
| Object servletOrFilter) { | ||
| this.callDepth = callDepth; | ||
| this.callDepth.getAndIncrement(); | ||
|
|
||
| Context currentContext = Context.current(); | ||
| Context attachedContext = helper().getServerContext(request); | ||
| Context contextToUpdate; | ||
|
|
||
| requestContext = new ServletRequestContext<>(request, servletOrFilter); | ||
| if (attachedContext == null && helper().shouldStart(currentContext, requestContext)) { | ||
| context = helper().start(currentContext, requestContext); | ||
| helper().setAsyncListenerResponse(context, 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; | ||
| context = null; | ||
| } else { | ||
| // We are inside nested servlet/filter/app-server span, don't create new span | ||
| contextToUpdate = currentContext; | ||
| context = 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; | ||
| contextToUpdate = helper().updateContext(contextToUpdate, request, mappingResolver, servlet); | ||
| scope = contextToUpdate.makeCurrent(); | ||
|
|
||
| if (context != null) { | ||
| // Only trigger response customizer once, so only if server span was created here | ||
| HttpServerResponseCustomizerHolder.getCustomizer() | ||
| .customize(contextToUpdate, response, Servlet3Accessor.INSTANCE); | ||
| } | ||
| } | ||
|
|
||
| public void exit( | ||
| @Nullable Throwable throwable, HttpServletRequest request, HttpServletResponse response) { | ||
|
|
||
| boolean topLevel = callDepth.decrementAndGet() == 0; | ||
| helper().end(requestContext, request, response, throwable, topLevel, context, scope); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
.../io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3ResponseAdviceScope.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * 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.responseInstrumenter; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.instrumentation.api.incubator.semconv.util.ClassAndMethod; | ||
| import io.opentelemetry.javaagent.bootstrap.CallDepth; | ||
| import io.opentelemetry.javaagent.instrumentation.servlet.common.response.HttpServletResponseAdviceHelper; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| public class Servlet3ResponseAdviceScope { | ||
| private final CallDepth callDepth; | ||
| private final ClassAndMethod classAndMethod; | ||
| private final Context context; | ||
| private final Scope scope; | ||
|
|
||
| public Servlet3ResponseAdviceScope( | ||
| CallDepth callDepth, Class<?> declaringClass, String methodName) { | ||
| this.callDepth = callDepth; | ||
| if (callDepth.getAndIncrement() > 0) { | ||
| this.classAndMethod = null; | ||
| this.context = null; | ||
| this.scope = null; | ||
| return; | ||
| } | ||
| HttpServletResponseAdviceHelper.StartResult result = | ||
| HttpServletResponseAdviceHelper.startSpan( | ||
| responseInstrumenter(), declaringClass, methodName); | ||
| if (result != null) { | ||
| classAndMethod = result.getClassAndMethod(); | ||
| context = result.getContext(); | ||
| scope = result.getScope(); | ||
| } else { | ||
| classAndMethod = null; | ||
| context = null; | ||
| scope = null; | ||
| } | ||
| } | ||
|
|
||
| public void exit(@Nullable Throwable throwable) { | ||
| if (callDepth.decrementAndGet() > 0) { | ||
| return; | ||
| } | ||
| HttpServletResponseAdviceHelper.stopSpan( | ||
| responseInstrumenter(), throwable, context, scope, classAndMethod); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This change isn't necessary, but it seems odd that these are duplicated. I combined them, but if it's intentional I can revert this change.