|
32 | 32 | import com.azure.core.util.TracingOptions; |
33 | 33 | import com.azure.core.util.tracing.Tracer; |
34 | 34 | import com.azure.core.util.tracing.TracerProvider; |
| 35 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 36 | +import io.opentelemetry.api.trace.Span; |
35 | 37 | import io.opentelemetry.api.trace.SpanKind; |
36 | 38 | import io.opentelemetry.instrumentation.api.internal.SpanKey; |
37 | 39 | import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
@@ -78,6 +80,34 @@ void testSpan() { |
78 | 80 | equalTo(stringKey("az.namespace"), "otel.tests")))); |
79 | 81 | } |
80 | 82 |
|
| 83 | + @Test |
| 84 | + void testExplicitParentContextBridge() { |
| 85 | + // Azure's bundled OpenTelemetryTracer reflectively calls |
| 86 | + // io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage |
| 87 | + // #getAgentContext(application io.opentelemetry.context.Context) |
| 88 | + // to convert an explicitly-supplied application context into the agent (shaded) context. |
| 89 | + // This test exercises that path: the parent span is supplied only via the Azure context bag |
| 90 | + // under PARENT_TRACE_CONTEXT_KEY and is never made current, so the only way the child can be |
| 91 | + // linked to it is through the reflective bridge. |
| 92 | + Tracer azTracer = createAzTracer(); |
| 93 | + |
| 94 | + Span parentSpan = GlobalOpenTelemetry.getTracer("test").spanBuilder("parent").startSpan(); |
| 95 | + // application (unshaded) context carrying the parent span, NOT made current |
| 96 | + io.opentelemetry.context.Context parentContext = |
| 97 | + io.opentelemetry.context.Context.root().with(parentSpan); |
| 98 | + |
| 99 | + Context azContext = new Context(Tracer.PARENT_TRACE_CONTEXT_KEY, parentContext); |
| 100 | + Context child = azTracer.start("child", azContext); |
| 101 | + azTracer.end(null, null, child); |
| 102 | + parentSpan.end(); |
| 103 | + |
| 104 | + testing.waitAndAssertTracesWithoutScopeVersionVerification( |
| 105 | + trace -> |
| 106 | + trace.hasSpansSatisfyingExactly( |
| 107 | + span -> span.hasName("parent").hasNoParent(), |
| 108 | + span -> span.hasName("child").hasParent(trace.getSpan(0)))); |
| 109 | + } |
| 110 | + |
81 | 111 | @Test |
82 | 112 | void testPipelineAndSuppression() { |
83 | 113 | AtomicBoolean hasClientAndHttpKeys = new AtomicBoolean(false); |
|
0 commit comments