如何将 opentracing 跨度注入 RabbitMQ?

Bol*_*oom 6 inject rabbitmq opentracing

我正在使用 OpenTracing,并且正在尝试通过 RabbitMQ 传播跨度。但是我不明白我应该如何注入跨度以及如何在以后提取它。

这是发送消息的代码

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()`
Run Code Online (Sandbox Code Playgroud)

我有一个用于接收消息的回调函数

def _callback(self, ch, method, properties, body):
        print(" [x] Received %r" % body)
Run Code Online (Sandbox Code Playgroud)

所以在某个地方,我想以某种方式注入跨度,然后提取它。有没有人知道或有任何关于如何做的例子?

我曾尝试在像这样在发件人中调用 basic_publish 之前注入

tracer.inject(span, Format.HTTP_HEADERS, headers)
Run Code Online (Sandbox Code Playgroud)

但我不知道哪些参数将进入注入方法。

然后我尝试在回调中像这样提取它

span_ctx = tracer.extract(Format.HTTP_HEADERS, {})
Run Code Online (Sandbox Code Playgroud)

同样,我不知道哪些参数会进入提取方法。

编辑:解决了,有点

我通过将载体发送到属性标题来解决它。然后我可以从回调属性属性中提取跨度

在发件人中:

channel.basic_publish(exchange='',
                            routing_key='default',                     
                     properties=pika.BasicProperties(headers=carrier),
                            body=json_message)
Run Code Online (Sandbox Code Playgroud)

在回调中,提取跨度:

def _callback(self, ch, method, properties, body):
    span_ctx = tracer.extract(Format.TEXT_MAP, properties.headers)
Run Code Online (Sandbox Code Playgroud)

Bol*_*oom 3

我通过将运营商发送到属性标头中解决了这个问题。然后我可以从回调属性中提取跨度

在发件人中:

channel.basic_publish(exchange='',
                        routing_key='default',                     
                 properties=pika.BasicProperties(headers=carrier),
                        body=json_message)
Run Code Online (Sandbox Code Playgroud)

在回调中,提取跨度:

def _callback(self, ch, method, properties, body):
    span_ctx = tracer.extract(Format.TEXT_MAP, properties.headers)
Run Code Online (Sandbox Code Playgroud)