How to inject an opentracing span into RabbitMQ?
Asked Answered
M

1

8

I´m using OpenTracing and I am trying to propagate a span through RabbitMQ. However I don't understand how I am supposed to inject the span and how to extract it later.

This is the code for sending a message

def send_message(self, message, tracer):
    root_span = tracer.get_span()
    with opentracing.tracer.start_span('Sending message to broker', child_of=root_span) as span:
        connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
        channel = connection.channel()
        channel.queue_declare(queue='default')

        json_message = json.dumps(message)

        channel.basic_publish(exchange='',
                            routing_key='default',
                            body=json_message)

        connection.close()`

And I have a callback function for receiving the message

def _callback(self, ch, method, properties, body):
        print(" [x] Received %r" % body)

So somewhere and somehow I want to inject the span and then extract it. Does anyone know or have any examples on how to do it?

I have tried to inject before calling basic_publish in sender like this

tracer.inject(span, Format.HTTP_HEADERS, headers)

But I have no idea which arguments are going to the inject method.

Then I tried to extract it like this in the callback

span_ctx = tracer.extract(Format.HTTP_HEADERS, {})

Again, I don't know which arguments are going into the extract method.

EDIT: solved, kinda

I solved it by sending the carrier into properties header. Then I could extract the span from the callback properties attribute

In the sender:

channel.basic_publish(exchange='',
                            routing_key='default',                     
                     properties=pika.BasicProperties(headers=carrier),
                            body=json_message)

In the callback, extract span:

def _callback(self, ch, method, properties, body):
    span_ctx = tracer.extract(Format.TEXT_MAP, properties.headers)
Mehalick answered 19/7, 2018 at 13:19 Comment(0)
M
4

I solved it by sending the carrier into properties header. Then I could extract the span from the callback properties attribute

In the sender:

channel.basic_publish(exchange='',
                        routing_key='default',                     
                 properties=pika.BasicProperties(headers=carrier),
                        body=json_message)

In the callback, extract span:

def _callback(self, ch, method, properties, body):
    span_ctx = tracer.extract(Format.TEXT_MAP, properties.headers)
Mehalick answered 20/7, 2018 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.