# How to monitor transactions which pass through Rabbit MQ, producer & consumer are python clients

**URL:** <https://discuss.elastic.co/t/how-to-monitor-transactions-which-pass-through-rabbit-mq-producer-consumer-are-python-clients/332647>\
**Category:** APM\
**Tags:** python\
**Created:** [May 5, 2023, 12:29pm UTC](https://discuss.elastic.co/t/how-to-monitor-transactions-which-pass-through-rabbit-mq-producer-consumer-are-python-clients/332647 "2023-05-05T12:29:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [May 5, 2023, 12:29pm UTC](https://discuss.elastic.co/t/how-to-monitor-transactions-which-pass-through-rabbit-mq-producer-consumer-are-python-clients/332647/1 "2023-05-05T12:29:18Z")

</div>

Hi All,

I want to "trace" distributed transactions which pass through "Rabbit MQ" through the Application. The clients for Rabbit MQ are written in python 2.x & Python 3,.x to consume and produce messages.

When in Elastic APM, how do we configure/add/span which shows the "Request -calls which pass through a queue and being processed by the consumer".

Entire lifecycle of the request -\> Application \> some processing in the server -\> generates message in RMQ - \> Consumers consume the message.

Each message should be considered as transaction and latency should be calculated.

Even the Request where it went and to which queue ?

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [May 5, 2023, 5:12pm UTC](https://discuss.elastic.co/t/how-to-monitor-transactions-which-pass-through-rabbit-mq-producer-consumer-are-python-clients/332647/2 "2023-05-05T17:12:40Z")

</div>

We unfortunately don't have support for RabbitMQ yet. We have an open issue, please express your interest there -- it helps us prioritize: [Add instrumentation and distributed tracing for RabbitMQ (pika) · Issue #678 · elastic/apm-agent-python · GitHub](https://github.com/elastic/apm-agent-python/issues/678)

If you want to tackle this as a custom instrumentation for your code, you'll need to add the TraceParent to your messages. You should be able to use headers, the same as we do in our kafka instrumentation. You can get the TraceParent string for the current transaction with `elasticapm.get_trace_parent_header()`.

Then, on the receiver end, you can start a new transaction using a TraceParent object from `elasticapm.trace_parent_from_string()`.

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [May 5, 2023, 5:50pm UTC](https://discuss.elastic.co/t/how-to-monitor-transactions-which-pass-through-rabbit-mq-producer-consumer-are-python-clients/332647/3 "2023-05-05T17:50:45Z")

</div>

Thank you Colton !!

If you have an example please share , a pseudo code.

If we have already instrumented and have spans across the distributed application then how do I wire the transaction from there all along to Rabbit mq queue ( producer & consumer)

I know you are very busy guy , however some light would help me

Thanks

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [May 8, 2023, 6:26pm UTC](https://discuss.elastic.co/t/how-to-monitor-transactions-which-pass-through-rabbit-mq-producer-consumer-are-python-clients/332647/4 "2023-05-08T18:26:27Z")

</div>

I haven't done anything in rabbitmq, so I can't help with that piece at this moment.

But something like this on the producer:

```python
headers = {"traceparent": elasticapm.get_trace_parent_header()}
rabbitmq.queue(message, headers=headers) # I'm making this line up

```

On the consumer:

```python
def consume(message, headers, client):
    traceparent = ealsticapm.trace_parent_from_string(headers.get("traceparent", ""))
    if traceparent:
        client.begin_transaction(transaction_type="messaging", trace_parent=traceparent)
    else:
        client.begin_transaction(transaction_type="messaging")

```

Note that you can also use span links if you're already in an active transaction in your consumer. This is where you link a span to a parent transaction so that they're tied together in the trace view, even though it's not a direct parent/child relationship. [There's a `links` argument in the capture\_span context manager.](https://www.elastic.co/guide/en/apm/agent/python/current/api.html#api-capture-span)

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [May 17, 2023, 11:03am UTC](https://discuss.elastic.co/t/how-to-monitor-transactions-which-pass-through-rabbit-mq-producer-consumer-are-python-clients/332647/5 "2023-05-17T11:03:30Z")

</div>

Let me try . Submitted interest in github for RMQ - Pika library instrumentation here - [Add instrumentation and distributed tracing for RabbitMQ (pika) · Issue #678 · elastic/apm-agent-python · GitHub](https://github.com/elastic/apm-agent-python/issues/678)

Thanks basepi

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 7, 2023, 7:04am UTC](https://discuss.elastic.co/t/how-to-monitor-transactions-which-pass-through-rabbit-mq-producer-consumer-are-python-clients/332647/6 "2023-06-07T07:04:23Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
