def decorated_callback(
     channel: Channel,
     method: Basic.Deliver,
     properties: BasicProperties,
     body: bytes,
 ) -> Any:
     if not properties:
         properties = BasicProperties(headers={})
     if properties.headers is None:
         properties.headers = {}
     ctx = propagate.extract(properties.headers, getter=_pika_getter)
     if not ctx:
         ctx = context.get_current()
     token = context.attach(ctx)
     span = _get_span(
         tracer,
         channel,
         properties,
         destination=method.exchange
         if method.exchange else method.routing_key,
         span_kind=SpanKind.CONSUMER,
         task_name=task_name,
         operation=MessagingOperationValues.RECEIVE,
     )
     try:
         with trace.use_span(span, end_on_exit=True):
             try:
                 consume_hook(span, body, properties)
             except Exception as hook_exception:  # pylint: disable=W0703
                 _LOG.exception(hook_exception)
             retval = callback(channel, method, properties, body)
     finally:
         context.detach(token)
     return retval
 def decorated_function(
     exchange: str,
     routing_key: str,
     body: bytes,
     properties: BasicProperties = None,
     mandatory: bool = False,
 ) -> Any:
     if not properties:
         properties = BasicProperties(headers={})
     if properties.headers is None:
         properties.headers = {}
     span = _get_span(
         tracer,
         channel,
         properties,
         destination=exchange if exchange else routing_key,
         span_kind=SpanKind.PRODUCER,
         task_name="(temporary)",
         operation=None,
     )
     if not span:
         return original_function(exchange, routing_key, body, properties,
                                  mandatory)
     with trace.use_span(span, end_on_exit=True):
         if span.is_recording():
             propagate.inject(properties.headers)
             try:
                 publish_hook(span, body, properties)
             except Exception as hook_exception:  # pylint: disable=W0703
                 _LOG.exception(hook_exception)
         retval = original_function(exchange, routing_key, body, properties,
                                    mandatory)
     return retval
Example #3
0
    def _build_basic_properties(self, properties):
        """
        Get the pika.BasicProperties from a pikachewie.data.Properties object.

        :param pikachewie.data.Properties properties: properties to convert
        :rtype: pika.spec.BasicProperties

        """
        basic_properties = BasicProperties()
        attrs = '''
            app_id
            content_encoding
            content_type
            correlation_id
            delivery_mode
            priority
            reply_to
            message_id
            type
            user_id
        '''.split()
        for attr in attrs:
            value = getattr(properties, attr)
            if value is not None:
                setattr(basic_properties, attr, value)

        basic_properties.timestamp = properties.timestamp or int(time.time())
        if properties.expiration is not None:
            basic_properties.expiration = str(properties.expiration)
        if properties.headers is not None and \
                len(properties.headers.keys()):
            basic_properties.headers = dict(properties.headers)

        return basic_properties