/** * Takes an {@link ActiveSpan} and attaches it to the {@link WorkItem} which can be fetched later * using {@link WorkItem#getSourceSpan()} */ public void setSourceSpan(@Nullable ActiveSpan activeSpan) { setSourceSpan(activeSpan, GlobalTracer.get()); }
boolean success; try { workItem.setSourceSpan(GlobalTracer.get().activeSpan()); WorkDetails workDetails = computeWorkDetails(workItem); if (SnapshotMetadataMgr.getInitializationMetadata(
@Test public void testNullActiveSpan() { _workItem.setSourceSpan(null, _mockTracer); SpanContext sourceSpanContext = _workItem.getSourceSpan(_mockTracer); assertThat(sourceSpanContext, nullValue()); try (ActiveSpan childSpan = _mockTracer .buildSpan("test dangling child") .addReference(References.FOLLOWS_FROM, sourceSpanContext) .startActive()) { assertThat(childSpan, notNullValue()); assertThat(childSpan, instanceOf(ThreadLocalActiveSpan.class)); } }
@Test public void testNullActiveSpanNoop() { _workItem.setSourceSpan(null, _noopTracer); SpanContext sourceSpanContext = _workItem.getSourceSpan(_noopTracer); try (ActiveSpan childSpan = _noopTracer .buildSpan("test dangling child") .addReference(References.FOLLOWS_FROM, sourceSpanContext) .startActive()) { assertThat(childSpan, notNullValue()); assertThat(childSpan, instanceOf(NoopActiveSpan.class)); } }
@Test public void testInjectExtractNoop() { ActiveSpan activeSpan = _noopTracer.buildSpan("test span").startActive(); _workItem.setSourceSpan(activeSpan, _noopTracer); try (ActiveSpan childSpan = _noopTracer .buildSpan("test span") .addReference(References.FOLLOWS_FROM, _workItem.getSourceSpan(_noopTracer)) .startActive()) { assertThat(childSpan, notNullValue()); assertThat(childSpan, instanceOf(NoopActiveSpan.class)); } }
@Test public void testInjectExtract() { MockContext sourceContext; try (ActiveSpan activeSpan = _mockTracer.buildSpan("test span").startActive()) { SpanContext sourceContextTmp = activeSpan.context(); assertThat(sourceContextTmp, instanceOf(MockContext.class)); sourceContext = (MockContext) sourceContextTmp; _workItem.setSourceSpan(activeSpan, _mockTracer); } SpanContext extractedContextTmp = _workItem.getSourceSpan(_mockTracer); assertThat(extractedContextTmp, notNullValue()); assertThat(extractedContextTmp, instanceOf(MockContext.class)); MockContext extractedContext = (MockContext) extractedContextTmp; // test that injected and extracted spans have same span context data assertThat(extractedContext.traceId(), equalTo(sourceContext.traceId())); assertThat(extractedContext.spanId(), equalTo(sourceContext.spanId())); } }