-
Notifications
You must be signed in to change notification settings - Fork 580
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
[4.x] - MicroProfile Telemetry Support #6493
Changes from 20 commits
77b139e
63d4a18
ca0e525
de223d6
a4dcd5b
0777c7c
2bb1c1e
c7a9fbe
d3d109f
1fa8d19
1c25d61
09c2db1
c78cbe8
21fb7d4
b6ce874
6148d0f
304f3e5
c9b3fa0
0184110
63fbd1f
1d4b1d1
4581ca3
a502f0b
00cd9f6
30432ed
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2023 Oracle and/or its affiliates. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>helidon-microprofile-project</artifactId> | ||
<groupId>io.helidon.microprofile</groupId> | ||
<version>4.0.0-SNAPSHOT</version> | ||
</parent> | ||
<groupId>io.helidon.microprofile.telemetry</groupId> | ||
<artifactId>helidon-microprofile-telemetry</artifactId> | ||
<name>Helidon Microprofile Telemetry</name> | ||
|
||
<description> | ||
Support for MicroProfile Telemetry | ||
</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.helidon.tracing</groupId> | ||
<artifactId>helidon-tracing-opentelemetry</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.opentelemetry</groupId> | ||
<artifactId>opentelemetry-sdk</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.opentelemetry.instrumentation</groupId> | ||
<artifactId>opentelemetry-instrumentation-annotations</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.opentelemetry</groupId> | ||
<artifactId>opentelemetry-context</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.opentelemetry</groupId> | ||
<artifactId>opentelemetry-exporter-otlp</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.opentelemetry</groupId> | ||
<artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.opentelemetry.instrumentation</groupId> | ||
<artifactId>opentelemetry-instrumentation-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.microprofile.server</groupId> | ||
<artifactId>helidon-microprofile-server</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.microprofile.config</groupId> | ||
<artifactId>helidon-microprofile-config</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.jersey</groupId> | ||
<artifactId>helidon-jersey-common</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.enterprise</groupId> | ||
<artifactId>jakarta.enterprise.cdi-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.interceptor</groupId> | ||
<artifactId>jakarta.interceptor-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.common.features</groupId> | ||
<artifactId>helidon-common-features-api</artifactId> | ||
<scope>provided</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<!-- Jersey on java 9 --> | ||
<groupId>jakarta.activation</groupId> | ||
<artifactId>jakarta.activation-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<compilerArgs> | ||
<compilerArg>--enable-preview</compilerArg> | ||
</compilerArgs> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>io.helidon.common.features</groupId> | ||
<artifactId>helidon-common-features-processor</artifactId> | ||
<version>${helidon.version}</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright (c) 2023 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.microprofile.telemetry; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.api.trace.Tracer; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.context.propagation.TextMapSetter; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.client.ClientRequestContext; | ||
import jakarta.ws.rs.client.ClientRequestFilter; | ||
import jakarta.ws.rs.client.ClientResponseContext; | ||
import jakarta.ws.rs.client.ClientResponseFilter; | ||
import jakarta.ws.rs.ext.Provider; | ||
|
||
import static java.net.HttpURLConnection.HTTP_OK; | ||
|
||
/** | ||
* Filter to process Client request and Client response. Starts a new {@link io.opentelemetry.api.trace.Span} on request and | ||
* ends it on a Response. | ||
*/ | ||
@Provider | ||
class HelidonTelemetryClientFilter implements ClientRequestFilter, ClientResponseFilter { | ||
private static final System.Logger LOGGER = System.getLogger(HelidonTelemetryContainerFilter.class.getName()); | ||
private static final String HTTP_STATUS_CODE = "http.status_code"; | ||
private static final String HTTP_METHOD = "http.method"; | ||
private static final String HTTP_SCHEME = "http.scheme"; | ||
private static final String HTTP_URL = "http.url"; | ||
private static final String CONFIG_STRING = "otel.span.client."; | ||
|
||
// Extract the current OpenTelemetry Context. Required for a parent/child relationship | ||
// to be correctly rebuilt in the next filter. | ||
private static final TextMapSetter<ClientRequestContext> CONTEXT_HEADER_EXTRACTOR = | ||
(carrier, key, value) -> carrier.getHeaders().add(key, value); | ||
|
||
private final Tracer tracer; | ||
private final OpenTelemetry openTelemetry; | ||
|
||
@Inject | ||
HelidonTelemetryClientFilter(Tracer tracer, OpenTelemetry openTelemetry) { | ||
this.tracer = tracer; | ||
this.openTelemetry = openTelemetry; | ||
} | ||
|
||
|
||
@Override | ||
public void filter(ClientRequestContext clientRequestContext) { | ||
|
||
if (LOGGER.isLoggable(System.Logger.Level.TRACE)) { | ||
LOGGER.log(System.Logger.Level.TRACE, "Starting Span in a Client Request"); | ||
} | ||
|
||
Context parentContext = Context.current(); | ||
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. Since this is not used on other places, the only usage could be replaced with Context.current() directly. This would make sense only it it was used in other places. But since on all places in this method, where Context is used, Context.current() is used instead, this is redundant. 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. fixed |
||
|
||
//Start new span for Client request. | ||
Span span = tracer.spanBuilder("HTTP " + clientRequestContext.getMethod()) | ||
.setParent(parentContext) | ||
.setSpanKind(SpanKind.CLIENT) | ||
.setAttribute(HTTP_STATUS_CODE, HTTP_OK) | ||
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. This is redundant to set, since you already set it in response filter. 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. ok |
||
.setAttribute(HTTP_METHOD, clientRequestContext.getMethod()) | ||
.setAttribute(HTTP_SCHEME, clientRequestContext.getUri().getScheme()) | ||
.setAttribute(HTTP_URL, clientRequestContext.getUri().toString()) | ||
.startSpan(); | ||
|
||
Scope scope = span.makeCurrent(); | ||
clientRequestContext.setProperty(CONFIG_STRING + "scope", scope); | ||
clientRequestContext.setProperty(CONFIG_STRING + "span", span); | ||
clientRequestContext.setProperty(CONFIG_STRING + "context", Context.current()); | ||
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. Create constants out of the strings such as 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. ok |
||
|
||
// Propagate OpenTelemetry context to next filter | ||
openTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), clientRequestContext, | ||
CONTEXT_HEADER_EXTRACTOR); | ||
} | ||
|
||
|
||
@Override | ||
public void filter(ClientRequestContext clientRequestContext, ClientResponseContext clientResponseContext) { | ||
|
||
if (LOGGER.isLoggable(System.Logger.Level.TRACE)) { | ||
LOGGER.log(System.Logger.Level.TRACE, "Closing Span in a Client Response"); | ||
} | ||
|
||
// End span for Client request. | ||
Context context = (Context) clientRequestContext.getProperty(CONFIG_STRING + "context"); | ||
if (context == null) { | ||
return; | ||
} | ||
|
||
Span span = (Span) clientRequestContext.getProperty(CONFIG_STRING + "span"); | ||
span.setAttribute(HTTP_STATUS_CODE, clientResponseContext.getStatus()); | ||
span.end(); | ||
|
||
Scope scope = (Scope) clientRequestContext.getProperty(CONFIG_STRING + "scope"); | ||
scope.close(); | ||
|
||
clientRequestContext.removeProperty(CONFIG_STRING + "context"); | ||
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 am not sure if it is needed, but since you are removing context, should you not remove also other registered components such as span or scope? 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. ok |
||
} | ||
|
||
} |
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.
These constants are also created in
HelidonTelemetryContainerFilter
.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.
ok