-
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 all commits
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
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
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,52 @@ | ||
| # Library Instrumentation for Java Servlet version 3.0 and higher | ||
|
|
||
| Provides OpenTelemetry instrumentation for Java Servlets through a servlet filter. | ||
|
|
||
| ## Quickstart | ||
|
|
||
| ### Add these dependencies to your project | ||
|
|
||
| Replace `OPENTELEMETRY_VERSION` with | ||
| the [latest release](https://central.sonatype.com/artifact/io.opentelemetry.instrumentation/opentelemetry-lettuce-5.1). | ||
|
|
||
| For Maven, add to your `pom.xml` dependencies: | ||
|
|
||
| ```xml | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.opentelemetry.instrumentation</groupId> | ||
| <artifactId>opentelemetry-servlet-3.0</artifactId> | ||
| <version>OPENTELEMETRY_VERSION</version> | ||
| </dependency> | ||
| </dependencies> | ||
| ``` | ||
|
|
||
| For Gradle, add to your dependencies: | ||
|
|
||
| ```kotlin | ||
| implementation("io.opentelemetry.instrumentation:opentelemetry-servlet-3.0:OPENTELEMETRY_VERSION") | ||
| ``` | ||
|
|
||
| ### Usage | ||
|
|
||
| Add the filter to your `web.xml` | ||
|
|
||
| ```xml | ||
| <web-app> | ||
| <filter> | ||
| <filter-name>OpenTelemetryServletFilter</filter-name> | ||
| <filter-class>io.opentelemetry.instrumentation.servlet.v3_0.OpenTelemetryServletFilter | ||
| </filter-class> | ||
| <async-supported>true</async-supported> | ||
| </filter> | ||
| <filter-mapping> | ||
| <filter-name>OpenTelemetryServletFilter</filter-name> | ||
| <url-pattern>/*</url-pattern> | ||
| </filter-mapping> | ||
| </web-app> | ||
| ``` | ||
|
|
||
| Note: GlobalOpenTelemetry must be set before filter initialization. If you are unable to ensure it | ||
| is set first, consider creating a subclass of `OpenTelemetryServletFilter` that handles | ||
| initialization of GlobalOpenTelemetry in a static initializer or constructor. |
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
| plugins { | ||
| id("otel.library-instrumentation") | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly("javax.servlet:javax.servlet-api:3.0.1") | ||
|
|
||
| testLibrary("org.eclipse.jetty:jetty-server:8.0.0.v20110901") | ||
| testLibrary("org.eclipse.jetty:jetty-servlet:8.0.0.v20110901") | ||
| testLibrary("org.apache.tomcat.embed:tomcat-embed-core:8.0.41") | ||
| testLibrary("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.41") | ||
|
|
||
| latestDepTestLibrary("org.eclipse.jetty:jetty-server:10.+") // see servlet-5.0 module | ||
| latestDepTestLibrary("org.eclipse.jetty:jetty-servlet:10.+") // see servlet-5.0 module | ||
|
|
||
| latestDepTestLibrary("org.apache.tomcat.embed:tomcat-embed-core:9.+") // see servlet-5.0 module | ||
| latestDepTestLibrary("org.apache.tomcat.embed:tomcat-embed-jasper:9.+") // see servlet-5.0 module | ||
| } | ||
|
|
||
| tasks { | ||
| withType<Test>().configureEach { | ||
| // required on jdk17+ to allow tomcat to shutdown properly. | ||
| jvmArgs("--add-opens=java.base/java.util=ALL-UNNAMED") | ||
| jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") | ||
| } | ||
| } | ||
|
|
||
| // Servlet 3.0 in latest Jetty versions requires Java 11 | ||
| // However, projects that depend on this module are still be using Java 8 in testLatestDeps mode | ||
| // Therefore, we need a separate project for servlet 3.0 tests | ||
| val latestDepTest = findProperty("testLatestDeps") as Boolean | ||
|
|
||
| if (latestDepTest) { | ||
| otelJava { | ||
| minJavaVersionSupported.set(JavaVersion.VERSION_11) | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
...c/main/java/io/opentelemetry/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,67 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.servlet.v3_0; | ||
|
|
||
| import static io.opentelemetry.instrumentation.servlet.v3_0.copied.Servlet3Singletons.FILTER_MAPPING_RESOLVER; | ||
|
|
||
| import io.opentelemetry.instrumentation.servlet.v3_0.copied.CallDepth; | ||
| import io.opentelemetry.instrumentation.servlet.v3_0.copied.Servlet3FilterMappingResolverFactory; | ||
| import io.opentelemetry.instrumentation.servlet.v3_0.copied.Servlet3RequestAdviceScope; | ||
| 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.annotation.WebFilter; | ||
| 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. | ||
| */ | ||
| @WebFilter("/*") | ||
| public class OpenTelemetryServletFilter implements Filter { | ||
|
|
||
| @Override | ||
| public void init(FilterConfig filterConfig) { | ||
| FILTER_MAPPING_RESOLVER.set(this, new Servlet3FilterMappingResolverFactory(filterConfig)); | ||
| } | ||
|
|
||
| @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; | ||
|
|
||
| Throwable throwable = null; | ||
| Servlet3RequestAdviceScope adviceScope = | ||
| new Servlet3RequestAdviceScope( | ||
| CallDepth.forClass(OpenTelemetryServletFilter.class), httpRequest, httpResponse, this); | ||
|
Member
Author
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. I tried to keep the instrumentation as similar as possible to the javaagent instrumentation. |
||
| try { | ||
| chain.doFilter( | ||
| new OtelHttpServletRequest(httpRequest), new OtelHttpServletResponse(httpResponse)); | ||
| } catch (Throwable e) { | ||
| throwable = e; | ||
| throw e; | ||
| } finally { | ||
| adviceScope.exit(throwable, httpRequest, httpResponse); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() {} | ||
| } | ||
90 changes: 90 additions & 0 deletions
90
...library/src/main/java/io/opentelemetry/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,90 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.servlet.v3_0; | ||
|
|
||
| import static io.opentelemetry.instrumentation.servlet.v3_0.copied.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(); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
...y/src/main/java/io/opentelemetry/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,47 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.servlet.v3_0; | ||
|
|
||
| import static io.opentelemetry.instrumentation.servlet.v3_0.copied.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() { | ||
| try { | ||
| return new OtelAsyncContext(super.startAsync()); | ||
| } finally { | ||
| helper().attachAsyncListener(this, Context.current()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) { | ||
| try { | ||
| return new OtelAsyncContext(super.startAsync(servletRequest, servletResponse)); | ||
| } finally { | ||
| helper().attachAsyncListener(this, Context.current()); | ||
| } | ||
| } | ||
| } |
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.