use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.
the class InstrumenterTest method server_http.
@Test
void server_http() {
Instrumenter<Map<String, String>, Map<String, String>> instrumenter = Instrumenter.<Map<String, String>, Map<String, String>>builder(otelTesting.getOpenTelemetry(), "test", unused -> "span").addAttributesExtractors(mockHttpServerAttributes, NetServerAttributesExtractor.create(new ConstantNetPeerIpGetter<>("2.2.2.2")), 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").hasAttributesSatisfying(attributes -> assertThat(attributes).containsEntry(SemanticAttributes.NET_PEER_IP, "2.2.2.2"))));
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.
the class BaseServletHelper method start.
public Context start(Context parentContext, ServletRequestContext<REQUEST> requestContext) {
Context context = instrumenter.start(parentContext, requestContext);
REQUEST request = requestContext.request();
SpanContext spanContext = Span.fromContext(context).getSpanContext();
// we do this e.g. so that servlet containers can use these values in their access logs
// TODO: These are only available when using servlet instrumentation or when server
// instrumentation extends servlet instrumentation e.g. jetty. Either remove or make sure they
// also work on tomcat and wildfly.
accessor.setRequestAttribute(request, "trace_id", spanContext.getTraceId());
accessor.setRequestAttribute(request, "span_id", spanContext.getSpanId());
context = addServletContextPath(context, request);
attachServerContext(context, request);
return context;
}
use of io.opentelemetry.api.trace.SpanContext in project smallrye-reactive-messaging by smallrye.
the class TracingAmqpToAppWithParentTest method testFromAmqpToAppWithParentSpan.
@Test
public void testFromAmqpToAppWithParentSpan() {
weld.addBeanClass(MyAppReceivingData.class);
new MapBasedConfig().put("mp.messaging.incoming.stuff.connector", AmqpConnector.CONNECTOR_NAME).put("mp.messaging.incoming.stuff.host", host).put("mp.messaging.incoming.stuff.port", port).put("amqp-username", username).put("amqp-password", password).write();
container = weld.initialize();
MyAppReceivingData bean = container.getBeanManager().createInstance().select(MyAppReceivingData.class).get();
await().until(() -> isAmqpConnectorReady(container));
AtomicInteger count = new AtomicInteger();
List<SpanContext> producedSpanContexts = new CopyOnWriteArrayList<>();
usage.produce("stuff", 10, () -> AmqpMessage.create().durable(false).ttl(10000).withIntegerAsBody(count.getAndIncrement()).applicationProperties(createTracingSpan(producedSpanContexts, "stuff")).build());
await().until(() -> bean.list().size() >= 10);
assertThat(bean.list()).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
List<String> producedTraceIds = producedSpanContexts.stream().map(SpanContext::getTraceId).collect(Collectors.toList());
assertThat(producedTraceIds).hasSize(10);
assertThat(bean.tracing()).hasSizeGreaterThanOrEqualTo(10);
assertThat(bean.tracing()).doesNotContainNull().doesNotHaveDuplicates();
List<String> receivedTraceIds = bean.tracing().stream().map(tracingMetadata -> Span.fromContext(tracingMetadata.getCurrentContext()).getSpanContext().getTraceId()).collect(Collectors.toList());
assertThat(receivedTraceIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
assertThat(receivedTraceIds).containsExactlyInAnyOrderElementsOf(producedTraceIds);
List<String> spanIds = new ArrayList<>();
for (TracingMetadata tracing : bean.tracing()) {
spanIds.add(Span.fromContext(tracing.getCurrentContext()).getSpanContext().getSpanId());
assertThat(tracing.getPreviousContext()).isNotNull();
Span previousSpan = Span.fromContextOrNull(tracing.getPreviousContext());
assertThat(previousSpan).isNotNull();
assertThat(previousSpan.getSpanContext().getTraceId()).isEqualTo(Span.fromContext(tracing.getCurrentContext()).getSpanContext().getTraceId());
assertThat(previousSpan.getSpanContext().getSpanId()).isNotEqualTo(Span.fromContext(tracing.getCurrentContext()).getSpanContext().getSpanId());
}
assertThat(spanIds).doesNotContainNull().doesNotHaveDuplicates().hasSizeGreaterThanOrEqualTo(10);
List<String> parentIds = bean.tracing().stream().map(tracingMetadata -> Span.fromContextOrNull(tracingMetadata.getPreviousContext()).getSpanContext().getSpanId()).collect(Collectors.toList());
assertThat(producedSpanContexts.stream().map(SpanContext::getSpanId)).containsExactlyElementsOf(parentIds);
for (SpanData data : testExporter.getFinishedSpanItems()) {
assertThat(data.getSpanId()).isIn(spanIds);
assertThat(data.getSpanId()).isNotEqualTo(data.getParentSpanId());
assertThat(data.getKind()).isEqualByComparingTo(SpanKind.CONSUMER);
assertThat(data.getParentSpanId()).isNotNull();
assertThat(data.getParentSpanId()).isIn(parentIds);
}
}
use of io.opentelemetry.api.trace.SpanContext in project smallrye-reactive-messaging by smallrye.
the class TracingAmqpToAppToAmqpTest method createTracingSpan.
private JsonObject createTracingSpan(List<SpanContext> spanContexts, String topic) {
Properties properties = new Properties();
final Span span = AmqpConnector.TRACER.spanBuilder(topic).setSpanKind(SpanKind.PRODUCER).startSpan();
final Context context = span.storeInContext(Context.current());
GlobalOpenTelemetry.getPropagators().getTextMapPropagator().inject(context, properties, (headers, key, value) -> {
if (headers != null) {
headers.put(key, value.getBytes(StandardCharsets.UTF_8));
}
});
spanContexts.add(span.getSpanContext());
return JsonObject.mapFrom(properties);
}
use of io.opentelemetry.api.trace.SpanContext in project smallrye-reactive-messaging by smallrye.
the class TracingPropagationTest method createTracingSpan.
private Iterable<Header> createTracingSpan(List<SpanContext> spanContexts, String topic) {
RecordHeaders proposedHeaders = new RecordHeaders();
final Span span = KafkaConnector.TRACER.spanBuilder(topic).setSpanKind(SpanKind.PRODUCER).startSpan();
final Context context = span.storeInContext(Context.current());
GlobalOpenTelemetry.getPropagators().getTextMapPropagator().inject(context, proposedHeaders, (headers, key, value) -> {
if (headers != null) {
headers.remove(key).add(key, value.getBytes(StandardCharsets.UTF_8));
}
});
spanContexts.add(span.getSpanContext());
return proposedHeaders;
}
Aggregations