-
Notifications
You must be signed in to change notification settings - Fork 24
Initial W3C Traceparent and otel integration. #1800
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
15c3f5c
Move to structured logging, with logback, for docker image, testing, …
MikeNeilson 0d924c0
Initial OTEL span setup.
MikeNeilson 67660cf
Additional attempts to see the trace id.
MikeNeilson 05b88d9
client provided trace id propagates.
MikeNeilson 7fcb100
Rename filter, validate use input.
MikeNeilson 131d57c
add client usage to at least one test.
MikeNeilson 050f2b5
Corrections from PR feedback.
MikeNeilson 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
34 changes: 34 additions & 0 deletions
34
cwms-data-api/src/main/java/cwms/cda/OpenTelemetrySetup.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,34 @@ | ||
| package cwms.cda; | ||
|
|
||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
| import io.opentelemetry.context.propagation.ContextPropagators; | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
|
|
||
| public final class OpenTelemetrySetup { | ||
| private OpenTelemetrySetup() { | ||
| /* This utility class should not be instantiated */ | ||
| } | ||
|
|
||
| /** | ||
| * Initializes the OpenTelemetry SDK with a logging span exporter and the W3C Trace Context | ||
| * propagator. | ||
| * | ||
| * @return A ready-to-use {@link OpenTelemetry} instance. | ||
| */ | ||
| @SuppressWarnings("null") // nothing here can be null without other exceptions getting thrown. | ||
| public static void initTelemetry() { | ||
| SdkTracerProvider sdkTracerProvider = | ||
| SdkTracerProvider.builder() | ||
| .build(); | ||
|
|
||
| OpenTelemetrySdk.builder() | ||
| .setTracerProvider(sdkTracerProvider) | ||
| .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())) | ||
| .buildAndRegisterGlobal(); | ||
| Runtime.getRuntime().addShutdownHook(new Thread(sdkTracerProvider::close)); | ||
|
|
||
| } | ||
|
|
||
| } |
33 changes: 33 additions & 0 deletions
33
cwms-data-api/src/main/java/cwms/cda/servlet/InitListener.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,33 @@ | ||
| package cwms.cda.servlet; | ||
|
|
||
| import javax.servlet.ServletContextEvent; | ||
| import javax.servlet.ServletContextListener; | ||
| import javax.servlet.annotation.WebListener; | ||
|
|
||
| import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
| import io.opentelemetry.context.propagation.ContextPropagators; | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
|
|
||
| @WebListener | ||
| public class InitListener implements ServletContextListener { | ||
|
|
||
| private static final SdkTracerProvider sdkTracerProvider = | ||
| SdkTracerProvider.builder() | ||
| .build(); | ||
|
|
||
| @Override | ||
| public void contextInitialized(ServletContextEvent sce) { | ||
|
|
||
|
|
||
| OpenTelemetrySdk.builder() | ||
| .setTracerProvider(sdkTracerProvider) | ||
| .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())) | ||
| .buildAndRegisterGlobal(); | ||
| } | ||
|
|
||
| @Override | ||
| public void contextDestroyed(ServletContextEvent sce) { | ||
| sdkTracerProvider.close(); | ||
| } | ||
| } |
85 changes: 85 additions & 0 deletions
85
cwms-data-api/src/main/java/cwms/cda/servlet/W3CTraceFilter.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 cwms.cda.servlet; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
| import javax.servlet.Filter; | ||
| import javax.servlet.FilterChain; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.ServletRequest; | ||
| import javax.servlet.ServletResponse; | ||
| import javax.servlet.annotation.WebFilter; | ||
| import javax.servlet.http.HttpServletRequest; | ||
|
|
||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.api.trace.SpanKind; | ||
| import io.opentelemetry.api.trace.StatusCode; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.ContextKey; | ||
| import io.opentelemetry.context.propagation.TextMapGetter; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| @WebFilter(urlPatterns = {"*"}) | ||
| public final class W3CTraceFilter implements Filter { | ||
|
|
||
| public static final ContextKey<String> TRACE_PARENT = ContextKey.named("traceparent"); | ||
| public static final Pattern TRACE_PARENT_MATCHER = | ||
| Pattern.compile("[a-z0-9]{2}-[a-z0-9]{32}-[a-z0-9]{16}-[a-z0-9]{2}"); | ||
|
|
||
| @SuppressWarnings("java:S1181") // We're catching Throwable intentionally so it can be recorded in the span. | ||
| @Override | ||
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
| throws IOException, ServletException { | ||
|
|
||
| var httpRequest = (HttpServletRequest)request; | ||
| var spanBuilder = GlobalOpenTelemetry.getTracer("cwms-data-api") | ||
| // using the full URI here results in too high of cardinality so spans can't be grouped | ||
| // endpoints can/should add the route as attributes to the current span. | ||
| .spanBuilder(httpRequest.getMethod() + " " + request.getServletContext()) | ||
| .setSpanKind(SpanKind.SERVER); | ||
| var provided = httpRequest.getHeader(TRACE_PARENT.toString()); | ||
| if (provided != null && !provided.isEmpty() && TRACE_PARENT_MATCHER.matcher(provided).matches()) { | ||
| var propagator = GlobalOpenTelemetry.getPropagators().getTextMapPropagator(); | ||
| var ctx = propagator.extract(Context.current(), provided, new TraceGetter()); | ||
| spanBuilder.setParent(ctx); | ||
| } | ||
|
|
||
| var span = spanBuilder.startSpan(); | ||
| try (var scope = span.makeCurrent()) { | ||
| chain.doFilter(request, response); | ||
| } catch (Throwable t) { | ||
| span.recordException(t); | ||
| span.setStatus(StatusCode.ERROR); | ||
| throw t; | ||
| } finally { | ||
| span.end(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A simple wrapper to just get the value in the required way. | ||
| */ | ||
| private static class TraceGetter implements TextMapGetter<String> | ||
| { | ||
| @Override | ||
| public Iterable<String> keys(@Nonnull String carrier) | ||
| { | ||
| return List.of(TRACE_PARENT.toString()); | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public String get(@Nullable String carrier, @Nonnull String key) { | ||
| if (TRACE_PARENT.toString().equalsIgnoreCase(key)) { | ||
| return carrier; | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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.