Exemplo n.º 1
0
def trace_message(message, parent_span=None):
    # type: (ServiceBusMessage, Optional[AbstractSpan]) -> None
    """Add tracing information to this message.
    Will open and close a "Azure.Servicebus.message" span, and
    add the "DiagnosticId" as app properties of the message.
    """
    try:
        span_impl_type = settings.tracing_implementation()  # type: Type[AbstractSpan]
        if span_impl_type is not None:
            current_span = parent_span or span_impl_type(
                span_impl_type.get_current_span()
            )
            link = Link({
                'traceparent': current_span.get_trace_parent()
            })
            with current_span.span(name=SPAN_NAME_MESSAGE, kind=SpanKind.PRODUCER, links=[link]) as message_span:
                message_span.add_attribute(TRACE_NAMESPACE_PROPERTY, TRACE_NAMESPACE)
                # TODO: Remove intermediary message; this is standin while this var is being renamed in a concurrent PR
                if not message.message.application_properties:
                    message.message.application_properties = dict()
                message.message.application_properties.setdefault(
                    TRACE_PARENT_PROPERTY,
                    message_span.get_trace_parent().encode(TRACE_PROPERTY_ENCODING),
                )
    except Exception as exp:  # pylint:disable=broad-except
        _log.warning("trace_message had an exception %r", exp)
Exemplo n.º 2
0
def trace_message(event, parent_span=None):
    # type: (EventData, Optional[AbstractSpan]) -> None
    """Add tracing information to this event.

    Will open and close a "Azure.EventHubs.message" span, and
    add the "DiagnosticId" as app properties of the event.
    """
    try:
        span_impl_type = settings.tracing_implementation(
        )  # type: Type[AbstractSpan]
        if span_impl_type is not None:
            current_span = parent_span or span_impl_type(
                span_impl_type.get_current_span())
            link = Link({'traceparent': current_span.get_trace_parent()})
            with current_span.span(name="Azure.EventHubs.message",
                                   kind=SpanKind.PRODUCER,
                                   links=[link]) as message_span:
                message_span.add_attribute("az.namespace",
                                           "Microsoft.EventHub")
                if not event.properties:
                    event.properties = dict()
                event.properties.setdefault(
                    b"Diagnostic-Id",
                    message_span.get_trace_parent().encode("ascii"))
    except Exception as exp:  # pylint:disable=broad-except
        _LOGGER.warning("trace_message had an exception %r", exp)
 def test_passing_links_in_ctor(self):
     with ContextHelper() as ctx:
         trace = tracer_module.Tracer(sampler=AlwaysOnSampler())
         parent = trace.start_span()
         wrapped_class = OpenCensusSpan(links=[
             Link(
                 headers={
                     "traceparent":
                     "00-2578531519ed94423ceae67588eff2c9-231ebdc614cb9ddd-01"
                 })
         ])
         assert len(wrapped_class.span_instance.links) == 1
         link = wrapped_class.span_instance.links[0]
         assert link.trace_id == "2578531519ed94423ceae67588eff2c9"
         assert link.span_id == "231ebdc614cb9ddd"
Exemplo n.º 4
0
def get_event_links(events):
    trace_events = events if isinstance(events, Iterable) else (events, )  # pylint:disable=isinstance-second-argument-not-valid-type
    links = []
    try:
        for event in trace_events:  # type: ignore
            if event.properties:
                traceparent = event.properties.get(b"Diagnostic-Id",
                                                   "").decode("ascii")
                if traceparent:
                    links.append(
                        Link({'traceparent': traceparent},
                             attributes={
                                 "enqueuedTime":
                                 event.message.annotations.get(PROP_TIMESTAMP)
                             }))
    except AttributeError:
        pass
    return links
Exemplo n.º 5
0
def get_receive_links(messages):
    trace_messages = (
        messages if isinstance(messages, Iterable)  # pylint:disable=isinstance-second-argument-not-valid-type
        else (messages, ))

    links = []
    try:
        for message in trace_messages:  # type: ignore
            if message.message.application_properties:
                traceparent = message.message.application_properties.get(
                    TRACE_PARENT_PROPERTY, "").decode(TRACE_PROPERTY_ENCODING)
                if traceparent:
                    links.append(
                        Link({'traceparent': traceparent}, {
                            SPAN_ENQUEUED_TIME_PROPERTY:
                            message.message.annotations.get(
                                TRACE_ENQUEUED_TIME_PROPERTY)
                        }))
    except AttributeError:
        pass
    return links