Added injetion to outgoing messages
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pünkösd Marcell 2021-08-10 15:13:23 +02:00
parent 00e9d02478
commit 9c3f8c65fb
2 changed files with 44 additions and 33 deletions

View File

@ -5,6 +5,10 @@ import pika.exceptions
import json import json
import time import time
import opentracing
from opentracing.ext import tags
from opentracing.propagation import Format
class MagicAMQP: class MagicAMQP:
""" """
@ -72,8 +76,12 @@ class MagicAMQP:
Publish a simple json serialized message to the configured queue. Publish a simple json serialized message to the configured queue.
If the connection is broken, then this call will block until the connection is restored If the connection is broken, then this call will block until the connection is restored
""" """
span_tags = {tags.SPAN_KIND: tags.SPAN_KIND_PRODUCER}
with opentracing.tracer.start_active_span('magic_amqp.publish', tags=span_tags) as scope:
opentracing.tracer.inject(scope.span.context, Format.TEXT_MAP, payload)
lock_start = time.time() lock_start = time.time()
with self._lock: with self._lock:
scope.span.log_kv({'event': 'lockAcquired'})
lock_acquire_time = time.time() - lock_start lock_acquire_time = time.time() - lock_start
if lock_acquire_time >= 0.2: if lock_acquire_time >= 0.2:
self.app.logger.warning(f"Publish: Lock acquire took {lock_acquire_time:5f} sec") self.app.logger.warning(f"Publish: Lock acquire took {lock_acquire_time:5f} sec")
@ -85,8 +93,10 @@ class MagicAMQP:
routing_key='feature', routing_key='feature',
body=json.dumps(payload).encode('UTF-8') body=json.dumps(payload).encode('UTF-8')
) )
self.app.logger.debug(f"Published: {payload}")
break # message sent successfully break # message sent successfully
except pika.exceptions.AMQPConnectionError as e: except pika.exceptions.AMQPConnectionError as e:
scope.span.log_kv({'event': 'connectionError', 'error': str(e)})
self.app.logger.warning(f"Connection error during publish: {e} (attempting reconnect)") self.app.logger.warning(f"Connection error during publish: {e} (attempting reconnect)")
if tries > 30: if tries > 30:
@ -97,7 +107,8 @@ class MagicAMQP:
self._reconnect_ampq() self._reconnect_ampq()
break break
except pika.exceptions.AMQPConnectionError as e: except pika.exceptions.AMQPConnectionError as e:
self.app.logger.warning(f"Connection error during reconnection: {e} (attempting reconnect)") self.app.logger.warning(
f"Connection error during reconnection: {e} (attempting reconnect)")
tries += 1 tries += 1
if tries > 30: if tries > 30:

View File

@ -119,7 +119,7 @@ class SampleResource(Resource):
db.session.commit() db.session.commit()
# Announce only after the data is successfully committed # Announce only after the data is successfully committed
with opentracing.tracer.start_active_span('amqp.publish'): with opentracing.tracer.start_active_span('publishMessage'):
try: try:
magic_amqp.publish({'tag': generated_tag}) magic_amqp.publish({'tag': generated_tag})
except Exception as e: except Exception as e: