Skip to content

fix(kafka): report canonical consumer span topic - #3395

Open
lesnik512 wants to merge 4 commits into
ag2ai:mainfrom
lesnik512:fix/kafka-consumer-topic
Open

lesnik512 wants to merge 4 commits into
ag2ai:mainfrom
lesnik512:fix/kafka-consumer-topic

Conversation

@lesnik512

@lesnik512 lesnik512 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stock aiokafka and Confluent consumer spans currently expose the topic only as messaging.destination_publish.name. Emit messaging.destination.name from the consumed record and emit it for a batch only when all records share the topic. Retain the legacy attribute for existing telemetry consumers. On batches, insert the canonical attribute before the legacy attribute so the latter survives SDK eviction when a process span has room for only two attributes.

OpenTelemetry semantics

The Kafka Process span convention makes messaging.destination.name conditionally required for a single message or when the same value applies to all messages in a batch. The value identifies the Kafka topic. OpenTelemetry deprecated the messaging.destination_publish.* namespace.

FastStream's batch subscriber flattens records across topic partitions. A batch can contain more than one topic, so using the first record's topic for the canonical attribute could mislabel the whole span.

Consumed records Expected canonical topic Regression evidence
Single record The record's topic Exported CONSUMER process span
Same-topic batch The shared topic across partitions Two connected batch cases and a synthetic cross-partition batch
Mixed-topic batch Attribute absent Exported span for orders, payments, orders
Singleton batch The record's topic Exported span with both topic keys
Same-topic batch with two attribute slots Attribute evicted Exported span retains the legacy topic and process operation

Local verification

  • Before the fix, the new single-record and same-topic batch assertions failed with KeyError: 'messaging.destination.name'.
  • All 36 aiokafka and Confluent telemetry tests passed locally with Python 3.13 and Kafka 8.0.0. This includes eight exported batch span cases across both backends.
  • The two-slot aiokafka test reproduced the review regression before the ordering fix. The mixed-topic and singleton tests caught first/last-only topic matching, middleware backfill, and skipping singleton batches in separate local runs.
  • As a downstream check, all four adapter v0.8.1 lifecycle cases passed against this patch with a local assertion for the exported span's exact topic.
  • Ruff lint, formatting, and targeted mypy checks passed for the changed files.

AI assistance was used for research, implementation, and test development.

@github-actions github-actions Bot added the AioKafka Issues related to `faststream.kafka` module label Nov 24, 2026
@lesnik512
lesnik512 marked this pull request as ready for review November 24, 2026 08:29

@Lancetnik Lancetnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please preserve the legacy topic key on same-topic batch spans when the SDK runs out of attribute slots.

One scope question: the Kafka backend rule calls for mirroring these fixes. Confluent still only reports the legacy key for single records and batches. Can we mirror the fix here, or explicitly keep this PR aiokafka-only?

MESSAGING_DESTINATION_PUBLISH_NAME: raw_message.topic,
}

if all(record.topic == raw_message.topic for record in msg.raw_message):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the canonical key before the legacy key, as the single-record provider does. With two orders records from different partitions and SpanLimits(max_span_attributes=2), the base exports messaging.destination_publish.name=orders and messaging.operation=process. This revision exports the canonical key and the operation instead. The SDK drops the older legacy attribute, so existing queries can miss the span.

I reproduced this with OpenTelemetry SDK 1.44.0 and verified that reordering preserves the legacy key under the same limit and exports both keys when there is room. Please pin the two-slot case on the exported process span.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the canonical key ahead of the legacy key in ac2445ce. The regression test checks that the exported process span keeps the legacy topic and operation when the SDK allows only two attributes. With the default limit, both topic keys are present.

Comment thread tests/opentelemetry/kafka/test_kafka.py Outdated
)
message = StreamMessage(raw_message=records, body=[b"message", b"message"])

attrs = BatchKafkaTelemetrySettingsProvider().get_consume_attrs_from_message(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check the exported process span here? The provider test would still pass if the middleware filled in the missing canonical key from the first topic. I tried that change, a first/last-only comparison, and a guard that skips singleton batches separately; all 16 tests passed each time.

With normal attribute limits, [orders:0, payments:1, orders:2] should export no canonical key and keep legacy orders. A singleton batch [orders:0] should export orders under both keys.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests now check exported process spans. [orders, payments, orders] keeps only the legacy topic, and [orders] exports both keys. I tried the three changes you described separately, and each one failed the new tests.

@github-actions github-actions Bot added the Confluent Issues related to `faststream.confluent` module label Nov 25, 2026
@lesnik512

Copy link
Copy Markdown
Contributor Author

I mirrored the fix in Confluent and ran the same batch cases against both backends. Both connected suites passed in CI. CI run

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AioKafka Issues related to `faststream.kafka` module Confluent Issues related to `faststream.confluent` module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants