use of io.opentelemetry.api.trace.SpanContext in project splunk-otel-java by signalfx.
the class SpanContextualizer method updateContext.
/**
* This updates the tracked thread context for a given span. This must only be called with
* ContextAttached events.
*/
public void updateContext(RecordedEvent event) {
String traceId = event.getString("traceId");
String spanId = event.getString("spanId");
long javaThreadId = event.getThread().getJavaThreadId();
logger.debug("Set thread context: [{}] {} {} at {}", javaThreadId, traceId, spanId, event.getStartTime());
if (traceId == null || spanId == null) {
threadSpans.remove(javaThreadId);
} else {
TraceFlags traceFlags = TraceFlags.fromByte(event.getByte("traceFlags"));
SpanContext spanContext = SpanContext.create(traceId, spanId, traceFlags, TraceState.getDefault());
SpanLinkage linkage = new SpanLinkage(spanContext, javaThreadId);
threadSpans.put(javaThreadId, linkage);
}
}
use of io.opentelemetry.api.trace.SpanContext in project smallrye-reactive-messaging by smallrye.
the class BatchTracingPropagationTest 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;
}
use of io.opentelemetry.api.trace.SpanContext in project smallrye-reactive-messaging by smallrye.
the class BatchTracingPropagationTest method testFromKafkaToAppWithParentSpan.
@Test
public void testFromKafkaToAppWithParentSpan() {
String parentTopic = topic + "-parent";
String stuffTopic = topic + "-stuff";
MyAppReceivingData bean = runApplication(getKafkaSinkConfigForMyAppReceivingData(parentTopic), MyAppReceivingData.class);
List<SpanContext> producedSpanContexts = new CopyOnWriteArrayList<>();
companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(parentTopic, null, null, "a-key", i, createTracingSpan(producedSpanContexts, stuffTopic)), 10);
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 TracingPropagationTest method testFromKafkaToAppToKafka.
@Test
public void testFromKafkaToAppToKafka() {
List<Context> receivedContexts = new CopyOnWriteArrayList<>();
String resultTopic = topic + "-result";
String parentTopic = topic + "-parent";
ConsumerTask<String, Integer> consumed = companion.consumeIntegers().fromTopics(resultTopic, m -> m.plug(until(10L, Duration.ofMinutes(1), null)).onItem().invoke(record -> {
receivedContexts.add(GlobalOpenTelemetry.getPropagators().getTextMapPropagator().extract(Context.current(), record.headers(), new HeaderExtractAdapter()));
}));
MyAppProcessingData bean = runApplication(getKafkaSinkConfigForMyAppProcessingData(resultTopic, parentTopic), MyAppProcessingData.class);
List<SpanContext> producedSpanContexts = new CopyOnWriteArrayList<>();
companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(parentTopic, null, null, "a-key", i, createTracingSpan(producedSpanContexts, parentTopic)), 10);
await().until(() -> consumed.getRecords().size() >= 10);
List<Integer> values = new ArrayList<>();
assertThat(consumed.getRecords()).allSatisfy(record -> {
assertThat(record.value()).isNotNull();
values.add(record.value());
});
assertThat(values).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<String> producedTraceIds = producedSpanContexts.stream().map(SpanContext::getTraceId).collect(Collectors.toList());
assertThat(producedTraceIds).hasSize(10);
assertThat(receivedContexts).hasSize(10);
assertThat(receivedContexts).doesNotContainNull().doesNotHaveDuplicates();
List<String> receivedSpanIds = receivedContexts.stream().map(context -> Span.fromContextOrNull(context).getSpanContext().getSpanId()).collect(Collectors.toList());
assertThat(receivedSpanIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
List<String> receivedTraceIds = receivedContexts.stream().map(context -> Span.fromContextOrNull(context).getSpanContext().getTraceId()).collect(Collectors.toList());
assertThat(receivedTraceIds).doesNotContainNull().doesNotHaveDuplicates().hasSize(10);
assertThat(receivedTraceIds).containsExactlyInAnyOrderElementsOf(producedTraceIds);
assertThat(bean.tracing()).hasSizeGreaterThanOrEqualTo(10);
assertThat(bean.tracing()).doesNotContainNull().doesNotHaveDuplicates();
List<String> spanIds = new ArrayList<>();
for (TracingMetadata tracing : bean.tracing()) {
Span span = Span.fromContext(tracing.getCurrentContext());
spanIds.add(span.getSpanContext().getSpanId());
assertThat(Span.fromContextOrNull(tracing.getPreviousContext())).isNotNull();
}
await().atMost(Duration.ofMinutes(2)).until(() -> testExporter.getFinishedSpanItems().size() >= 10);
List<String> outgoingParentIds = new ArrayList<>();
List<String> incomingParentIds = new ArrayList<>();
for (SpanData data : testExporter.getFinishedSpanItems()) {
if (data.getKind().equals(SpanKind.CONSUMER)) {
incomingParentIds.add(data.getParentSpanId());
assertThat(data.getAttributes().get(SemanticAttributes.MESSAGING_KAFKA_CONSUMER_GROUP)).isNotNull();
assertThat(data.getAttributes().get(AttributeKey.stringKey("messaging.consumer_id"))).isNotNull();
// Need to skip the spans created during @Incoming processing
continue;
}
assertThat(data.getSpanId()).isIn(receivedSpanIds);
assertThat(data.getSpanId()).isNotEqualTo(data.getParentSpanId());
assertThat(data.getTraceId()).isIn(producedTraceIds);
assertThat(data.getKind()).isEqualByComparingTo(SpanKind.PRODUCER);
outgoingParentIds.add(data.getParentSpanId());
}
// Assert span created on Kafka record is the parent of consumer span we create
assertThat(producedSpanContexts.stream().map(SpanContext::getSpanId)).containsExactlyElementsOf(incomingParentIds);
// Assert consumer span is the parent of the producer span we received in Kafka
assertThat(spanIds.stream()).containsExactlyElementsOf(outgoingParentIds);
}
use of io.opentelemetry.api.trace.SpanContext in project smallrye-reactive-messaging by smallrye.
the class TracingPropagationTest method testFromKafkaToAppWithParentSpan.
@Test
public void testFromKafkaToAppWithParentSpan() {
String parentTopic = topic + "-parent";
String stuffTopic = topic + "-stuff";
MyAppReceivingData bean = runApplication(getKafkaSinkConfigForMyAppReceivingData(parentTopic), MyAppReceivingData.class);
List<SpanContext> producedSpanContexts = new CopyOnWriteArrayList<>();
companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(parentTopic, null, null, "a-key", i, createTracingSpan(producedSpanContexts, stuffTopic)), 10);
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);
}
}
Aggregations