Skip to content

Commit d2f4d8c

Browse files
adinauerclaude
andcommitted
fix(kafka): Honor ignored producer span origins
Short-circuit the raw Kafka producer interceptor when its trace origin is configured in ignoredSpanOrigins. This lets customers disable the integration quickly without relying on the later no-op span path, and keeps the interceptor from injecting tracing headers when the origin is ignored. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 74430c0 commit d2f4d8c

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaProducerInterceptor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.sentry.SpanDataConvention;
1010
import io.sentry.SpanOptions;
1111
import io.sentry.SpanStatus;
12+
import io.sentry.util.SpanUtils;
1213
import io.sentry.util.TracingUtils;
1314
import java.nio.charset.StandardCharsets;
1415
import java.util.Map;
@@ -45,7 +46,7 @@ public SentryKafkaProducerInterceptor(
4546

4647
@Override
4748
public @NotNull ProducerRecord<K, V> onSend(final @NotNull ProducerRecord<K, V> record) {
48-
if (!scopes.getOptions().isEnableQueueTracing()) {
49+
if (!scopes.getOptions().isEnableQueueTracing() || isIgnored()) {
4950
return record;
5051
}
5152

@@ -81,6 +82,10 @@ public SentryKafkaProducerInterceptor(
8182
public void onAcknowledgement(
8283
final @Nullable RecordMetadata metadata, final @Nullable Exception exception) {}
8384

85+
private boolean isIgnored() {
86+
return SpanUtils.isIgnored(scopes.getOptions().getIgnoredSpanOrigins(), traceOrigin);
87+
}
88+
8489
@Override
8590
public void close() {}
8691

sentry-kafka/src/test/kotlin/io/sentry/kafka/SentryKafkaProducerInterceptorTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,23 @@ class SentryKafkaProducerInterceptorTest {
9090
assertEquals(0, tx.spans.size)
9191
}
9292

93+
@Test
94+
fun `does not create span when trace origin is ignored`() {
95+
val tx = createTransaction()
96+
options.setIgnoredSpanOrigins(listOf(SentryKafkaProducerInterceptor.TRACE_ORIGIN))
97+
val interceptor = SentryKafkaProducerInterceptor<String, String>(scopes)
98+
val record = ProducerRecord("my-topic", "key", "value")
99+
100+
interceptor.onSend(record)
101+
102+
assertEquals(0, tx.spans.size)
103+
assertEquals(null, record.headers().lastHeader(SentryTraceHeader.SENTRY_TRACE_HEADER))
104+
assertEquals(
105+
null,
106+
record.headers().lastHeader(SentryKafkaProducerInterceptor.SENTRY_ENQUEUED_TIME_HEADER),
107+
)
108+
}
109+
93110
@Test
94111
fun `returns original record when no active span`() {
95112
whenever(scopes.span).thenReturn(null)

0 commit comments

Comments
 (0)