Skip to content

add the headers capture feature to Kafka 2.6 interceptors #14290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tasks {
excludeTestsMatching("WrapperSuppressReceiveSpansTest")
}
jvmArgs("-Dotel.instrumentation.messaging.experimental.receive-telemetry.enabled=true")
jvmArgs("-Dotel.instrumentation.messaging.experimental.capture-headers=baggage")
}

check {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.instrumentation.kafkaclients.v2_6;

import static java.util.Collections.emptyList;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.Context;
Expand Down Expand Up @@ -32,6 +34,9 @@ public class TracingConsumerInterceptor<K, V> implements ConsumerInterceptor<K,
.setMessagingReceiveInstrumentationEnabled(
ConfigPropertiesUtil.getBoolean(
"otel.instrumentation.messaging.experimental.receive-telemetry.enabled", false))
.setCapturedHeaders(
ConfigPropertiesUtil.getList(
"otel.instrumentation.messaging.experimental.capture-headers", emptyList()))
.build();

private String consumerGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

package io.opentelemetry.instrumentation.kafkaclients.v2_6;

import static java.util.Collections.emptyList;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
Expand All @@ -22,7 +25,12 @@
*/
public class TracingProducerInterceptor<K, V> implements ProducerInterceptor<K, V> {

private static final KafkaTelemetry telemetry = KafkaTelemetry.create(GlobalOpenTelemetry.get());
private static final KafkaTelemetry telemetry =
KafkaTelemetry.builder(GlobalOpenTelemetry.get())
.setCapturedHeaders(
ConfigPropertiesUtil.getList(
"otel.instrumentation.messaging.experimental.capture-headers", emptyList()))
.build();

@Nullable private String clientId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.kafkaclients.v2_6;

import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;
import static io.opentelemetry.instrumentation.testing.util.TelemetryDataUtil.orderByRootSpanName;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
Expand All @@ -16,12 +17,15 @@
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE;
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_OPERATION;
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_SYSTEM;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.sdk.trace.data.LinkData;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
import org.assertj.core.api.AbstractLongAssert;
import org.assertj.core.api.AbstractStringAssert;
Expand All @@ -42,6 +46,11 @@ void assertTraces() {
.hasKind(SpanKind.PRODUCER)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(
stringArrayKey("messaging.header.baggage"),
asList(
"test-baggage-key-1=test-baggage-value-1",
"test-baggage-key-2=test-baggage-value-2")),
equalTo(MESSAGING_SYSTEM, "kafka"),
equalTo(MESSAGING_DESTINATION_NAME, SHARED_TOPIC),
equalTo(MESSAGING_OPERATION, "publish"),
Expand All @@ -64,6 +73,11 @@ void assertTraces() {
.hasNoParent()
.hasLinksSatisfying(links -> assertThat(links).isEmpty())
.hasAttributesSatisfyingExactly(
equalTo(
AttributeKey.stringArrayKey("messaging.header.baggage"),
Arrays.asList(
"test-baggage-key-1=test-baggage-value-1",
"test-baggage-key-2=test-baggage-value-2")),
equalTo(MESSAGING_SYSTEM, "kafka"),
equalTo(MESSAGING_DESTINATION_NAME, SHARED_TOPIC),
equalTo(MESSAGING_OPERATION, "receive"),
Expand All @@ -78,6 +92,11 @@ void assertTraces() {
.hasParent(trace.getSpan(0))
.hasLinks(LinkData.create(producerSpanContext.get()))
.hasAttributesSatisfyingExactly(
equalTo(
AttributeKey.stringArrayKey("messaging.header.baggage"),
Arrays.asList(
"test-baggage-key-1=test-baggage-value-1",
"test-baggage-key-2=test-baggage-value-2")),
equalTo(MESSAGING_SYSTEM, "kafka"),
equalTo(MESSAGING_DESTINATION_NAME, SHARED_TOPIC),
equalTo(MESSAGING_OPERATION, "process"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
description: >
This instrumentation provides a library integeration that enables messaging spans and metrics for Apache Kafka 2.6+ clients.
This instrumentation provides a library integrations that enables messaging spans and metrics for Apache Kafka 2.6+ clients.
configurations:
- name: otel.instrumentation.messaging.experimental.capture-headers
description: A comma-separated list of header names to capture as span attributes.
type: list
default: ''
- name: otel.instrumentation.messaging.experimental.receive-telemetry.enabled
description: >
Enables experimental receive telemetry, which will cause consumers to start a new trace, with
only a span link connecting it to the producer trace.
type: boolean
default: false
Loading