Skip to content

[Bug]: aio-pika wrapper does not trace consumer callback #794

@tobmi1

Description

@tobmi1

Problem Description

Hi,
we are using aio-pika and RabbitMQ to realize a federated master-worker pattern where a single master puts tasks on a queue and multiple workers consume those tasks via a callback. Ever since support for aio-pika was added to the instana python sensor, we were able to see events for tasks being published and consumed in our traces (which is already pretty awesome ☺️).

However, any further calls made in the consumer callback method do not appear in the trace. I believe this could be due to a bug in the instana wrapper implementation:

with tracer.start_as_current_span(
"rabbitmq", span_context=parent_context
) as span:
_extract_span_attributes(
span, connection, "consume", message.routing_key, message.exchange
)
try:
response = await wrapped(*args, **kwargs)
except Exception as exc:
span.record_exception(exc)
else:
return response

The code that actually executes the consumer callback wrapper (l. 104) is not contained in the with statement that constitutes the span.
Compare this to the regular pika wrapper for basic_consume:
with tracer.start_as_current_span(
"rabbitmq", span_context=parent_context
) as span:
try:
_extract_consumer_tags(
span, conn=instance.connection._impl, queue=queue
)
except Exception:
logger.debug(
"pika basic_consume_with_instana error:", exc_info=True
)
try:
on_message_callback(channel, method, properties, body)
except Exception as exc:
span.record_exception(exc)

Here, the callback is executed inside the with statement.

Since the fix should be pretty straightforward, I'll create a PR for this issue and link it here once available.

Minimal, Complete, Verifiable, Example

My PR will include tests to demonstrate the behaviour

Python Version

3.12

Python Modules

N/A

Python Environment

N/A

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions