-
Notifications
You must be signed in to change notification settings - Fork 1k
Add jdk.httpserver instrumentation
#13243
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
Merged
Merged
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
76dbc9c
start
SentryMan 0c28cba
start
SentryMan 874c553
Update AbstractJdkHttpServerTest.java
SentryMan 8651b06
agent
SentryMan d0d35eb
spotless
SentryMan 60c3382
Update AbstractJdkHttpServerTest.java
SentryMan cd1e121
Update AbstractJdkHttpServerTest.java
SentryMan 2f19961
span filter
SentryMan e386c06
Update AbstractJdkHttpServerTest.java
SentryMan 1433ddc
Update AbstractJdkHttpServerTest.java
SentryMan 60b29fb
Update AbstractJdkHttpServerTest.java
SentryMan 959b667
address comments
SentryMan 578e275
address more comments
SentryMan 43a78c2
http pipeline
SentryMan 8ee019f
add executor
SentryMan 43cc1cf
Update supported-libraries.md
SentryMan 2332175
try future stop
SentryMan d2698c0
Update AbstractJdkHttpServerTest.java
SentryMan fd3662a
Update AbstractJdkHttpServerTest.java
SentryMan 15383a8
Update AbstractJdkHttpServerTest.java
SentryMan 10b3679
Create README.md
SentryMan 85c3cf2
Merge branch 'main' into jdk.httpserver
laurit 46cb20f
update fossa configuration
laurit 6487593
rename classes
laurit ec176d8
fix jdk8
laurit 373abd7
modify tests a bit, add a method that configures tracing for HttpContext
laurit dd43a41
spotless
laurit a46b078
reformat table
laurit f6b556a
enable response customizer test
laurit a42e07e
package change
SentryMan f78ec08
static imports
SentryMan ec93763
Merge branch 'main' into jdk.httpserver
laurit 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
14 changes: 14 additions & 0 deletions
14
instrumentation/java-http-server/javaagent/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,14 @@ | ||
| plugins { | ||
| id("otel.javaagent-instrumentation") | ||
| } | ||
|
|
||
| muzzle { | ||
| pass { | ||
| coreJdk() | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(project(":instrumentation:java-http-server:library")) | ||
| testImplementation(project(":instrumentation:java-http-server:testing")) | ||
| } |
42 changes: 42 additions & 0 deletions
42
.../io/opentelemetry/javaagent/instrumentation/javahttpserver/HttpServerInstrumentation.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,42 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.javahttpserver; | ||
|
|
||
| import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
|
||
| import com.sun.net.httpserver.HttpContext; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
|
|
||
| public class HttpServerInstrumentation implements TypeInstrumentation { | ||
|
|
||
| @Override | ||
| public ElementMatcher<TypeDescription> typeMatcher() { | ||
| return extendsClass(named("com.sun.net.httpserver.HttpServer")); | ||
| } | ||
|
|
||
| @Override | ||
| public void transform(TypeTransformer transformer) { | ||
| transformer.applyAdviceToMethod( | ||
| isMethod().and(isPublic()).and(named("createContext")), | ||
| HttpServerInstrumentation.class.getName() + "$BuildAdvice"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public static class BuildAdvice { | ||
|
|
||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void onExit(@Advice.Return HttpContext httpContext) { | ||
| httpContext.getFilters().addAll(JavaHttpServerSingletons.FILTERS); | ||
| } | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
...lemetry/javaagent/instrumentation/javahttpserver/JavaHttpServerInstrumentationModule.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,24 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.javahttpserver; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| @AutoService(InstrumentationModule.class) | ||
| public class JavaHttpServerInstrumentationModule extends InstrumentationModule { | ||
| public JavaHttpServerInstrumentationModule() { | ||
| super("java-http-server"); | ||
| } | ||
|
|
||
| @Override | ||
| public List<TypeInstrumentation> typeInstrumentations() { | ||
| return Collections.singletonList(new HttpServerInstrumentation()); | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
...opentelemetry/javaagent/instrumentation/javahttpserver/JavaHttpServerResponseMutator.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,18 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.javahttpserver; | ||
|
|
||
| import com.sun.net.httpserver.Headers; | ||
| import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseMutator; | ||
|
|
||
| enum JavaHttpServerResponseMutator implements HttpServerResponseMutator<Headers> { | ||
| INSTANCE; | ||
|
|
||
| @Override | ||
| public void appendHeader(Headers response, String name, String value) { | ||
| response.add(name, value); | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
...a/io/opentelemetry/javaagent/instrumentation/javahttpserver/JavaHttpServerSingletons.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,36 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.javahttpserver; | ||
|
|
||
| import com.sun.net.httpserver.Filter; | ||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.instrumentation.api.incubator.config.internal.CommonConfig; | ||
| import io.opentelemetry.instrumentation.javahttpserver.JavaHttpServerTelemetry; | ||
| import io.opentelemetry.instrumentation.javahttpserver.JavaHttpServerTelemetryBuilder; | ||
| import io.opentelemetry.instrumentation.javahttpserver.internal.JavaHttpServerInstrumenterBuilderUtil; | ||
| import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public final class JavaHttpServerSingletons { | ||
|
|
||
| public static final List<Filter> FILTERS; | ||
|
|
||
| static { | ||
| CommonConfig config = AgentCommonConfig.get(); | ||
|
|
||
| JavaHttpServerTelemetryBuilder serverBuilder = | ||
| JavaHttpServerTelemetry.builder(GlobalOpenTelemetry.get()); | ||
| JavaHttpServerInstrumenterBuilderUtil.getServerBuilderExtractor() | ||
| .apply(serverBuilder) | ||
| .configure(config); | ||
| JavaHttpServerTelemetry serverTelemetry = serverBuilder.build(); | ||
|
|
||
| FILTERS = Arrays.asList(serverTelemetry.newFilter(), new ResponseCustomizingFilter()); | ||
| } | ||
|
|
||
| private JavaHttpServerSingletons() {} | ||
| } |
30 changes: 30 additions & 0 deletions
30
.../io/opentelemetry/javaagent/instrumentation/javahttpserver/ResponseCustomizingFilter.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,30 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.javahttpserver; | ||
|
|
||
| import com.sun.net.httpserver.Filter; | ||
| import com.sun.net.httpserver.HttpExchange; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder; | ||
| import java.io.IOException; | ||
|
|
||
| class ResponseCustomizingFilter extends Filter { | ||
SentryMan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ResponseCustomizingFilter() {} | ||
|
|
||
| @Override | ||
| public void doFilter(HttpExchange exchange, Chain chain) throws IOException { | ||
| Context context = Context.current(); | ||
| HttpServerResponseCustomizerHolder.getCustomizer() | ||
| .customize(context, exchange.getResponseHeaders(), JavaHttpServerResponseMutator.INSTANCE); | ||
| chain.doFilter(exchange); | ||
| } | ||
|
|
||
| @Override | ||
| public String description() { | ||
| return "OpenTelemetry response customizing filter"; | ||
| } | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
...st/java/io/opentelemetry/javaagent/instrumentation/javahttpserver/JavaHttpServerTest.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,25 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.javahttpserver; | ||
|
|
||
| import io.opentelemetry.instrumentation.javahttpserver.AbstractJavaHttpServerTest; | ||
| import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
| import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension; | ||
| import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| class JavaHttpServerTest extends AbstractJavaHttpServerTest { | ||
|
|
||
| @RegisterExtension | ||
| static final InstrumentationExtension testing = HttpServerInstrumentationExtension.forAgent(); | ||
|
|
||
| @Override | ||
| protected void configure(HttpServerTestOptions options) { | ||
| super.configure(options); | ||
|
|
||
| options.setHasResponseCustomizer(serverEndpoint -> true); | ||
| } | ||
| } |
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,63 @@ | ||
| # Library Instrumentation for Java HTTP Server | ||
|
|
||
| Provides OpenTelemetry instrumentation for [Java HTTP Server](https://docs.oracle.com/en/java/javase/21/docs/api/jdk.httpserver/module-summary.html). | ||
|
|
||
| ## Quickstart | ||
|
|
||
| ### Add these dependencies to your project | ||
|
|
||
| Replace `OPENTELEMETRY_VERSION` with the [latest | ||
| release](https://search.maven.org/search?q=g:io.opentelemetry.instrumentation%20AND%20a:opentelemetry-java-http-server). | ||
|
|
||
| For Maven, add to your `pom.xml` dependencies: | ||
|
|
||
| ```xml | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.opentelemetry.instrumentation</groupId> | ||
| <artifactId>opentelemetry-java-http-server</artifactId> | ||
| <version>OPENTELEMETRY_VERSION</version> | ||
| </dependency> | ||
| </dependencies> | ||
| ``` | ||
|
|
||
| For Gradle, add to your dependencies: | ||
|
|
||
| ```groovy | ||
| implementation("io.opentelemetry.instrumentation:opentelemetry-java-http-server:OPENTELEMETRY_VERSION") | ||
| ``` | ||
|
|
||
| ### Usage | ||
|
|
||
| The instrumentation library contains a `Filter` wrapper that provides OpenTelemetry-based spans | ||
| and context propagation. | ||
|
|
||
| ```java | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.InetSocketAddress; | ||
|
|
||
| import com.sun.net.httpserver.HttpContext; | ||
| import com.sun.net.httpserver.HttpServer; | ||
|
|
||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
|
|
||
| public class Application { | ||
|
|
||
| static void main(String args) throws IOException { | ||
|
|
||
| final HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); | ||
| final HttpContext context = | ||
| server.createContext( | ||
| "/", | ||
| ctx -> { | ||
| // http logic | ||
| }); | ||
|
|
||
| OpenTelemetry openTelemetry = //... | ||
|
|
||
| JavaHttpServerTelemetry.create(openTelemetry).configure(context); | ||
| } | ||
| } | ||
| ``` |
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,8 @@ | ||
| plugins { | ||
| id("otel.library-instrumentation") | ||
| id("otel.nullaway-conventions") | ||
| } | ||
|
|
||
| dependencies { | ||
| testImplementation(project(":instrumentation:java-http-server:testing")) | ||
| } |
87 changes: 87 additions & 0 deletions
87
.../java/io/opentelemetry/instrumentation/javahttpserver/JavaHttpServerAttributesGetter.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,87 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.javahttpserver; | ||
|
|
||
| import com.sun.net.httpserver.HttpExchange; | ||
| import com.sun.net.httpserver.HttpsExchange; | ||
| import io.opentelemetry.instrumentation.api.internal.HttpProtocolUtil; | ||
| import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesGetter; | ||
| import java.net.InetSocketAddress; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| enum JavaHttpServerAttributesGetter | ||
| implements HttpServerAttributesGetter<HttpExchange, HttpExchange> { | ||
| INSTANCE; | ||
|
|
||
| @Override | ||
| public String getHttpRequestMethod(HttpExchange exchange) { | ||
| return exchange.getRequestMethod(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getUrlScheme(HttpExchange exchange) { | ||
| return exchange instanceof HttpsExchange ? "https" : "http"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getUrlPath(HttpExchange exchange) { | ||
| return exchange.getRequestURI().getPath(); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String getUrlQuery(HttpExchange exchange) { | ||
| return exchange.getRequestURI().getQuery(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getHttpRequestHeader(HttpExchange exchange, String name) { | ||
| return exchange.getRequestHeaders().getOrDefault(name, Collections.emptyList()); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public Integer getHttpResponseStatusCode( | ||
| HttpExchange exchange, @Nullable HttpExchange res, @Nullable Throwable error) { | ||
| int status = exchange.getResponseCode(); | ||
| return status != -1 ? status : null; | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getHttpResponseHeader( | ||
| HttpExchange exchange, @Nullable HttpExchange res, String name) { | ||
| return exchange.getResponseHeaders().getOrDefault(name, Collections.emptyList()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getHttpRoute(HttpExchange exchange) { | ||
| return exchange.getHttpContext().getPath(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getNetworkProtocolName(HttpExchange exchange, @Nullable HttpExchange res) { | ||
| return HttpProtocolUtil.getProtocol(exchange.getProtocol()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getNetworkProtocolVersion(HttpExchange exchange, @Nullable HttpExchange res) { | ||
| return HttpProtocolUtil.getVersion(exchange.getProtocol()); | ||
| } | ||
|
|
||
| @Override | ||
| public InetSocketAddress getNetworkPeerInetSocketAddress( | ||
| HttpExchange exchange, @Nullable HttpExchange res) { | ||
| return exchange.getRemoteAddress(); | ||
| } | ||
|
|
||
| @Override | ||
| public InetSocketAddress getNetworkLocalInetSocketAddress( | ||
| HttpExchange exchange, @Nullable HttpExchange res) { | ||
| return exchange.getLocalAddress(); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.