|
| 1 | +# 3p |
| 2 | +import kombu |
| 3 | +import wrapt |
| 4 | + |
| 5 | +# project |
| 6 | +from ddtrace import config |
| 7 | + |
| 8 | +from ...pin import Pin |
| 9 | +from ...utils.formats import get_env |
| 10 | +from .constants import DEFAULT_SERVICE |
| 11 | +from ...ext import kombu as kombux |
| 12 | +from ...ext import AppTypes |
| 13 | +from ...utils.wrappers import unwrap |
| 14 | +from ...propagation.http import HTTPPropagator |
| 15 | +from .utils import ( |
| 16 | + get_exchange_from_args, |
| 17 | + get_body_length_from_args, |
| 18 | + get_routing_key_from_args, |
| 19 | + extract_conn_tags, |
| 20 | + HEADER_POS |
| 21 | +) |
| 22 | + |
| 23 | +# kombu default settings |
| 24 | +config._add('kombu',{ |
| 25 | + 'service_name': get_env('kombu', 'service_name', DEFAULT_SERVICE) |
| 26 | +}) |
| 27 | + |
| 28 | +propagator = HTTPPropagator() |
| 29 | + |
| 30 | + |
| 31 | +def patch(): |
| 32 | + """Patch the instrumented methods |
| 33 | +
|
| 34 | + This duplicated doesn't look nice. The nicer alternative is to use an ObjectProxy on top |
| 35 | + of Kombu. However, it means that any "import kombu.Connection" won't be instrumented. |
| 36 | + """ |
| 37 | + if getattr(kombu, '_datadog_patch', False): |
| 38 | + return |
| 39 | + setattr(kombu, '_datadog_patch', True) |
| 40 | + |
| 41 | + _w = wrapt.wrap_function_wrapper |
| 42 | + # We wrap the _publish method because the publish method: |
| 43 | + # * defines defaults in its kwargs |
| 44 | + # * potentially overrides kwargs with values from self |
| 45 | + # * extracts/normalizes things like exchange |
| 46 | + _w(kombux.TYPE, 'Producer._publish', traced_publish) |
| 47 | + _w(kombux.TYPE, 'Consumer.receive', traced_receive) |
| 48 | + Pin( |
| 49 | + service=config.kombu['service_name'], |
| 50 | + app='kombu', |
| 51 | + app_type=AppTypes.worker, |
| 52 | + ).onto(kombu.messaging.Producer) |
| 53 | + |
| 54 | + Pin( |
| 55 | + service=config.kombu['service_name'], |
| 56 | + app='kombu', |
| 57 | + app_type=AppTypes.worker, |
| 58 | + ).onto(kombu.messaging.Consumer) |
| 59 | + |
| 60 | + |
| 61 | +def unpatch(): |
| 62 | + if getattr(kombu, '_datadog_patch', False): |
| 63 | + setattr(kombu, '_datadog_patch', False) |
| 64 | + unwrap(kombu.Producer, '_publish') |
| 65 | + unwrap(kombu.Consumer, 'receive') |
| 66 | + |
| 67 | +# |
| 68 | +# tracing functions |
| 69 | +# |
| 70 | + |
| 71 | + |
| 72 | +def traced_receive(func, instance, args, kwargs): |
| 73 | + pin = Pin.get_from(instance) |
| 74 | + if not pin or not pin.enabled(): |
| 75 | + return func(*args, **kwargs) |
| 76 | + |
| 77 | + # Signature only takes 2 args: (body, message) |
| 78 | + message = args[1] |
| 79 | + context = propagator.extract(message.headers) |
| 80 | + # only need to active the new context if something was propagated |
| 81 | + if context.trace_id: |
| 82 | + pin.tracer.context_provider.activate(context) |
| 83 | + with pin.tracer.trace(kombux.RECEIVE_NAME, service=pin.service, span_type='kombu') as s: |
| 84 | + # run the command |
| 85 | + exchange = message.delivery_info['exchange'] |
| 86 | + s.resource = exchange |
| 87 | + s.set_tag(kombux.EXCHANGE, exchange) |
| 88 | + |
| 89 | + s.set_tags(extract_conn_tags(message.channel.connection)) |
| 90 | + s.set_tag(kombux.ROUTING_KEY, message.delivery_info['routing_key']) |
| 91 | + return func(*args, **kwargs) |
| 92 | + |
| 93 | + |
| 94 | +def traced_publish(func, instance, args, kwargs): |
| 95 | + pin = Pin.get_from(instance) |
| 96 | + if not pin or not pin.enabled(): |
| 97 | + return func(*args, **kwargs) |
| 98 | + |
| 99 | + with pin.tracer.trace(kombux.PUBLISH_NAME, service=pin.service, span_type='kombu') as s: |
| 100 | + exchange_name = get_exchange_from_args(args) |
| 101 | + s.resource = exchange_name |
| 102 | + s.set_tag(kombux.EXCHANGE, exchange_name) |
| 103 | + if pin.tags: |
| 104 | + s.set_tags(pin.tags) |
| 105 | + s.set_tag(kombux.ROUTING_KEY, get_routing_key_from_args(args)) |
| 106 | + s.set_tags(extract_conn_tags(instance.channel.connection)) |
| 107 | + s.set_metric(kombux.BODY_LEN, get_body_length_from_args(args)) |
| 108 | + # run the command |
| 109 | + propagator.inject(s.context, args[HEADER_POS]) |
| 110 | + return func(*args, **kwargs) |
0 commit comments