29 lines
1.4 KiB
Python
29 lines
1.4 KiB
Python
import os, json, ssl
|
|
import pika
|
|
from opentelemetry import trace
|
|
from telemetry import propagator
|
|
QUEUE = 'attachment.jobs'
|
|
def connect():
|
|
p = pika.URLParameters(os.environ['BROKER_URL'])
|
|
ctx = ssl.create_default_context(cafile='/tls/ca.crt')
|
|
p.ssl_options = pika.SSLOptions(ctx, p.host)
|
|
p.socket_timeout = 5
|
|
p.blocked_connection_timeout = 10
|
|
c = pika.BlockingConnection(p)
|
|
ch = c.channel()
|
|
ch.queue_declare(queue=QUEUE, durable=True)
|
|
ch.confirm_delivery()
|
|
return c, ch
|
|
|
|
def publish(ch, job):
|
|
payload=dict(schema_version=1,job_id=str(job['id']),type='attachment.process.v1',attachment_id=str(job['attachment_id']),pipeline_version=1,attempt=job['attempt'],created_at=job['created_at'].isoformat())
|
|
carrier={}
|
|
if job.get('traceparent'): carrier['traceparent']=job['traceparent']
|
|
if job.get('tracestate'): carrier['tracestate']=job['tracestate']
|
|
ctx=propagator.extract(carrier)
|
|
with trace.get_tracer('messenger.dispatcher').start_as_current_span('queue.publish', context=ctx) as span:
|
|
headers={}; propagator.inject(headers)
|
|
span.set_attribute('messaging.system','rabbitmq')
|
|
span.set_attribute('messaging.destination.name',QUEUE)
|
|
ch.basic_publish('',QUEUE,json.dumps(payload).encode(),properties=pika.BasicProperties(delivery_mode=2,content_type='application/json',headers=headers),mandatory=True)
|