Skip to content

Instantly share code, notes, and snippets.

@lmolkova
Last active November 18, 2021 07:59
Show Gist options
  • Save lmolkova/af083090744eea9eabc6f9d19fc16b16 to your computer and use it in GitHub Desktop.
Save lmolkova/af083090744eea9eabc6f9d19fc16b16 to your computer and use it in GitHub Desktop.
Context key shading issues
public static void main(String[] args) {
// first call in not instrumented...
doSomething();
doSomething().transform(t -> {
Span s = GlobalOpenTelemetry.getTracer("test")
.spanBuilder("span")
.startSpan();
return t.doFinally(signal -> s.end())
.contextWrite(ctx -> ContextPropagationOperator.storeOpenTelemetryContext(ctx, Context.current().with(s)));
}).block();
}
@WithSpan
private static Mono<String> doSomething() {
return Mono.deferContextual(ctx -> {
// one record is from ContextPropagationOperator.storeOpenTelemetryContext,
// another one from @WithSpan instrumentation (shaded context)
LOGGER.info("contains {} contexts", ctx.stream().count());
ctx.stream().forEach( entry -> {
LOGGER.info("key name={} hashCode={}, className={}", entry.getKey().toString(), entry.getKey().hashCode(), entry.getKey().getClass().getName());
LOGGER.info("value className={}", entry.getValue().getClass().getName());
// throws on shaded context
Context otelContxt = (Context) entry.getValue();
});
return Mono.just("result");
});
}
// output:
contains 2 contexts
key name=otel-trace-context hashCode=788344609, className=io.opentelemetry.instrumentation.reactor.ContextPropagationOperator$1
value className=io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage$AgentContextWrapper
key name=otel-trace-context hashCode=1506083063, className=io.opentelemetry.javaagent.shaded.instrumentation.reactor.ContextPropagationOperator$1
value className=io.opentelemetry.javaagent.shaded.io.opentelemetry.context.ArrayBasedContext
Exception in thread "main" java.lang.ClassCastException: class io.opentelemetry.javaagent.shaded.io.opentelemetry.context.ArrayBasedContext cannot be cast to class io.opentelemetry.context.Context (io.opentelemetry.javaagent.shaded.io.opentelemetry.context.ArrayBasedContext is in unnamed module of loader 'bootstrap'; io.opentelemetry.context.Context is in unnamed module of loader 'app')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment