|
| 1 | +--- |
| 2 | +alwaysApply: false |
| 3 | +description: Sentry Queues module and Java SDK queue tracing |
| 4 | +--- |
| 5 | +# Sentry Queues and Java SDK Queue Tracing |
| 6 | + |
| 7 | +## Product model |
| 8 | + |
| 9 | +Sentry Queues is built from tracing data. SDKs mark queue work with queue-specific span operations and messaging span data so Sentry can identify producers, consumers, destinations, latency, and failures. |
| 10 | + |
| 11 | +The important concepts are: |
| 12 | +- `queue.publish`: a span for enqueueing/publishing a message to a queue or topic. |
| 13 | +- `queue.process`: a transaction for processing a dequeued message. |
| 14 | +- Messaging span data, especially: |
| 15 | + - `messaging.system` (for example `kafka`) |
| 16 | + - `messaging.destination.name` (queue/topic name) |
| 17 | + - `messaging.message.id` |
| 18 | + - `messaging.message.retry.count` |
| 19 | + - `messaging.message.body.size` |
| 20 | + - `messaging.message.envelope.size` |
| 21 | + - `messaging.message.receive.latency` |
| 22 | +- Distributed tracing headers (`sentry-trace` and `baggage`) link producer-side work to consumer-side processing. |
| 23 | +- Queue receive latency is the time a message spent waiting between publish/enqueue and processing. For Java Kafka, this comes from the `sentry-task-enqueued-time` header that the producer writes and the consumer reads. |
| 24 | + |
| 25 | +The Queues UI is not backed by a separate Java event type. The Java SDK contributes data through spans/transactions with the expected operations, trace context, statuses, and messaging attributes. |
| 26 | + |
| 27 | +## Java SDK implementation |
| 28 | + |
| 29 | +Queue tracing is opt-in. `SentryOptions.isEnableQueueTracing()` defaults to `false` and can be enabled with `setEnableQueueTracing(true)` or external config key `enable-queue-tracing` (`sentry.enable-queue-tracing` in Spring Boot). Captured queue spans/transactions still depend on tracing being enabled and sampled. |
| 30 | + |
| 31 | +Kafka support lives in `sentry-kafka`: |
| 32 | +- `SentryKafkaProducer.wrap(Producer)` wraps Kafka `Producer.send(...)` calls. |
| 33 | + - Creates a `queue.publish` child span when there is an active span. |
| 34 | + - Sets `messaging.system=kafka` and `messaging.destination.name=<topic>`. |
| 35 | + - Injects `sentry-trace`, `baggage`, and `sentry-task-enqueued-time` headers. |
| 36 | + - Still injects tracing/enqueued-time headers when queue tracing is enabled but there is no active span, so background producers can link to consumers. |
| 37 | + - Finishes the span from the Kafka callback with `OK` or `INTERNAL_ERROR`. |
| 38 | +- `SentryKafkaConsumerTracing.withTracing(record, callback)` is the manual raw-Kafka consumer helper. |
| 39 | + - Forks root scopes for the processing lifecycle and makes them current. |
| 40 | + - Continues the trace from Kafka headers. |
| 41 | + - Starts a `queue.process` transaction bound to scope when tracing is enabled. |
| 42 | + - Sets Kafka messaging data, body size, retry count, and receive latency when available. |
| 43 | + - Finishes with `OK` or `INTERNAL_ERROR` and never lets instrumentation failures break customer processing. |
| 44 | + |
| 45 | +Spring Kafka support lives in `sentry-spring`, `sentry-spring-jakarta`, and `sentry-spring-7`: |
| 46 | +- `SentryKafkaProducerBeanPostProcessor` installs a producer post-processor on `DefaultKafkaProducerFactory` and wraps created producers with `SentryKafkaProducer.wrap(...)`. |
| 47 | +- `SentryKafkaConsumerBeanPostProcessor` installs `SentryKafkaRecordInterceptor` on listener container factories. |
| 48 | +- `SentryKafkaRecordInterceptor` starts/finishes `queue.process` transactions around listener processing, continues traces from headers, forks scopes for the record lifecycle, and preserves any existing delegate interceptor. |
| 49 | +- Spring Boot auto-configuration registers both post-processors only when Spring Kafka and `sentry-kafka` are present and `sentry.enable-queue-tracing=true`. |
| 50 | +- Spring Boot queue auto-configuration is disabled when Sentry OpenTelemetry integration classes are present to avoid duplicate Kafka instrumentation. |
| 51 | + |
| 52 | +## Trace origins and suppression |
| 53 | + |
| 54 | +Queue instrumentation sets span origins so it can be identified and suppressed with `ignoredSpanOrigins`: |
| 55 | +- Raw Kafka producer: `auto.queue.kafka.producer` |
| 56 | +- Raw Kafka consumer helper: `manual.queue.kafka.consumer` |
| 57 | +- Spring Kafka producer: `auto.queue.spring.kafka.producer`, `auto.queue.spring_jakarta.kafka.producer`, `auto.queue.spring7.kafka.producer` |
| 58 | +- Spring Kafka consumer: `auto.queue.spring.kafka.consumer`, `auto.queue.spring_jakarta.kafka.consumer`, `auto.queue.spring7.kafka.consumer` |
| 59 | + |
| 60 | +## Files to inspect when changing queue tracing |
| 61 | + |
| 62 | +- Core option and conventions: |
| 63 | + - `sentry/src/main/java/io/sentry/SentryOptions.java` |
| 64 | + - `sentry/src/main/java/io/sentry/ExternalOptions.java` |
| 65 | + - `sentry/src/main/java/io/sentry/SpanDataConvention.java` |
| 66 | +- Raw Kafka: |
| 67 | + - `sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaProducer.java` |
| 68 | + - `sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaConsumerTracing.java` |
| 69 | + - `sentry-kafka/src/test/kotlin/io/sentry/kafka/*Test.kt` |
| 70 | +- Spring Kafka: |
| 71 | + - `sentry-spring*/src/main/java/io/sentry/**/kafka/*` |
| 72 | + - `sentry-spring*/src/test/kotlin/io/sentry/**/kafka/*Test.kt` |
| 73 | + - `sentry-spring-boot*/src/main/java/io/sentry/**/SentryAutoConfiguration.java` |
| 74 | + - `sentry-spring-boot*/src/test/kotlin/io/sentry/**/SentryKafkaAutoConfigurationTest.kt` |
| 75 | + |
| 76 | +## Related rules |
| 77 | + |
| 78 | +Also fetch: |
| 79 | +- `options` when changing `enableQueueTracing` or configuration surfaces. |
| 80 | +- `scopes` when changing consumer scope forking/lifecycle. |
| 81 | +- `opentelemetry` when changing coexistence with OTel auto-instrumentation. |
| 82 | +- `api` when changing public Kafka APIs or option methods. |
0 commit comments