Skip to content
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

Merged
merged 25 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,13 @@
<version>${helidon.version}</version>
</dependency>

<!-- MP Telemetry -->
<dependency>
<groupId>io.helidon.microprofile.telemetry</groupId>
<artifactId>helidon-microprofile-telemetry</artifactId>
<version>${helidon.version}</version>
</dependency>

<!-- REST Client -->
<dependency>
<groupId>io.helidon.microprofile.rest-client</groupId>
Expand Down
39 changes: 39 additions & 0 deletions dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<version.lib.microprofile-reactive-streams-operators-api>3.0</version.lib.microprofile-reactive-streams-operators-api>
<version.lib.microprofile-reactive-streams-operators-core>3.0</version.lib.microprofile-reactive-streams-operators-core>
<version.lib.microprofile-rest-client>3.0</version.lib.microprofile-rest-client>
<version.lib.microprofile-telemetry-tck>1.0</version.lib.microprofile-telemetry-tck>
<version.lib.microprofile-tracing>3.0</version.lib.microprofile-tracing>
<version.lib.microprofile-lra-api>2.0-RC1</version.lib.microprofile-lra-api>
<version.lib.microstream>05.00.02-MS-GA</version.lib.microstream>
Expand All @@ -133,7 +134,10 @@
<version.lib.okio>1.17.5</version.lib.okio>
<version.lib.opentelemetry>1.22.0</version.lib.opentelemetry>
<version.lib.opentelemetry.semconv>1.22.0-alpha</version.lib.opentelemetry.semconv>
<version.lib.opentelemetry-sdk-extension-autoconfigure>1.22.0-alpha</version.lib.opentelemetry-sdk-extension-autoconfigure>
<version.lib.opentelemetry.opentracing.shim>1.22.0-alpha</version.lib.opentelemetry.opentracing.shim>
<version.lib.opentelemetry>1.22.0</version.lib.opentelemetry>
<version.lib.opentelemetry-sdk-extension-autoconfigure>1.22.0-alpha</version.lib.opentelemetry-sdk-extension-autoconfigure>
<version.lib.opentracing>0.33.0</version.lib.opentracing>
<version.lib.opentracing.grpc>0.2.1</version.lib.opentracing.grpc>
<version.lib.opentracing.tracerresolver>0.1.8</version.lib.opentracing.tracerresolver>
Expand Down Expand Up @@ -250,6 +254,36 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
<version>${version.lib.opentelemetry}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-context</artifactId>
<version>${version.lib.opentelemetry}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
<version>${version.lib.opentelemetry}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-api</artifactId>
<version>${version.lib.opentelemetry}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
<version>${version.lib.opentelemetry}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
<version>${version.lib.opentelemetry-sdk-extension-autoconfigure}</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
Expand Down Expand Up @@ -1375,6 +1409,11 @@
<artifactId>microprofile-lra-tck</artifactId>
<version>${version.lib.microprofile-lra-api}</version>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.telemetry.tracing</groupId>
<artifactId>microprofile-telemetry-tracing-tck</artifactId>
<version>${version.lib.microprofile-telemetry-tck}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
1 change: 1 addition & 0 deletions microprofile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
<module>lra</module>
<module>bean-validation</module>
<module>service-common</module>
<module>telemetry</module>
</modules>
</project>
120 changes: 120 additions & 0 deletions microprofile/telemetry/pom.xml
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";
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

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();
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create constants out of the strings such as CONFIG_STRING + "scope". Also CONFIG_STRING is not correct name here. It has nothing to do with config.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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");
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}

}
Loading