use of io.smallrye.reactive.messaging.TracingMetadata in project smallrye-reactive-messaging by smallrye.
the class AmqpConnector method incomingTrace.
private void incomingTrace(AmqpMessage<?> message) {
TracingMetadata tracingMetadata = TracingMetadata.fromMessage(message).orElse(TracingMetadata.empty());
final SpanBuilder spanBuilder = TRACER.spanBuilder(message.getAddress() + " receive").setSpanKind(SpanKind.CONSUMER);
// Handle possible parent span
final Context parentSpanContext = tracingMetadata.getPreviousContext();
if (parentSpanContext != null) {
spanBuilder.setParent(parentSpanContext);
} else {
spanBuilder.setNoParent();
}
final Span span = spanBuilder.startSpan();
// Set Span attributes
span.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "AMQP 1.0");
span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, message.getAddress());
span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "queue");
// Make available as parent for subsequent spans inside message processing
span.makeCurrent();
message.injectTracingMetadata(tracingMetadata.withSpan(span));
span.end();
}
use of io.smallrye.reactive.messaging.TracingMetadata 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.smallrye.reactive.messaging.TracingMetadata in project smallrye-reactive-messaging by smallrye.
the class TracingAmqpToAppNoParentTest method testFromAmqpToAppWithNoParent.
@Test
public void testFromAmqpToAppWithNoParent() {
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("mp.messaging.incoming.stuff.address", "no-parent-stuff").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();
usage.produce("no-parent-stuff", 10, count::getAndIncrement);
await().until(() -> bean.list().size() >= 10);
assertThat(bean.list()).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
assertThat(bean.tracing()).hasSizeGreaterThanOrEqualTo(10);
assertThat(bean.tracing()).doesNotContainNull().doesNotHaveDuplicates();
List<String> spanIds = new ArrayList<>();
for (TracingMetadata tracing : bean.tracing()) {
spanIds.add(Span.fromContext(tracing.getCurrentContext()).getSpanContext().getSpanId());
assertThat(Span.fromContextOrNull(tracing.getPreviousContext())).isNull();
}
assertThat(spanIds).doesNotContainNull().doesNotHaveDuplicates().hasSizeGreaterThanOrEqualTo(10);
for (SpanData data : testExporter.getFinishedSpanItems()) {
assertThat(data.getSpanId()).isIn(spanIds);
assertThat(data.getSpanId()).isNotEqualTo(data.getParentSpanId());
assertThat(data.getKind()).isEqualByComparingTo(SpanKind.CONSUMER);
}
}
use of io.smallrye.reactive.messaging.TracingMetadata in project smallrye-reactive-messaging by smallrye.
the class KafkaSink method createOutgoingTrace.
private void createOutgoingTrace(Message<?> message, String topic, int partition, Headers headers) {
if (isTracingEnabled) {
Optional<TracingMetadata> tracingMetadata = TracingMetadata.fromMessage(message);
final SpanBuilder spanBuilder = TRACER.spanBuilder(topic + " send").setSpanKind(SpanKind.PRODUCER);
if (tracingMetadata.isPresent()) {
// Handle possible parent span
final Context parentSpanContext = tracingMetadata.get().getCurrentContext();
if (parentSpanContext != null) {
spanBuilder.setParent(parentSpanContext);
} else {
spanBuilder.setNoParent();
}
} else {
spanBuilder.setNoParent();
}
final Span span = spanBuilder.startSpan();
Scope scope = span.makeCurrent();
// Set Span attributes
if (partition != -1) {
span.setAttribute(SemanticAttributes.MESSAGING_KAFKA_PARTITION, partition);
}
span.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "kafka");
span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, topic);
span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic");
// Set span onto headers
GlobalOpenTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), headers, HeaderInjectAdapter.SETTER);
span.end();
scope.close();
}
}
use of io.smallrye.reactive.messaging.TracingMetadata in project smallrye-reactive-messaging by smallrye.
the class KafkaSource method incomingTrace.
public void incomingTrace(IncomingKafkaRecord<K, V> kafkaRecord, boolean insideBatch) {
if (isTracingEnabled && TRACER != null) {
TracingMetadata tracingMetadata = TracingMetadata.fromMessage(kafkaRecord).orElse(TracingMetadata.empty());
final SpanBuilder spanBuilder = TRACER.spanBuilder(kafkaRecord.getTopic() + " receive").setSpanKind(SpanKind.CONSUMER);
// Handle possible parent span
final Context parentSpanContext = tracingMetadata.getPreviousContext();
if (parentSpanContext != null) {
spanBuilder.setParent(parentSpanContext);
} else {
spanBuilder.setNoParent();
}
final Span span = spanBuilder.startSpan();
// Set Span attributes
span.setAttribute(SemanticAttributes.MESSAGING_KAFKA_PARTITION, kafkaRecord.getPartition());
span.setAttribute("offset", kafkaRecord.getOffset());
span.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "kafka");
span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, kafkaRecord.getTopic());
span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic");
final String groupId = client.get(ConsumerConfig.GROUP_ID_CONFIG);
final String clientId = client.get(ConsumerConfig.CLIENT_ID_CONFIG);
span.setAttribute("messaging.consumer_id", constructConsumerId(groupId, clientId));
span.setAttribute(SemanticAttributes.MESSAGING_KAFKA_CONSUMER_GROUP, groupId);
if (!clientId.isEmpty()) {
span.setAttribute(SemanticAttributes.MESSAGING_KAFKA_CLIENT_ID, clientId);
}
if (!insideBatch) {
// Make available as parent for subsequent spans inside message processing
span.makeCurrent();
}
kafkaRecord.injectTracingMetadata(tracingMetadata.withSpan(span));
span.end();
}
}
Aggregations