1from typing import Optional 2from typing import TYPE_CHECKING 3from typing import Tuple 4 5import ddtrace 6from ddtrace.internal.utils.deprecation import deprecated 7 8 9if TYPE_CHECKING: 10 from ddtrace.tracer import Tracer 11 12 13@deprecated("This method and module will be removed altogether", "1.0.0") 14def get_correlation_ids(tracer=None): 15 # type: (Optional[Tracer]) -> Tuple[Optional[int], Optional[int]] 16 """Retrieves the Correlation Identifiers for the current active ``Trace``. 17 This helper method can be achieved manually and should be considered 18 only a shortcut. The main reason is to abstract the current ``Tracer`` 19 implementation so that these identifiers can be extracted either the 20 tracer is an OpenTracing tracer or a Datadog tracer. 21 22 OpenTracing users can still extract these values using the ``ScopeManager`` 23 API, though this shortcut is a simple one-liner. The usage is: 24 25 from ddtrace import helpers 26 27 trace_id, span_id = helpers.get_correlation_ids() 28 29 :returns: a tuple containing the trace_id and span_id 30 """ 31 # Consideration: currently we don't have another way to "define" a 32 # GlobalTracer. In the case of OpenTracing, ``opentracing.tracer`` is exposed 33 # and we're doing the same here for ``ddtrace.tracer``. Because this helper 34 # must work also with OpenTracing, we should take the right used ``Tracer``. 35 # At the time of writing, it's enough to support our Datadog Tracer. 36 37 # If no tracer passed in, use global tracer 38 if not tracer: 39 tracer = ddtrace.tracer 40 41 # If tracer is disabled, skip 42 if not tracer.enabled: 43 return None, None 44 45 span = tracer.current_span() 46 if not span: 47 return None, None 48 return span.trace_id, span.span_id 49