-
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
10
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 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cd80806
Library instrumentation for Java Servlet 3.0 Filters.
tylerbenson 58773b5
passing unit tests
tylerbenson 4f0eb99
fix package name
tylerbenson dea3d1c
spotlessApply
tylerbenson a3e548b
./gradlew generateFossaConfiguration
tylerbenson 2e0d691
copy/embed all necessary classes.
tylerbenson 4b6cd18
Fix dependency declarations.
tylerbenson fc4f6b8
Copy `enduser.id` key over and remove dependency.
tylerbenson a34d722
./gradlew spotlessApply
otelbot[bot] 6ce59fa
Fix latestDepTests and add readme.
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
11 changes: 11 additions & 0 deletions
11
instrumentation/servlet/servlet-3.0/library/build.gradle.kts
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,11 @@ | ||
| plugins { | ||
| id("otel.library-instrumentation") | ||
| } | ||
|
|
||
| dependencies { | ||
| library("javax.servlet:javax.servlet-api:3.0.1") | ||
|
|
||
| // FIXME: These dependencies need to be shadowed into the library. | ||
| library(project(":instrumentation:servlet:servlet-3.0:javaagent")) | ||
| library(project(":instrumentation:servlet:servlet-common:javaagent")) | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
...a/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/OpenTelemetryServletFilter.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,69 @@ | ||
| 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.instrumentation.servlet.ServletRequestContext; | ||
| import java.io.IOException; | ||
| import javax.servlet.Filter; | ||
| import javax.servlet.FilterChain; | ||
| import javax.servlet.FilterConfig; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.ServletRequest; | ||
| import javax.servlet.ServletResponse; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| /** | ||
| * OpenTelemetry Library instrumentation for Java Servlet based applications that can't use a Java | ||
| * Agent. Due to inherit limitations in the servlet filter API, instrumenting at the filter level | ||
| * will miss anything that happens earlier in the filter stack or problems handled directly by the | ||
| * app server. For this reason, Java Agent instrumentation is preferred when possible. | ||
| */ | ||
| public class OpenTelemetryServletFilter implements Filter { | ||
|
|
||
| @Override | ||
| public void init(FilterConfig filterConfig) throws ServletException {} | ||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
| throws IOException, ServletException { | ||
| // Only HttpServlets are supported. | ||
| if (!(request instanceof HttpServletRequest && response instanceof HttpServletResponse)) { | ||
| chain.doFilter(request, response); | ||
| return; | ||
| } | ||
|
|
||
| HttpServletRequest httpRequest = (HttpServletRequest) request; | ||
| HttpServletResponse httpResponse = (HttpServletResponse) response; | ||
| ServletRequestContext<HttpServletRequest> requestContext = | ||
| new ServletRequestContext<>(httpRequest, this); | ||
|
|
||
| // Bail if we shouldn't start a new span. | ||
| if (!helper().shouldStart(Context.current(), requestContext)) { | ||
| chain.doFilter(request, response); | ||
| return; | ||
| } | ||
|
|
||
| Context spanContext = helper().start(Context.current(), requestContext); | ||
| helper().setAsyncListenerResponse(spanContext, (HttpServletResponse) response); | ||
|
|
||
| // Not using try-with-resources to match the api usage of Servlet3Advice. | ||
| // (helper().end is responsible for closing the scope.) | ||
| Scope scope = spanContext.makeCurrent(); | ||
| Throwable throwable = null; | ||
| try { | ||
| chain.doFilter( | ||
| new OtelHttpServletRequest((HttpServletRequest) request), response); | ||
| } catch (Throwable e) { | ||
| throwable = e; | ||
| throw e; | ||
| } finally { | ||
| helper().end(requestContext, httpRequest, httpResponse, throwable, true, spanContext, scope); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() {} | ||
| } |
85 changes: 85 additions & 0 deletions
85
...c/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/OtelAsyncContext.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,85 @@ | ||
| 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.AsyncListener; | ||
| import javax.servlet.ServletContext; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.ServletRequest; | ||
| import javax.servlet.ServletResponse; | ||
|
|
||
| /// Delegates all methods except [#start(Runnable) which wraps the [Runnable]. | ||
| public class OtelAsyncContext implements AsyncContext { | ||
| private final AsyncContext delegate; | ||
|
|
||
| public OtelAsyncContext(AsyncContext delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public ServletRequest getRequest() { | ||
| return delegate.getRequest(); | ||
| } | ||
|
|
||
| @Override | ||
| public ServletResponse getResponse() { | ||
| return delegate.getResponse(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasOriginalRequestAndResponse() { | ||
| return delegate.hasOriginalRequestAndResponse(); | ||
| } | ||
|
|
||
| @Override | ||
| public void dispatch() { | ||
| delegate.dispatch(); | ||
| } | ||
|
|
||
| @Override | ||
| public void dispatch(String path) { | ||
| delegate.dispatch(path); | ||
| } | ||
|
|
||
| @Override | ||
| public void dispatch(ServletContext context, String path) { | ||
| delegate.dispatch(context, path); | ||
| } | ||
|
|
||
| @Override | ||
| public void complete() { | ||
| delegate.complete(); | ||
| } | ||
|
|
||
| @Override | ||
| public void start(Runnable run) { | ||
| delegate.start(helper().wrapAsyncRunnable(run)); | ||
| } | ||
|
|
||
| @Override | ||
| public void addListener(AsyncListener listener) { | ||
| delegate.addListener(listener); | ||
| } | ||
|
|
||
| @Override | ||
| public void addListener( | ||
| AsyncListener listener, ServletRequest servletRequest, ServletResponse servletResponse) { | ||
| delegate.addListener(listener, servletRequest, servletResponse); | ||
| } | ||
|
|
||
| @Override | ||
| public <T extends AsyncListener> T createListener(Class<T> clazz) throws ServletException { | ||
| return delegate.createListener(clazz); | ||
| } | ||
|
|
||
| @Override | ||
| public void setTimeout(long timeout) { | ||
| delegate.setTimeout(timeout); | ||
| } | ||
|
|
||
| @Override | ||
| public long getTimeout() { | ||
| return delegate.getTimeout(); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
.../java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/OtelHttpServletRequest.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,43 @@ | ||
| 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 javax.servlet.AsyncContext; | ||
| import javax.servlet.ServletRequest; | ||
| import javax.servlet.ServletResponse; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletRequestWrapper; | ||
|
|
||
| /// Wrapper around [HttpServletRequest] that attaches an async listener if [#startAsync()] is | ||
| /// invoked and a wrapper around [#getAsyncContext()] to capture exceptions from async [Runnable]s. | ||
| public class OtelHttpServletRequest extends HttpServletRequestWrapper { | ||
|
|
||
| public OtelHttpServletRequest(HttpServletRequest request) { | ||
| super(request); | ||
| } | ||
|
|
||
| @Override | ||
| public AsyncContext getAsyncContext() { | ||
| return new OtelAsyncContext(super.getAsyncContext()); | ||
| } | ||
|
|
||
| @Override | ||
| public AsyncContext startAsync() throws IllegalStateException { | ||
| try { | ||
| return super.startAsync(); | ||
| } finally { | ||
| helper().attachAsyncListener(this, Context.current()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) | ||
| throws IllegalStateException { | ||
| try { | ||
| return super.startAsync(servletRequest, servletResponse); | ||
| } finally { | ||
| helper().attachAsyncListener(this, Context.current()); | ||
| } | ||
| } | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.