Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject an opentracing span into RabbitMQ?

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)
like image 768
BoliBoom Avatar asked Jul 19 '18 13:07

BoliBoom


1 Answers

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)
like image 99
BoliBoom Avatar answered Jan 04 '23 07:01

BoliBoom