use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.
the class ParentContextExtractorTest method shouldUseHttpIfAwsParentNotSampled.
@Test
void shouldUseHttpIfAwsParentNotSampled() {
// given
Map<String, String> headers = ImmutableMap.of("X-b3-traceId", "4fd0b6131f19f39af59518d127b0cafe", "x-b3-spanid", "0000000000000123", "X-B3-Sampled", "true");
environmentVariables.set("_X_AMZN_TRACE_ID", "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=0000000000000456;Sampled=0");
// when
Context context = ParentContextExtractor.extract(headers, INSTRUMENTER);
// then
Span span = Span.fromContext(context);
SpanContext spanContext = span.getSpanContext();
assertThat(spanContext.isValid()).isTrue();
assertThat(spanContext.isValid()).isTrue();
assertThat(spanContext.getSpanId()).isEqualTo("0000000000000123");
assertThat(spanContext.getTraceId()).isEqualTo("4fd0b6131f19f39af59518d127b0cafe");
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.
the class ParentContextExtractorTest method shouldPreferAwsParentHeaderIfValidAndSampled.
@Test
void shouldPreferAwsParentHeaderIfValidAndSampled() {
// given
Map<String, String> headers = ImmutableMap.of("X-b3-traceId", "4fd0b6131f19f39af59518d127b0cafe", "x-b3-spanid", "0000000000000456", "X-B3-Sampled", "true");
environmentVariables.set("_X_AMZN_TRACE_ID", "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=0000000000000456;Sampled=1");
// when
Context context = ParentContextExtractor.extract(headers, INSTRUMENTER);
// then
Span span = Span.fromContext(context);
SpanContext spanContext = span.getSpanContext();
assertThat(spanContext.isValid()).isTrue();
assertThat(spanContext.isValid()).isTrue();
assertThat(spanContext.getSpanId()).isEqualTo("0000000000000456");
assertThat(spanContext.getTraceId()).isEqualTo("8a3c60f7d188f8fa79d48a391a778fa6");
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.
the class ContextDispatcher method dispatch.
@Override
// Instrumenting deprecated class
@SuppressWarnings("deprecation")
public void dispatch(Remote obj, java.rmi.server.RemoteCall call) throws IOException {
ObjectInput in = call.getInputStream();
int operationId = in.readInt();
// skip 8 bytes
in.readLong();
if (PROPAGATOR.isOperationWithPayload(operationId)) {
ContextPayload payload = ContextPayload.read(in);
if (payload != null) {
Context context = payload.extract();
SpanContext spanContext = Span.fromContext(context).getSpanContext();
if (spanContext.isValid()) {
THREAD_LOCAL_CONTEXT.set(context);
} else {
THREAD_LOCAL_CONTEXT.set(null);
}
}
}
// send result stream the client is expecting
call.getResultStream(true);
// release held streams to allow next call to continue
call.releaseInputStream();
call.releaseOutputStream();
call.done();
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.
the class InstrumenterTest method client.
@Test
void client() {
Instrumenter<Map<String, String>, Map<String, String>> instrumenter = Instrumenter.<Map<String, String>, Map<String, String>>builder(otelTesting.getOpenTelemetry(), "test", unused -> "span").addAttributesExtractors(new AttributesExtractor1(), new AttributesExtractor2()).addSpanLinksExtractor(new LinksExtractor()).newClientInstrumenter(Map::put);
Map<String, String> request = new HashMap<>(REQUEST);
Context context = instrumenter.start(Context.root(), request);
SpanContext spanContext = Span.fromContext(context).getSpanContext();
assertThat(spanContext.isValid()).isTrue();
assertThat(request).containsKey("traceparent");
instrumenter.end(context, request, RESPONSE, null);
otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("span").hasKind(SpanKind.CLIENT).hasInstrumentationLibraryInfo(InstrumentationLibraryInfo.create("test", null)).hasTraceId(spanContext.getTraceId()).hasSpanId(spanContext.getSpanId()).hasParentSpanId(SpanId.getInvalid()).hasStatus(StatusData.unset()).hasLinks(expectedSpanLink()).hasAttributesSatisfying(attributes -> assertThat(attributes).containsOnly(attributeEntry("req1", "req1_value"), attributeEntry("req2", "req2_2_value"), attributeEntry("req3", "req3_value"), attributeEntry("resp1", "resp1_value"), attributeEntry("resp2", "resp2_2_value"), attributeEntry("resp3", "resp3_value")))));
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.
the class InstrumenterTest method client_parent.
@Test
void client_parent() {
Instrumenter<Map<String, String>, Map<String, String>> instrumenter = Instrumenter.<Map<String, String>, Map<String, String>>builder(otelTesting.getOpenTelemetry(), "test", unused -> "span").addAttributesExtractors(new AttributesExtractor1(), new AttributesExtractor2()).newClientInstrumenter(Map::put);
Context parent = Context.root().with(Span.wrap(SpanContext.create("ff01020304050600ff0a0b0c0d0e0f00", "090a0b0c0d0e0f00", TraceFlags.getSampled(), TraceState.getDefault())));
Map<String, String> request = new HashMap<>(REQUEST);
Context context = instrumenter.start(parent, request);
SpanContext spanContext = Span.fromContext(context).getSpanContext();
assertThat(spanContext.isValid()).isTrue();
assertThat(request).containsKey("traceparent");
instrumenter.end(context, request, RESPONSE, new IllegalStateException("test"));
otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("span").hasTraceId("ff01020304050600ff0a0b0c0d0e0f00").hasSpanId(spanContext.getSpanId()).hasParentSpanId("090a0b0c0d0e0f00")));
}
Aggregations