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

Context Propagation via W3C and B3 for OTel and Brave #32487

Closed
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 @@ -40,12 +40,16 @@
import io.micrometer.tracing.brave.bridge.BraveCurrentTraceContext;
import io.micrometer.tracing.brave.bridge.BraveHttpClientHandler;
import io.micrometer.tracing.brave.bridge.BraveHttpServerHandler;
import io.micrometer.tracing.brave.bridge.BravePropagator;
import io.micrometer.tracing.brave.bridge.BraveTracer;
import io.micrometer.tracing.brave.bridge.W3CPropagation;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -55,6 +59,7 @@
* {@link EnableAutoConfiguration Auto-configuration} for Brave.
*
* @author Moritz Halbritter
* @author Marcin Grzejszczak
* @since 3.0.0
*/
@AutoConfiguration(before = MicrometerTracingAutoConfiguration.class)
Expand Down Expand Up @@ -105,12 +110,6 @@ public CurrentTraceContext braveCurrentTraceContext(List<CurrentTraceContext.Sco
return builder.build();
}

@Bean
@ConditionalOnMissingBean
public Factory bravePropagationFactory() {
return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).build();
}

@Bean
@ConditionalOnMissingBean
public Sampler braveSampler(TracingProperties properties) {
Expand All @@ -135,21 +134,35 @@ public HttpClientHandler<HttpClientRequest, HttpClientResponse> httpClientHandle
return HttpClientHandler.create(httpTracing);
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingClass("io.micrometer.tracing.brave.bridge.BraveTracer")
static class BraveMicrometerMissing {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "management.tracing.propagation.type", havingValue = "B3", matchIfMissing = true)
Factory bravePropagationFactory() {
return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).build();
}

}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(BraveTracer.class)
static class BraveMicrometer {

private static final BraveBaggageManager BRAVE_BAGGAGE_MANAGER = new BraveBaggageManager();

@Bean
@ConditionalOnMissingBean
BraveTracer braveTracerBridge(brave.Tracer tracer, CurrentTraceContext currentTraceContext,
BraveBaggageManager braveBaggageManager) {
return new BraveTracer(tracer, new BraveCurrentTraceContext(currentTraceContext), braveBaggageManager);
BraveTracer braveTracerBridge(brave.Tracer tracer, CurrentTraceContext currentTraceContext) {
return new BraveTracer(tracer, new BraveCurrentTraceContext(currentTraceContext), BRAVE_BAGGAGE_MANAGER);
}

@Bean
@ConditionalOnMissingBean
BraveBaggageManager braveBaggageManager() {
return new BraveBaggageManager();
BravePropagator bravePropagator(Tracing tracing) {
return new BravePropagator(tracing);
}

@Bean
Expand All @@ -166,6 +179,33 @@ BraveHttpClientHandler braveHttpClientHandler(
return new BraveHttpClientHandler(httpClientHandler);
}

@Configuration(proxyBeanMethods = false)
static class BraveNoBaggageConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "management.tracing.propagation.type", havingValue = "W3C",
matchIfMissing = true)
Factory w3cPropagationNoBaggageFactory() {
return new W3CPropagation(BRAVE_BAGGAGE_MANAGER, List.of()); // TODO: Use
// snapshots
// of
// tracing
// to not
// use
// baggage
// for W3C
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "management.tracing.propagation.type", havingValue = "B3")
Factory b3PropagationNoBaggageFactory() {
return B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).build();
}

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@
import io.micrometer.tracing.SamplerFunction;
import io.micrometer.tracing.otel.bridge.DefaultHttpClientAttributesGetter;
import io.micrometer.tracing.otel.bridge.DefaultHttpServerAttributesExtractor;
import io.micrometer.tracing.otel.bridge.EventListener;
import io.micrometer.tracing.otel.bridge.EventPublishingContextWrapper;
import io.micrometer.tracing.otel.bridge.OtelBaggageManager;
import io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext;
import io.micrometer.tracing.otel.bridge.OtelHttpClientHandler;
import io.micrometer.tracing.otel.bridge.OtelHttpServerHandler;
import io.micrometer.tracing.otel.bridge.OtelPropagator;
import io.micrometer.tracing.otel.bridge.OtelTracer;
import io.micrometer.tracing.otel.bridge.OtelTracer.EventPublisher;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.ContextStorage;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.extension.trace.propagation.B3Propagator;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
Expand All @@ -48,6 +54,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -58,6 +65,7 @@
* {@link OpenTelemetryAutoConfiguration}.
*
* @author Moritz Halbritter
* @author Marcin Grzejszczak
*/
class OpenTelemetryConfigurations {

Expand Down Expand Up @@ -105,12 +113,43 @@ Sampler otelSampler(TracingProperties properties) {
}

@Bean
@ConditionalOnMissingBean
SpanProcessor otelSpanProcessor(List<SpanExporter> spanExporter) {
return SpanProcessor.composite(spanExporter.stream()
.map((exporter) -> BatchSpanProcessor.builder(exporter).build()).collect(Collectors.toList()));
}

@Configuration(proxyBeanMethods = false)
static class PropagationConfiguration {

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(B3Propagator.class)
static class B3NoBaggagePropagatorConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "management.tracing.propagation.type", havingValue = "B3")
B3Propagator b3TextMapPropagator() {
return B3Propagator.injectingSingleHeader();
}

}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(W3CTraceContextPropagator.class)
static class W3CNoBaggagePropagatorConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "management.tracing.propagation.type", havingValue = "W3C",
matchIfMissing = true)
W3CTraceContextPropagator w3cTextMapPropagatorWithoutBaggage() {
return W3CTraceContextPropagator.getInstance();
}

}

}

}

@Configuration(proxyBeanMethods = false)
Expand All @@ -128,6 +167,7 @@ Tracer otelTracer(OpenTelemetry openTelemetry) {

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(OtelTracer.class)
@EnableConfigurationProperties(TracingProperties.class)
static class MicrometerConfiguration {

@Bean
Expand All @@ -141,14 +181,21 @@ OtelTracer micrometerOtelTracer(Tracer tracer, EventPublisher eventPublisher,

@Bean
@ConditionalOnMissingBean
EventPublisher otelTracerEventPublisher() {
return (event) -> {
};
@ConditionalOnBean({ Tracer.class, ContextPropagators.class })
OtelPropagator otelPropagator(ContextPropagators contextPropagators, Tracer tracer) {
return new OtelPropagator(contextPropagators, tracer);
}

@Bean
@ConditionalOnMissingBean
OtelCurrentTraceContext otelCurrentTraceContext() {
EventPublisher otelTracerEventPublisher(List<EventListener> eventListeners) {
return new OTelEventPublisher(eventListeners);
}

@Bean
@ConditionalOnMissingBean
OtelCurrentTraceContext otelCurrentTraceContext(EventPublisher publisher) {
ContextStorage.addWrapper(new EventPublishingContextWrapper(publisher));
return new OtelCurrentTraceContext();
}

Expand All @@ -168,6 +215,23 @@ OtelHttpServerHandler otelHttpServerHandler(OpenTelemetry openTelemetry) {
new DefaultHttpServerAttributesExtractor());
}

static class OTelEventPublisher implements EventPublisher {

private final List<EventListener> listeners;

OTelEventPublisher(List<EventListener> listeners) {
this.listeners = listeners;
}

@Override
public void publishEvent(Object event) {
for (EventListener listener : this.listeners) {
listener.onEvent(event);
}
}

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@ public class TracingProperties {
*/
private final Sampling sampling = new Sampling();

/**
* Propagation configuration.
*/
private final Propagation propagation = new Propagation();

public Sampling getSampling() {
return this.sampling;
}

public Propagation getPropagation() {
return this.propagation;
}

public static class Sampling {

/**
Expand All @@ -53,4 +62,35 @@ public void setProbability(float probability) {

}

public static class Propagation {

/**
* Tracing context propagation types.
*/
private PropagationType type = PropagationType.W3C;

public PropagationType getType() {
return this.type;
}

public void setType(PropagationType type) {
this.type = type;
}

enum PropagationType {

/**
* B3 propagation type.
*/
B3,

/**
* W3C propagation type.
*/
W3C

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,14 @@
"replacement": "management.trace.http.include",
"level": "error"
}
},
{
"name": "management.tracing.propagation.type",
"defaultValue": "W3C"
},
{
"name": "management.tracing.sampling.probability",
"defaultValue": "0.10"
}
],
"hints": [
Expand Down
Loading