|
| 1 | +from ..context import Context |
| 2 | +from ..internal.logger import get_logger |
| 3 | + |
| 4 | +from .utils import get_wsgi_header |
| 5 | + |
| 6 | +log = get_logger(__name__) |
| 7 | + |
| 8 | +# HTTP headers one should set for distributed tracing. |
| 9 | +# These are cross-language (eg: Python, Go and other implementations should honor these) |
| 10 | +HTTP_HEADER_TRACE_ID = 'x-datadog-trace-id' |
| 11 | +HTTP_HEADER_PARENT_ID = 'x-datadog-parent-id' |
| 12 | +HTTP_HEADER_SAMPLING_PRIORITY = 'x-datadog-sampling-priority' |
| 13 | +HTTP_HEADER_ORIGIN = 'x-datadog-origin' |
| 14 | + |
| 15 | + |
| 16 | +# Note that due to WSGI spec we have to also check for uppercased and prefixed |
| 17 | +# versions of these headers |
| 18 | +POSSIBLE_HTTP_HEADER_TRACE_IDS = frozenset( |
| 19 | + [HTTP_HEADER_TRACE_ID, get_wsgi_header(HTTP_HEADER_TRACE_ID)] |
| 20 | +) |
| 21 | +POSSIBLE_HTTP_HEADER_PARENT_IDS = frozenset( |
| 22 | + [HTTP_HEADER_PARENT_ID, get_wsgi_header(HTTP_HEADER_PARENT_ID)] |
| 23 | +) |
| 24 | +POSSIBLE_HTTP_HEADER_SAMPLING_PRIORITIES = frozenset( |
| 25 | + [HTTP_HEADER_SAMPLING_PRIORITY, get_wsgi_header(HTTP_HEADER_SAMPLING_PRIORITY)] |
| 26 | +) |
| 27 | +POSSIBLE_HTTP_HEADER_ORIGIN = frozenset( |
| 28 | + [HTTP_HEADER_ORIGIN, get_wsgi_header(HTTP_HEADER_ORIGIN)] |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +class DatadogHTTPPropagator(object): |
| 33 | + """A HTTP Propagator using HTTP headers as carrier.""" |
| 34 | + |
| 35 | + def inject(self, span_context, headers): |
| 36 | + """Inject Context attributes that have to be propagated as HTTP headers. |
| 37 | +
|
| 38 | + Here is an example using `requests`:: |
| 39 | +
|
| 40 | + import requests |
| 41 | + from oteltrace.propagation.http import DatadogHTTPPropagator |
| 42 | +
|
| 43 | + def parent_call(): |
| 44 | + with tracer.trace('parent_span') as span: |
| 45 | + headers = {} |
| 46 | + propagator = DatadogHTTPPropagator() |
| 47 | + propagator.inject(span.context, headers) |
| 48 | + url = '<some RPC endpoint>' |
| 49 | + r = requests.get(url, headers=headers) |
| 50 | +
|
| 51 | + :param Context span_context: Span context to propagate. |
| 52 | + :param dict headers: HTTP headers to extend with tracing attributes. |
| 53 | + """ |
| 54 | + headers[HTTP_HEADER_TRACE_ID] = str(span_context.trace_id) |
| 55 | + headers[HTTP_HEADER_PARENT_ID] = str(span_context.span_id) |
| 56 | + sampling_priority = span_context.sampling_priority |
| 57 | + # Propagate priority only if defined |
| 58 | + if sampling_priority is not None: |
| 59 | + headers[HTTP_HEADER_SAMPLING_PRIORITY] = str(span_context.sampling_priority) |
| 60 | + # Propagate origin only if defined |
| 61 | + if span_context._otel_origin is not None: |
| 62 | + headers[HTTP_HEADER_ORIGIN] = str(span_context._otel_origin) |
| 63 | + |
| 64 | + @staticmethod |
| 65 | + def extract_header_value(possible_header_names, headers, default=None): |
| 66 | + for header, value in headers.items(): |
| 67 | + for header_name in possible_header_names: |
| 68 | + if header.lower() == header_name.lower(): |
| 69 | + return value |
| 70 | + |
| 71 | + return default |
| 72 | + |
| 73 | + @staticmethod |
| 74 | + def extract_trace_id(headers): |
| 75 | + return int( |
| 76 | + DatadogHTTPPropagator.extract_header_value( |
| 77 | + POSSIBLE_HTTP_HEADER_TRACE_IDS, headers, default=0, |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + @staticmethod |
| 82 | + def extract_parent_span_id(headers): |
| 83 | + return int( |
| 84 | + DatadogHTTPPropagator.extract_header_value( |
| 85 | + POSSIBLE_HTTP_HEADER_PARENT_IDS, headers, default=0, |
| 86 | + ) |
| 87 | + ) |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + def extract_sampling_priority(headers): |
| 91 | + return DatadogHTTPPropagator.extract_header_value( |
| 92 | + POSSIBLE_HTTP_HEADER_SAMPLING_PRIORITIES, headers, |
| 93 | + ) |
| 94 | + |
| 95 | + @staticmethod |
| 96 | + def extract_origin(headers): |
| 97 | + return DatadogHTTPPropagator.extract_header_value( |
| 98 | + POSSIBLE_HTTP_HEADER_ORIGIN, headers, |
| 99 | + ) |
| 100 | + |
| 101 | + def extract(self, headers): |
| 102 | + """Extract a Context from HTTP headers into a new Context. |
| 103 | +
|
| 104 | + Here is an example from a web endpoint:: |
| 105 | +
|
| 106 | + from oteltrace.propagation.http import DatadogHTTPPropagator |
| 107 | +
|
| 108 | + def my_controller(url, headers): |
| 109 | + propagator = DatadogHTTPPropagator() |
| 110 | + context = propagator.extract(headers) |
| 111 | + tracer.context_provider.activate(context) |
| 112 | +
|
| 113 | + with tracer.trace('my_controller') as span: |
| 114 | + span.set_meta('http.url', url) |
| 115 | +
|
| 116 | + :param dict headers: HTTP headers to extract tracing attributes. |
| 117 | + :return: New `Context` with propagated attributes. |
| 118 | + """ |
| 119 | + if not headers: |
| 120 | + return Context() |
| 121 | + |
| 122 | + try: |
| 123 | + trace_id = DatadogHTTPPropagator.extract_trace_id(headers) |
| 124 | + parent_span_id = DatadogHTTPPropagator.extract_parent_span_id(headers) |
| 125 | + sampling_priority = DatadogHTTPPropagator.extract_sampling_priority(headers) |
| 126 | + origin = DatadogHTTPPropagator.extract_origin(headers) |
| 127 | + |
| 128 | + if sampling_priority is not None: |
| 129 | + sampling_priority = int(sampling_priority) |
| 130 | + |
| 131 | + return Context( |
| 132 | + trace_id=trace_id, |
| 133 | + span_id=parent_span_id, |
| 134 | + sampling_priority=sampling_priority, |
| 135 | + _otel_origin=origin, |
| 136 | + ) |
| 137 | + # If headers are invalid and cannot be parsed, return a new context and log the issue. |
| 138 | + except Exception as error: |
| 139 | + try: |
| 140 | + log.debug( |
| 141 | + 'invalid x-datadog-* headers, trace-id: %s, parent-id: %s, priority: %s, origin: %s, error: %s', |
| 142 | + headers.get(HTTP_HEADER_TRACE_ID, 0), |
| 143 | + headers.get(HTTP_HEADER_PARENT_ID, 0), |
| 144 | + headers.get(HTTP_HEADER_SAMPLING_PRIORITY), |
| 145 | + headers.get(HTTP_HEADER_ORIGIN, ''), |
| 146 | + error, |
| 147 | + ) |
| 148 | + # We might fail on string formatting errors ; in that case only format the first error |
| 149 | + except Exception: |
| 150 | + log.debug(error) |
| 151 | + return Context() |
0 commit comments