fix(RHINENG-31023): keep consumer spans active - #2520
Open
rodrigonull wants to merge 2 commits into
Open
rodrigonull wants to merge 2 commits into
rodrigonull wants to merge 2 commits into
Conversation
Reviewer's GuideThe PR fixes prematurely terminated Kafka consumer traces by creating listener and grouper consumer spans around the actual awaited processing work, carrying extracted context through queued items, and awaiting outbound Kafka futures so producer and database-related child operations remain correctly nested until completion. New tracing tests validate span lifetimes, hierarchy, attributes, and context propagation. Sequence diagram for listener consumer span lifetimesequenceDiagram
participant Kafka
participant Listener
participant Processor
participant Grouper
participant PayloadTracker
Kafka->>Listener: _consume_message
Listener->>Listener: consume_inventory_msg
activate Listener
Listener->>Processor: _consume_inventory_msg
activate Processor
Processor->>Grouper: _send_for_evaluation
Processor->>PayloadTracker: _send_to_payload_tracker
Processor-->>Processor: await outbound futures
deactivate Processor
Listener-->>Listener: release semaphore
deactivate Listener
Sequence diagram for grouper queued processing span lifetimesequenceDiagram
participant Kafka
participant Grouper
participant Queue
participant Evaluator
participant PayloadTracker
Kafka->>Grouper: consume_message
Grouper->>Queue: push_inventory_msg or push_advisor_msg
Note over Queue: QueueItem stores otel_context and topic
Queue->>Queue: _start_item_processing
activate Queue
Queue-->>Queue: await pair event or timeout
Queue->>PayloadTracker: send_msg_to_payload_tracker
Queue->>Evaluator: send
Queue-->>Queue: await tracker and evaluator futures
deactivate Queue
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/grouper_tests/test_grouper_tracing.py" line_range="85" />
<code_context>
+ topic="platform.vulnerability.inventory-upload",
+ )
+
+ # Let the background task run to completion
+ await asyncio.sleep(0.05)
+
</code_context>
<issue_to_address>
**issue (testing):** The test assumes the background grouper task completes within a fixed 50ms sleep and then immediately indexes `active_checks`; under CI scheduling or load, the task has not run yet and the test raises `KeyError` or observes incomplete state.
**Triggers:** When the test process is delayed for more than 50ms.
**Suggested fix:** Synchronize on an explicit completion event or await the task returned by `create_task_and_log` instead of sleeping for a fixed duration.
</issue_to_address>The Kafka consumer spans in listener and grouper closed prematurely at scheduling time (~50-300µs) because the span wrapped only the task scheduling. Descendant operations (DB queries and Kafka sends) ran in background tasks under the already-ended parent span. Move consumer spans into the awaited coroutines: - listener: wrap consume_inventory_msg and consume_advisor_msg - grouper: wrap _start_item_processing - await outgoing producer sends in listener and grouper so they finish before consumer spans close Assisted-by: Cursor:gemini-3.8-flash
rodrigonull
force-pushed
the
RHINENG-31023
branch
from
September 15, 2026 12:50
9e1eabe to
9990161
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Jira
RHINENG-31023
What
Move OpenTelemetry Kafka consumer spans in the vulnerability listener and grouper into the awaited processing coroutines so spans remain active until message processing and outgoing operations complete.
Why
In both the listener and grouper, the consumer spans (
process <topic>) were previously opened around fire-and-forget task scheduling and enqueue calls, causing spans to end prematurely in ~50–300µs. Descendant operations (such as psycopg3 DB transactions and aiokafka sends) ran in background tasks under an already-ended parent span, violating the OpenTelemetry span lifecycle model. This distorted Tempo's service-graph metrics (server_secondsreported ≤5ms for ~99.98% of requests instead of their true 10–20ms duration).How
listener/listener.py): Removed span creation from_consume_messageat scheduling time. Startedprocess <topic>CONSUMER spans insideconsume_inventory_msgandconsume_advisor_msgwrapping the awaited_consume_*_msgcoroutines while preservingrh.org_id,rh.request_id, andthreadctxcontext variables.grouper/grouper.py,grouper/queue.py,grouper/common.py): Removed the span wrapping only enqueue inconsume_message. Wrapped_start_item_processinginprocess <topic>using the OTel context stored onQueueItem, covering pair waiting/timeout, semaphore release, and evaluator sends.listener/*_processor.py,grouper/queue.py,common/utils.py): Returned send futures fromsend_msg_to_payload_trackerand awaited outgoing sends so producer child spans complete before the parent consumer span closes.Testing
tests/listener_tests/test_listener_tracing.pyandtests/grouper_tests/test_grouper_tracing.pyasserting that consumer spans remain active during processing, do not close at scheduling time, and outlive all child spans (parent.end_time >= child.end_time).Ingress->Puptoo->HBI->vulnerability-engine-listener->vulnerability-engine-grouper->vulnerability-engine-evaluator):pre-commitpassed (black,flake8,isort, whitespace).Secure Coding Practices Checklist GitHub Link
Secure Coding Checklist
Summary by Sourcery
Keep consumer tracing spans open until message processing and downstream sends complete.
Bug Fixes:
Enhancements:
Tests: