Skip to content

deep.api.plugin.otel

Provide plugin for Deep to connect to OTEL.

OTelPlugin

Bases: ResourceProvider, SnapshotDecorator, SpanProcessor

Deep Otel plugin.

Provide span and trace information to the snapshot.

Source code in deep/api/plugin/otel.py
class OTelPlugin(ResourceProvider, SnapshotDecorator, SpanProcessor):
    """
    Deep Otel plugin.

    Provide span and trace information to the snapshot.
    """

    def create_span(self, name: str, context_id: str, tracepoint_id: str) -> Optional['Span']:
        """
        Create and return a new span.

        :param name: the name of the span to create
        :param context_id: the id of the context
        :param tracepoint_id: the id of thr tracepoint
        :return: the created span
        """
        span = trace.get_tracer("deep").start_as_current_span(name,
                                                              end_on_exit=False,
                                                              attributes={'dynamic': 'deep',
                                                                          'context': context_id,
                                                                          "tracepoint": tracepoint_id
                                                                          },
                                                              )
        if span:
            # noinspection PyUnresolvedReferences
            # this is a generator contextlib._GeneratorContextManager
            current = span.__enter__()
            if isinstance(current, _Span):
                return _OtelSpan(current)
        return None

    def current_span(self) -> Optional['Span']:
        """
        Get the current span from the underlying provider.

        :return: the current span
        """
        span = self.__get_span()
        if span:
            return _OtelSpan(span)
        return None

    def resource(self) -> Optional[Resource]:
        """
        Provide resource.

        :return: the provided resource
        """
        provider = trace.get_tracer_provider()
        if isinstance(provider, TracerProvider):
            # noinspection PyUnresolvedReferences
            resource = provider.resource
            attributes = dict(resource.attributes)
            return Resource(attributes=attributes)
        return None

    def decorate(self, snapshot_id: str, context: ActionContext) -> Optional[BoundedAttributes]:
        """
        Decorate a snapshot with additional data.

        :param snapshot_id: the id of the collected snapshot
        :param context: the action context for this action

        :return: the additional attributes to attach
        """
        span = self.current_span()
        if span is not None:
            span.add_event(context.location_action.location.name,
                           attributes={"snapshot": snapshot_id,
                                       "tracepoint": context.location_action.id,
                                       "context": context.trigger_context.id})
            return BoundedAttributes(attributes={
                "span_name": span.name,
                "trace_id": span.trace_id,
                "span_id": span.span_id
            })
        return None

    @staticmethod
    def __get_span() -> Optional[_Span]:
        span = trace.get_current_span()
        if isinstance(span, _Span):
            return span
        return None

create_span(name, context_id, tracepoint_id)

Create and return a new span.

:param name: the name of the span to create :param context_id: the id of the context :param tracepoint_id: the id of thr tracepoint :return: the created span

Source code in deep/api/plugin/otel.py
def create_span(self, name: str, context_id: str, tracepoint_id: str) -> Optional['Span']:
    """
    Create and return a new span.

    :param name: the name of the span to create
    :param context_id: the id of the context
    :param tracepoint_id: the id of thr tracepoint
    :return: the created span
    """
    span = trace.get_tracer("deep").start_as_current_span(name,
                                                          end_on_exit=False,
                                                          attributes={'dynamic': 'deep',
                                                                      'context': context_id,
                                                                      "tracepoint": tracepoint_id
                                                                      },
                                                          )
    if span:
        # noinspection PyUnresolvedReferences
        # this is a generator contextlib._GeneratorContextManager
        current = span.__enter__()
        if isinstance(current, _Span):
            return _OtelSpan(current)
    return None

current_span()

Get the current span from the underlying provider.

:return: the current span

Source code in deep/api/plugin/otel.py
def current_span(self) -> Optional['Span']:
    """
    Get the current span from the underlying provider.

    :return: the current span
    """
    span = self.__get_span()
    if span:
        return _OtelSpan(span)
    return None

decorate(snapshot_id, context)

Decorate a snapshot with additional data.

:param snapshot_id: the id of the collected snapshot :param context: the action context for this action

:return: the additional attributes to attach

Source code in deep/api/plugin/otel.py
def decorate(self, snapshot_id: str, context: ActionContext) -> Optional[BoundedAttributes]:
    """
    Decorate a snapshot with additional data.

    :param snapshot_id: the id of the collected snapshot
    :param context: the action context for this action

    :return: the additional attributes to attach
    """
    span = self.current_span()
    if span is not None:
        span.add_event(context.location_action.location.name,
                       attributes={"snapshot": snapshot_id,
                                   "tracepoint": context.location_action.id,
                                   "context": context.trigger_context.id})
        return BoundedAttributes(attributes={
            "span_name": span.name,
            "trace_id": span.trace_id,
            "span_id": span.span_id
        })
    return None

resource()

Provide resource.

:return: the provided resource

Source code in deep/api/plugin/otel.py
def resource(self) -> Optional[Resource]:
    """
    Provide resource.

    :return: the provided resource
    """
    provider = trace.get_tracer_provider()
    if isinstance(provider, TracerProvider):
        # noinspection PyUnresolvedReferences
        resource = provider.resource
        attributes = dict(resource.attributes)
        return Resource(attributes=attributes)
    return None