|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.kafkaclients.v2_6; |
| 7 | + |
| 8 | +import static io.opentelemetry.api.common.AttributeKey.stringArrayKey; |
| 9 | +import static io.opentelemetry.instrumentation.testing.util.TelemetryDataUtil.orderByRootSpanName; |
| 10 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 11 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; |
| 12 | +import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_BATCH_MESSAGE_COUNT; |
| 13 | +import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME; |
| 14 | +import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_DESTINATION_PARTITION_ID; |
| 15 | +import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_KAFKA_CONSUMER_GROUP; |
| 16 | +import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_KAFKA_MESSAGE_OFFSET; |
| 17 | +import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE; |
| 18 | +import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_OPERATION; |
| 19 | +import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_SYSTEM; |
| 20 | +import static java.util.Collections.singletonList; |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +import io.opentelemetry.api.trace.SpanContext; |
| 24 | +import io.opentelemetry.api.trace.SpanKind; |
| 25 | +import io.opentelemetry.sdk.trace.data.LinkData; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
| 28 | +import org.assertj.core.api.AbstractLongAssert; |
| 29 | +import org.assertj.core.api.AbstractStringAssert; |
| 30 | + |
| 31 | +class DeprecatedInterceptorsTest extends AbstractDeprecatedInterceptorsTest { |
| 32 | + |
| 33 | + @SuppressWarnings("deprecation") // using deprecated semconv |
| 34 | + @Override |
| 35 | + void assertTraces() { |
| 36 | + AtomicReference<SpanContext> producerSpanContext = new AtomicReference<>(); |
| 37 | + testing.waitAndAssertSortedTraces( |
| 38 | + orderByRootSpanName("parent", SHARED_TOPIC + " receive", "producer callback"), |
| 39 | + trace -> { |
| 40 | + trace.hasSpansSatisfyingExactly( |
| 41 | + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), |
| 42 | + span -> |
| 43 | + span.hasName(SHARED_TOPIC + " publish") |
| 44 | + .hasKind(SpanKind.PRODUCER) |
| 45 | + .hasParent(trace.getSpan(0)) |
| 46 | + .hasAttributesSatisfyingExactly( |
| 47 | + equalTo( |
| 48 | + stringArrayKey("messaging.header.Test_Message_Header"), |
| 49 | + singletonList("test")), |
| 50 | + equalTo(MESSAGING_SYSTEM, "kafka"), |
| 51 | + equalTo(MESSAGING_DESTINATION_NAME, SHARED_TOPIC), |
| 52 | + equalTo(MESSAGING_OPERATION, "publish"), |
| 53 | + satisfies( |
| 54 | + MESSAGING_CLIENT_ID, |
| 55 | + stringAssert -> stringAssert.startsWith("producer")))); |
| 56 | + SpanContext spanContext = trace.getSpan(1).getSpanContext(); |
| 57 | + producerSpanContext.set( |
| 58 | + SpanContext.createFromRemoteParent( |
| 59 | + spanContext.getTraceId(), |
| 60 | + spanContext.getSpanId(), |
| 61 | + spanContext.getTraceFlags(), |
| 62 | + spanContext.getTraceState())); |
| 63 | + }, |
| 64 | + trace -> |
| 65 | + trace.hasSpansSatisfyingExactly( |
| 66 | + span -> |
| 67 | + span.hasName(SHARED_TOPIC + " receive") |
| 68 | + .hasKind(SpanKind.CONSUMER) |
| 69 | + .hasNoParent() |
| 70 | + .hasLinksSatisfying(links -> assertThat(links).isEmpty()) |
| 71 | + .hasAttributesSatisfyingExactly( |
| 72 | + equalTo( |
| 73 | + stringArrayKey("messaging.header.Test_Message_Header"), |
| 74 | + singletonList("test")), |
| 75 | + equalTo(MESSAGING_SYSTEM, "kafka"), |
| 76 | + equalTo(MESSAGING_DESTINATION_NAME, SHARED_TOPIC), |
| 77 | + equalTo(MESSAGING_OPERATION, "receive"), |
| 78 | + equalTo(MESSAGING_KAFKA_CONSUMER_GROUP, "test"), |
| 79 | + satisfies( |
| 80 | + MESSAGING_CLIENT_ID, |
| 81 | + stringAssert -> stringAssert.startsWith("consumer")), |
| 82 | + equalTo(MESSAGING_BATCH_MESSAGE_COUNT, 1)), |
| 83 | + span -> |
| 84 | + span.hasName(SHARED_TOPIC + " process") |
| 85 | + .hasKind(SpanKind.CONSUMER) |
| 86 | + .hasParent(trace.getSpan(0)) |
| 87 | + .hasLinks(LinkData.create(producerSpanContext.get())) |
| 88 | + .hasAttributesSatisfyingExactly( |
| 89 | + equalTo( |
| 90 | + stringArrayKey("messaging.header.Test_Message_Header"), |
| 91 | + singletonList("test")), |
| 92 | + equalTo(MESSAGING_SYSTEM, "kafka"), |
| 93 | + equalTo(MESSAGING_DESTINATION_NAME, SHARED_TOPIC), |
| 94 | + equalTo(MESSAGING_OPERATION, "process"), |
| 95 | + equalTo( |
| 96 | + MESSAGING_MESSAGE_BODY_SIZE, |
| 97 | + greeting.getBytes(StandardCharsets.UTF_8).length), |
| 98 | + satisfies( |
| 99 | + MESSAGING_DESTINATION_PARTITION_ID, |
| 100 | + AbstractStringAssert::isNotEmpty), |
| 101 | + satisfies( |
| 102 | + MESSAGING_KAFKA_MESSAGE_OFFSET, AbstractLongAssert::isNotNegative), |
| 103 | + equalTo(MESSAGING_KAFKA_CONSUMER_GROUP, "test"), |
| 104 | + satisfies( |
| 105 | + MESSAGING_CLIENT_ID, |
| 106 | + stringAssert -> stringAssert.startsWith("consumer"))), |
| 107 | + span -> |
| 108 | + span.hasName("process child") |
| 109 | + .hasKind(SpanKind.INTERNAL) |
| 110 | + .hasParent(trace.getSpan(1))), |
| 111 | + // ideally we'd want producer callback to be part of the main trace, we just aren't able to |
| 112 | + // instrument that |
| 113 | + trace -> |
| 114 | + trace.hasSpansSatisfyingExactly( |
| 115 | + span -> |
| 116 | + span.hasName("producer callback").hasKind(SpanKind.INTERNAL).hasNoParent())); |
| 117 | + } |
| 118 | +} |
0 commit comments