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.1.3: Add context propagation to the Zipkin tracing provider (#9119) #9385

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void propagateData(OpenTelemetryContext context) {
}

/**
* OpenTelementry context.
* OpenTelemetry context.
*/
public static class OpenTelemetryContext {
private final Span span;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.helidon.common.LazyValue;
import io.helidon.common.Weight;
import io.helidon.common.Weighted;
import io.helidon.common.config.Config;
import io.helidon.common.config.GlobalConfig;
import io.helidon.tracing.Span;
import io.helidon.tracing.SpanListener;
import io.helidon.tracing.Tracer;
Expand All @@ -35,27 +37,48 @@
/**
* {@link java.util.ServiceLoader} service implementation of {@link io.helidon.tracing.spi.TracerProvider} for Open Tracing
* tracers.
* <p>
* When dealing with the global tracer, manage both the Helidon one and also the OpenTracing one in concert, whether
* defaulting them or assigning them via {@link #global(io.helidon.tracing.Tracer)}.
* </p>
*/
@Weight(Weighted.DEFAULT_WEIGHT - 50) // low weight, so it is easy to override
public class OpenTracingTracerProvider implements TracerProvider {

private static final LazyValue<List<SpanListener>> SPAN_LISTENERS =
LazyValue.create(() -> HelidonServiceLoader.create(ServiceLoader.load(SpanListener.class)).asList());

private LazyValue<Tracer> globalHelidonTracer = LazyValue.create(() -> {
Config tracingConfig = GlobalConfig.config().get("tracing");

// Set up to create an explicit OpenTracing tracer only if we have config for tracing, indicating that the user wants
// something other than the no-op implementation.
if (tracingConfig.exists()) {
io.opentracing.Tracer openTracingTracer = OpenTracingTracerBuilder.create(tracingConfig)
.build();
GlobalTracer.registerIfAbsent(openTracingTracer);
}
return OpenTracingTracer.create(GlobalTracer.get());
});

@Override
public TracerBuilder<?> createBuilder() {
return OpenTracingTracer.builder();
}

@Override
public Tracer global() {
return OpenTracingTracer.create(GlobalTracer.get());
return globalHelidonTracer.get();
}

@Override
public void global(Tracer tracer) {
if (tracer instanceof OpenTracingTracer opt) {
GlobalTracer.registerIfAbsent(opt.openTracing());
GlobalTracer.registerIfAbsent(() -> {
io.opentracing.Tracer openTracingTracer = opt.openTracing();
globalHelidonTracer = LazyValue.create(OpenTracingTracer.create(openTracingTracer));
return openTracingTracer;
});
}
}

Expand Down
34 changes: 27 additions & 7 deletions tracing/providers/zipkin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
<groupId>io.opentracing.brave</groupId>
<artifactId>brave-opentracing</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-context</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common.features</groupId>
<artifactId>helidon-common-features-api</artifactId>
Expand Down Expand Up @@ -138,17 +142,33 @@
</dependencies>
</plugin>
<!--
Because the Helidon Zipkin provider does not itself implement the Helidon tracing API (it does so through
OpenTracing), exempt this build from a test that requires that.
The propagation test expects the global tracer to be enabled. Other tests run with their own explicit config which
might have disabled the global tracer, so run the propagation test separately.
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>io.helidon.tracing.providers.tests.TestTracerAndSpanPropagation.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>default-test</id>
<configuration>
<excludes>
<exclude>io.helidon.tracing.providers.tests.TestTracerAndSpanPropagation.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>zipkin-propagation-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>io.helidon.tracing.providers.tests.TestTracerAndSpanPropagation.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2024 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.tracing.providers.zipkin;

import io.helidon.common.context.Contexts;
import io.helidon.common.context.spi.DataPropagationProvider;

import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;

/**
* Data propagation provider for the Helidon Zipkin tracing provider.
*/
public class ZipkinDataPropagationProvider implements DataPropagationProvider<ZipkinDataPropagationProvider.ZipkinContext> {

private static final System.Logger LOGGER = System.getLogger(ZipkinDataPropagationProvider.class.getName());

/**
* Creates new provider; public for service loading.
*/
public ZipkinDataPropagationProvider() {
}

@Override
public ZipkinContext data() {
Tracer tracer = Contexts.context()
.flatMap(ctx -> ctx.get(Tracer.class))
.orElseGet(GlobalTracer::get);
Span span = tracer.activeSpan();
return new ZipkinContext(tracer, span);
}

@Override
public void propagateData(ZipkinContext data) {
if (data != null && data.span != null) {
data.scope = data.tracer.activateSpan(data.span);
}
}

@Override
public void clearData(ZipkinContext data) {
if (data != null && data.scope != null) {
try {
data.scope.close();
} catch (Exception e) {
LOGGER.log(System.Logger.Level.TRACE, "Cannot close tracing span", e);
}
}
}

/**
* Zipkin-specific propagation context.
*/
static class ZipkinContext {

private final Tracer tracer;
private final Span span;
private Scope scope;

private ZipkinContext(Tracer tracer, Span span) {
this.tracer = tracer;
this.span = span;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public class ZipkinTracerProvider implements OpenTracingProvider {
private static final List<String> TRACING_CONTEXT_PROPAGATION_HEADERS =
List.of(X_OT_SPAN_CONTEXT, X_B3_TRACE_ID, X_B3_SPAN_ID, X_B3_PARENT_SPAN_ID, X_B3_SAMPLED, X_B3_FLAGS);

/**
* Public constructor for service loading.
*/
public ZipkinTracerProvider() {
}

@Override
public ZipkinTracerBuilder createBuilder() {
return ZipkinTracerBuilder.create();
Expand Down
3 changes: 3 additions & 0 deletions tracing/providers/zipkin/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
requires brave.opentracing;
requires brave;
requires io.helidon.common;
requires io.helidon.common.context;
requires io.helidon.tracing.providers.opentracing;
requires io.opentracing.noop;
requires io.opentracing.util;
Expand All @@ -51,4 +52,6 @@
provides io.helidon.tracing.providers.opentracing.spi.OpenTracingProvider
with io.helidon.tracing.providers.zipkin.ZipkinTracerProvider;

provides io.helidon.common.context.spi.DataPropagationProvider
with io.helidon.tracing.providers.zipkin.ZipkinDataPropagationProvider;
}
5 changes: 3 additions & 2 deletions tracing/providers/zipkin/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2017, 2023 Oracle and/or its affiliates.
# Copyright (c) 2017, 2024 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.
Expand Down Expand Up @@ -45,4 +45,5 @@ tracing:
int-tags:
tag5: 145
tag6: 741

# With changes to OpenTracing global tracer handling, provide a service name for the Zipkin implementation to use.
service: "helidon-test-service"