|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from re import compile as re_compile |
| 16 | +from typing import Iterable, Optional |
| 17 | + |
| 18 | +from opentelemetry.baggage import get_all, set_baggage |
| 19 | +from opentelemetry.context import Context |
| 20 | +from opentelemetry.trace import ( |
| 21 | + INVALID_SPAN_ID, |
| 22 | + INVALID_TRACE_ID, |
| 23 | + DefaultSpan, |
| 24 | + SpanContext, |
| 25 | + TraceFlags, |
| 26 | + get_current_span, |
| 27 | + set_span_in_context, |
| 28 | +) |
| 29 | +from opentelemetry.trace.propagation.textmap import ( |
| 30 | + Getter, |
| 31 | + Setter, |
| 32 | + TextMapPropagator, |
| 33 | + TextMapPropagatorT, |
| 34 | +) |
| 35 | + |
| 36 | +OT_TRACE_ID_HEADER = "ot-tracer-traceid" |
| 37 | +OT_SPAN_ID_HEADER = "ot-tracer-spanid" |
| 38 | +OT_SAMPLED_HEADER = "ot-tracer-sampled" |
| 39 | +OT_BAGGAGE_PREFIX = "ot-baggage-" |
| 40 | + |
| 41 | +_valid_header_name = re_compile(r"^[\w_^`!#$%&'*+.|~]+$") |
| 42 | +_valid_header_value = re_compile(r"^[\t\x20-\x7e\x80-\xff]+$") |
| 43 | +_valid_traceid = re_compile(r"[0-9a-f]{32}|[0-9a-f]{16}") |
| 44 | +_valid_spanid = re_compile(r"[0-9a-f]{16}") |
| 45 | + |
| 46 | + |
| 47 | +class OpenTracingPropagator(TextMapPropagator): |
| 48 | + """Propagator for the OpenTracing HTTP header format""" |
| 49 | + |
| 50 | + def extract( |
| 51 | + self, |
| 52 | + getter: Getter[TextMapPropagatorT], |
| 53 | + carrier: TextMapPropagatorT, |
| 54 | + context: Optional[Context] = None, |
| 55 | + ) -> Context: |
| 56 | + |
| 57 | + traceid = extract_first_element( |
| 58 | + getter.get(carrier, OT_TRACE_ID_HEADER) |
| 59 | + ) |
| 60 | + |
| 61 | + spanid = extract_first_element(getter.get(carrier, OT_SPAN_ID_HEADER)) |
| 62 | + |
| 63 | + sampled = extract_first_element(getter.get(carrier, OT_SAMPLED_HEADER)) |
| 64 | + |
| 65 | + if sampled == "true": |
| 66 | + traceflags = TraceFlags.SAMPLED |
| 67 | + else: |
| 68 | + traceflags = TraceFlags.DEFAULT |
| 69 | + |
| 70 | + if ( |
| 71 | + traceid != INVALID_TRACE_ID |
| 72 | + and _valid_traceid.match(traceid) is not None |
| 73 | + and spanid != INVALID_SPAN_ID |
| 74 | + and _valid_spanid.match(spanid) is not None |
| 75 | + ): |
| 76 | + # FIXME this padding is lost when trace_id is casted to int when a |
| 77 | + # SpanContext is created. This causes a test case to fail. The |
| 78 | + # approach coded here attempts to closely follow JS implementation: |
| 79 | + # https://github.com/open-telemetry/opentelemetry-js-contrib/pull/318/files?file-filters%5B%5D=.js&file-filters%5B%5D=.json&file-filters%5B%5D=.ts&file-filters%5B%5D=dotfile#diff-824ceb4ccf0b4daec4207237a16f2ff4833d941e2a931bd45788f9620b4e8837R91 |
| 80 | + if len(traceid) == 16: |
| 81 | + traceid = "".join(["0" * 16, traceid]) |
| 82 | + |
| 83 | + context = set_span_in_context( |
| 84 | + DefaultSpan( |
| 85 | + SpanContext( |
| 86 | + trace_id=int(traceid, 16), |
| 87 | + span_id=int(spanid, 16), |
| 88 | + is_remote=True, |
| 89 | + trace_flags=traceflags, |
| 90 | + ) |
| 91 | + ), |
| 92 | + context, |
| 93 | + ) |
| 94 | + |
| 95 | + baggage = get_all(context) or {} |
| 96 | + |
| 97 | + for key in getter.keys(carrier): |
| 98 | + |
| 99 | + if not key.startswith(OT_BAGGAGE_PREFIX): |
| 100 | + continue |
| 101 | + |
| 102 | + baggage[key[len(OT_BAGGAGE_PREFIX) :]] = extract_first_element( |
| 103 | + getter.get(carrier, key) |
| 104 | + ) |
| 105 | + |
| 106 | + for key, value in baggage.items(): |
| 107 | + context = set_baggage(key, value, context) |
| 108 | + |
| 109 | + return context |
| 110 | + |
| 111 | + def inject( |
| 112 | + self, |
| 113 | + set_in_carrier: Setter[TextMapPropagatorT], |
| 114 | + carrier: TextMapPropagatorT, |
| 115 | + context: Optional[Context] = None, |
| 116 | + ) -> None: |
| 117 | + |
| 118 | + span_context = get_current_span(context).get_span_context() |
| 119 | + |
| 120 | + if ( |
| 121 | + span_context.trace_id == INVALID_TRACE_ID |
| 122 | + or _valid_traceid.match(hex(span_context.trace_id)[2:]) is None |
| 123 | + or span_context.span_id == INVALID_SPAN_ID |
| 124 | + or _valid_spanid.match(hex(span_context.span_id)[2:]) is None |
| 125 | + ): |
| 126 | + return |
| 127 | + |
| 128 | + set_in_carrier( |
| 129 | + carrier, OT_TRACE_ID_HEADER, hex(span_context.trace_id)[18:] |
| 130 | + ) |
| 131 | + set_in_carrier( |
| 132 | + carrier, OT_SPAN_ID_HEADER, hex(span_context.span_id)[2:], |
| 133 | + ) |
| 134 | + |
| 135 | + if span_context.trace_flags == TraceFlags.SAMPLED: |
| 136 | + traceflags = "true" |
| 137 | + else: |
| 138 | + traceflags = "false" |
| 139 | + |
| 140 | + set_in_carrier(carrier, OT_SAMPLED_HEADER, traceflags) |
| 141 | + |
| 142 | + baggage = get_all(context) |
| 143 | + |
| 144 | + if not baggage: |
| 145 | + return |
| 146 | + |
| 147 | + for header_name, header_value in baggage.items(): |
| 148 | + |
| 149 | + if ( |
| 150 | + _valid_header_name.match(header_name) is None |
| 151 | + or _valid_header_value.match(header_value) is None |
| 152 | + ): |
| 153 | + continue |
| 154 | + |
| 155 | + set_in_carrier( |
| 156 | + carrier, |
| 157 | + "".join([OT_BAGGAGE_PREFIX, header_name]), |
| 158 | + header_value, |
| 159 | + ) |
| 160 | + |
| 161 | + @property |
| 162 | + def fields(self): |
| 163 | + """Returns a set with the fields set in `inject`. |
| 164 | +
|
| 165 | + See |
| 166 | + `opentelemetry.trace.propagation.textmap.TextMapPropagator.fields` |
| 167 | + """ |
| 168 | + return { |
| 169 | + OT_TRACE_ID_HEADER, |
| 170 | + OT_SPAN_ID_HEADER, |
| 171 | + OT_SAMPLED_HEADER, |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | +def extract_first_element( |
| 176 | + items: Iterable[TextMapPropagatorT], |
| 177 | +) -> Optional[TextMapPropagatorT]: |
| 178 | + if items is None: |
| 179 | + return None |
| 180 | + return next(iter(items), None) |
0 commit comments