-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRabbitMQ.py
More file actions
33 lines (26 loc) · 1.32 KB
/
RabbitMQ.py
File metadata and controls
33 lines (26 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from contextlib import contextmanager
import pika
from .tracer import RequestIdContext, trace_id_ctx
@contextmanager
def rabbitmq_trace_context(channel, properties):
request_id = properties.headers.get("X-Request-ID") if properties.headers else None
with RequestIdContext(request_id):
yield
if properties.headers is None:
properties.headers = {}
properties.headers["X-Request-ID"] = trace_id_ctx.get()
class RabbitMQMiddleware:
def __init__(self, connection_parameters):
self.connection = pika.BlockingConnection(connection_parameters)
self.channel = self.connection.channel()
def publish(self, exchange, routing_key, body, properties=None):
if properties is None:
properties = pika.BasicProperties()
with rabbitmq_trace_context(self.channel, properties):
self.channel.basic_publish(exchange=exchange, routing_key=routing_key, body=body, properties=properties)
def consume(self, queue, on_message_callback, auto_ack=False):
def callback(ch, method, properties, body):
with rabbitmq_trace_context(ch, properties):
on_message_callback(ch, method, properties, body)
self.channel.basic_consume(queue=queue, on_message_callback=callback, auto_ack=auto_ack)
self.channel.start_consuming()