use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.
the class LogDataBuilderTest method canSetSpanContextFromContext.
@Test
void canSetSpanContextFromContext() {
String traceId = "33333333333333333333333333333333";
String spanId = "7777777777777777";
SpanContext spanContext = SpanContext.create(traceId, spanId, TraceFlags.getSampled(), TraceState.getDefault());
Span span = Span.wrap(spanContext);
Context context = mock(Context.class);
when(context.get(any())).thenReturn(span);
LogDataBuilder builder = LogDataBuilder.create(resource, libraryInfo);
LogData result = builder.setContext(context).build();
assertSame(spanContext, result.getSpanContext());
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.
the class ParentContextExtractorTest method shouldExtractCaseInsensitiveHeaders.
@Test
void shouldExtractCaseInsensitiveHeaders() {
// given
Map<String, String> headers = ImmutableMap.of("X-b3-traceId", "4fd0b6131f19f39af59518d127b0cafe", "x-b3-spanid", "0000000000000456", "X-B3-Sampled", "true");
// 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("4fd0b6131f19f39af59518d127b0cafe");
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.
the class InstrumenterTest method client_error.
@Test
void client_error() {
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);
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, new IllegalStateException("test"));
otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("span").hasStatus(StatusData.error())));
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.
the class InstrumenterTest method server.
@Test
void server() {
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()).newServerInstrumenter(new MapGetter());
Context context = instrumenter.start(Context.root(), REQUEST);
SpanContext spanContext = Span.fromContext(context).getSpanContext();
assertThat(spanContext.isValid()).isTrue();
assertThat(SpanKey.SERVER.fromContextOrNull(context).getSpanContext()).isEqualTo(spanContext);
instrumenter.end(context, REQUEST, RESPONSE, null);
otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("span").hasKind(SpanKind.SERVER).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 server_parent.
@Test
void server_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()).newServerInstrumenter(new MapGetter());
Map<String, String> request = new HashMap<>(REQUEST);
W3CTraceContextPropagator.getInstance().inject(Context.root().with(Span.wrap(SpanContext.createFromRemoteParent("ff01020304050600ff0a0b0c0d0e0f00", "090a0b0c0d0e0f00", TraceFlags.getSampled(), TraceState.getDefault()))), request, Map::put);
Context context = instrumenter.start(Context.root(), request);
SpanContext spanContext = Span.fromContext(context).getSpanContext();
assertThat(spanContext.isValid()).isTrue();
assertThat(SpanKey.SERVER.fromContextOrNull(context).getSpanContext()).isEqualTo(spanContext);
instrumenter.end(context, request, RESPONSE, null);
otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("span").hasTraceId("ff01020304050600ff0a0b0c0d0e0f00").hasSpanId(spanContext.getSpanId()).hasParentSpanId("090a0b0c0d0e0f00")));
}
Aggregations