|
| 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 | +import unittest |
| 16 | + |
| 17 | +from opentelemetry import trace as trace_api |
| 18 | +from opentelemetry.ext.datadog import constants, propagator |
| 19 | +from opentelemetry.sdk import trace |
| 20 | +from opentelemetry.trace.propagation import ( |
| 21 | + get_span_from_context, |
| 22 | + set_span_in_context, |
| 23 | +) |
| 24 | + |
| 25 | +FORMAT = propagator.DatadogFormat() |
| 26 | + |
| 27 | + |
| 28 | +def get_as_list(dict_object, key): |
| 29 | + value = dict_object.get(key) |
| 30 | + return [value] if value is not None else [] |
| 31 | + |
| 32 | + |
| 33 | +class TestDatadogFormat(unittest.TestCase): |
| 34 | + @classmethod |
| 35 | + def setUpClass(cls): |
| 36 | + cls.serialized_trace_id = propagator.format_trace_id( |
| 37 | + trace.generate_trace_id() |
| 38 | + ) |
| 39 | + cls.serialized_parent_id = propagator.format_span_id( |
| 40 | + trace.generate_span_id() |
| 41 | + ) |
| 42 | + cls.serialized_origin = "origin-service" |
| 43 | + |
| 44 | + def test_malformed_headers(self): |
| 45 | + """Test with no Datadog headers""" |
| 46 | + malformed_trace_id_key = FORMAT.TRACE_ID_KEY + "-x" |
| 47 | + malformed_parent_id_key = FORMAT.PARENT_ID_KEY + "-x" |
| 48 | + context = get_span_from_context( |
| 49 | + FORMAT.extract( |
| 50 | + get_as_list, |
| 51 | + { |
| 52 | + malformed_trace_id_key: self.serialized_trace_id, |
| 53 | + malformed_parent_id_key: self.serialized_parent_id, |
| 54 | + }, |
| 55 | + ) |
| 56 | + ).get_context() |
| 57 | + |
| 58 | + self.assertNotEqual(context.trace_id, int(self.serialized_trace_id)) |
| 59 | + self.assertNotEqual(context.span_id, int(self.serialized_parent_id)) |
| 60 | + self.assertFalse(context.is_remote) |
| 61 | + |
| 62 | + def test_missing_trace_id(self): |
| 63 | + """If a trace id is missing, populate an invalid trace id.""" |
| 64 | + carrier = { |
| 65 | + FORMAT.PARENT_ID_KEY: self.serialized_parent_id, |
| 66 | + } |
| 67 | + |
| 68 | + ctx = FORMAT.extract(get_as_list, carrier) |
| 69 | + span_context = get_span_from_context(ctx).get_context() |
| 70 | + self.assertEqual(span_context.trace_id, trace_api.INVALID_TRACE_ID) |
| 71 | + |
| 72 | + def test_missing_parent_id(self): |
| 73 | + """If a parent id is missing, populate an invalid trace id.""" |
| 74 | + carrier = { |
| 75 | + FORMAT.TRACE_ID_KEY: self.serialized_trace_id, |
| 76 | + } |
| 77 | + |
| 78 | + ctx = FORMAT.extract(get_as_list, carrier) |
| 79 | + span_context = get_span_from_context(ctx).get_context() |
| 80 | + self.assertEqual(span_context.span_id, trace_api.INVALID_SPAN_ID) |
| 81 | + |
| 82 | + def test_context_propagation(self): |
| 83 | + """Test the propagation of Datadog headers.""" |
| 84 | + parent_context = get_span_from_context( |
| 85 | + FORMAT.extract( |
| 86 | + get_as_list, |
| 87 | + { |
| 88 | + FORMAT.TRACE_ID_KEY: self.serialized_trace_id, |
| 89 | + FORMAT.PARENT_ID_KEY: self.serialized_parent_id, |
| 90 | + FORMAT.SAMPLING_PRIORITY_KEY: str(constants.AUTO_KEEP), |
| 91 | + FORMAT.ORIGIN_KEY: self.serialized_origin, |
| 92 | + }, |
| 93 | + ) |
| 94 | + ).get_context() |
| 95 | + |
| 96 | + self.assertEqual( |
| 97 | + parent_context.trace_id, int(self.serialized_trace_id) |
| 98 | + ) |
| 99 | + self.assertEqual( |
| 100 | + parent_context.span_id, int(self.serialized_parent_id) |
| 101 | + ) |
| 102 | + self.assertEqual(parent_context.trace_flags, constants.AUTO_KEEP) |
| 103 | + self.assertEqual( |
| 104 | + parent_context.trace_state.get(constants.DD_ORIGIN), |
| 105 | + self.serialized_origin, |
| 106 | + ) |
| 107 | + self.assertTrue(parent_context.is_remote) |
| 108 | + |
| 109 | + child = trace.Span( |
| 110 | + "child", |
| 111 | + trace_api.SpanContext( |
| 112 | + parent_context.trace_id, |
| 113 | + trace.generate_span_id(), |
| 114 | + is_remote=False, |
| 115 | + trace_flags=parent_context.trace_flags, |
| 116 | + trace_state=parent_context.trace_state, |
| 117 | + ), |
| 118 | + parent=parent_context, |
| 119 | + ) |
| 120 | + |
| 121 | + child_carrier = {} |
| 122 | + child_context = set_span_in_context(child) |
| 123 | + FORMAT.inject(dict.__setitem__, child_carrier, context=child_context) |
| 124 | + |
| 125 | + self.assertEqual( |
| 126 | + child_carrier[FORMAT.TRACE_ID_KEY], self.serialized_trace_id |
| 127 | + ) |
| 128 | + self.assertEqual( |
| 129 | + child_carrier[FORMAT.PARENT_ID_KEY], str(child.context.span_id) |
| 130 | + ) |
| 131 | + self.assertEqual( |
| 132 | + child_carrier[FORMAT.SAMPLING_PRIORITY_KEY], |
| 133 | + str(constants.AUTO_KEEP), |
| 134 | + ) |
| 135 | + self.assertEqual( |
| 136 | + child_carrier.get(FORMAT.ORIGIN_KEY), self.serialized_origin |
| 137 | + ) |
| 138 | + |
| 139 | + def test_sampling_priority_auto_reject(self): |
| 140 | + """Test sampling priority rejected.""" |
| 141 | + parent_context = get_span_from_context( |
| 142 | + FORMAT.extract( |
| 143 | + get_as_list, |
| 144 | + { |
| 145 | + FORMAT.TRACE_ID_KEY: self.serialized_trace_id, |
| 146 | + FORMAT.PARENT_ID_KEY: self.serialized_parent_id, |
| 147 | + FORMAT.SAMPLING_PRIORITY_KEY: str(constants.AUTO_REJECT), |
| 148 | + }, |
| 149 | + ) |
| 150 | + ).get_context() |
| 151 | + |
| 152 | + self.assertEqual(parent_context.trace_flags, constants.AUTO_REJECT) |
| 153 | + |
| 154 | + child = trace.Span( |
| 155 | + "child", |
| 156 | + trace_api.SpanContext( |
| 157 | + parent_context.trace_id, |
| 158 | + trace.generate_span_id(), |
| 159 | + is_remote=False, |
| 160 | + trace_flags=parent_context.trace_flags, |
| 161 | + trace_state=parent_context.trace_state, |
| 162 | + ), |
| 163 | + parent=parent_context, |
| 164 | + ) |
| 165 | + |
| 166 | + child_carrier = {} |
| 167 | + child_context = set_span_in_context(child) |
| 168 | + FORMAT.inject(dict.__setitem__, child_carrier, context=child_context) |
| 169 | + |
| 170 | + self.assertEqual( |
| 171 | + child_carrier[FORMAT.SAMPLING_PRIORITY_KEY], |
| 172 | + str(constants.AUTO_REJECT), |
| 173 | + ) |
0 commit comments