Problem
Currently, there is no way (short of ugly hacks)† to add custom TextMapPropagators when using AutoConfiguredOpenTelemetrySdkBuilder. However, TextMapPropagator is the natural and idiomatic way to propagate certain request information, such as theoretical X-Example-Foo and X-Example-Bar headers. This leaves users with the choice of either using programmatic configuration, losing the advantages of automatic configuration (such as cross-platform consistency and ease of change deployment), or of manually propagating their organisation-specific request information without using a TextMapPropagator.
Solution 1: Add a method
It would be simple to add a method to AutoConfiguredOpenTelemetrySdkBuilder (#addAdditionalPropagators, for example) to support adding additional custom propagators.
Pros:
- Easy to implement
- Easy for applications to use
Cons:
- Not extensible to current or future cross-platform automatic or environment-based configuration
- Changing application behaviour requires recompiling the application (or manually adding your own environment-based configuration)
Solution 2: SPI
An SPI such as the following could be used to allow applications or libraries to supply TextMapPropagators which could then be enabled using the OTEL_PROPAGATORS environment variable.
// CC0
public interface TextMapPropagatorProvider {
String getName();
TextMapPropagator getInstance()
}
If the name is provided by users, it should be strongly recommended ("SHOULD", RFC 2119) that the name be domain-namespaced (e.g. com.example.foobar). Alternatively, the propagator's name could be the fully-qualified class name of the value returned by getInstance(); however, this makes configuration largely platform-dependent.
Pros:
- Potentially extensible to other platforms in the future
- Only requires a jar on the classpath and a change to an environment variable to use—potentially no code needed by applications
Cons:
- More work to implement in SDK
- Slightly more work for the implementers of custom
TextMapPropagators
Alternatives
The current alternatives I'm looking at are using the pure-Scala backend in otel4s or using the awful hack listed in the footnote.
Somewhat related is the following discussion: open-telemetry/opentelemetry-java-instrumentation#9489
† ugly hack
show
public class Customizer implements BiFunction<TextMapPropagator, ConfigProperties, TextMapPropagator> {
private AtomicBoolean added = new AtomicBoolean(false);
private Iterable<TextMapPropagator> propagators;
public Customizer(Iterable<TextMapPropagator> propagators) {
this.propagators = propagators;
}
@Override
public TextMapPropagator apply(TextMapPropagator textMapPropagator, ConfigProperties configProperties) {
if (!added.getAndSet(true)) {
// please excuse my less-than-pretty code; I primarily write Scala
List<TextMapPropagator> toAdd = new ArrayList<>();
toAdd.add(textMapPropagator);
for (TextMapPropagator propagator: propagators) {
toAdd.add(propagator);
}
return TextMapPropagator.composite(toAdd);
} else {
return textMapPropagator;
}
}
}
Problem
Currently, there is no way (short of ugly hacks)† to add custom
TextMapPropagators when usingAutoConfiguredOpenTelemetrySdkBuilder. However,TextMapPropagatoris the natural and idiomatic way to propagate certain request information, such as theoreticalX-Example-FooandX-Example-Barheaders. This leaves users with the choice of either using programmatic configuration, losing the advantages of automatic configuration (such as cross-platform consistency and ease of change deployment), or of manually propagating their organisation-specific request information without using aTextMapPropagator.Solution 1: Add a method
It would be simple to add a method to
AutoConfiguredOpenTelemetrySdkBuilder(#addAdditionalPropagators, for example) to support adding additional custom propagators.Pros:
Cons:
Solution 2: SPI
An SPI such as the following could be used to allow applications or libraries to supply
TextMapPropagators which could then be enabled using theOTEL_PROPAGATORSenvironment variable.If the name is provided by users, it should be strongly recommended ("SHOULD", RFC 2119) that the name be domain-namespaced (e.g.
com.example.foobar). Alternatively, the propagator's name could be the fully-qualified class name of the value returned bygetInstance(); however, this makes configuration largely platform-dependent.Pros:
Cons:
TextMapPropagatorsAlternatives
The current alternatives I'm looking at are using the pure-Scala backend in otel4s or using the awful hack listed in the footnote.
Somewhat related is the following discussion: open-telemetry/opentelemetry-java-instrumentation#9489
† ugly hack
show