|
| 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 typing |
| 16 | +import unittest |
| 17 | + |
| 18 | +import opentelemetry.trace as trace |
| 19 | +from opentelemetry.context import get_current |
| 20 | +from opentelemetry.exporter.cloud_trace.cloud_trace_propagator import ( |
| 21 | + _TRACE_CONTEXT_HEADER_NAME, |
| 22 | + CloudTraceFormatPropagator, |
| 23 | +) |
| 24 | +from opentelemetry.trace.span import ( |
| 25 | + INVALID_SPAN_ID, |
| 26 | + INVALID_TRACE_ID, |
| 27 | + SpanContext, |
| 28 | + TraceFlags, |
| 29 | + get_hexadecimal_trace_id, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +def get_dict_value(dict_object: typing.Dict[str, str], key: str) -> str: |
| 34 | + return dict_object.get(key, "") |
| 35 | + |
| 36 | + |
| 37 | +class TestCloudTraceFormatPropagator(unittest.TestCase): |
| 38 | + def setUp(self): |
| 39 | + self.propagator = CloudTraceFormatPropagator() |
| 40 | + self.valid_trace_id = 281017822499060589596062859815111849546 |
| 41 | + self.valid_span_id = 17725314949316355921 |
| 42 | + self.too_long_id = 111111111111111111111111111111111111111111111 |
| 43 | + |
| 44 | + def _extract(self, header_value): |
| 45 | + """Test helper""" |
| 46 | + header = {_TRACE_CONTEXT_HEADER_NAME: [header_value]} |
| 47 | + new_context = self.propagator.extract(get_dict_value, header) |
| 48 | + return trace.get_current_span(new_context).get_context() |
| 49 | + |
| 50 | + def _inject(self, span=None): |
| 51 | + """Test helper""" |
| 52 | + ctx = get_current() |
| 53 | + if span is not None: |
| 54 | + ctx = trace.set_span_in_context(span, ctx) |
| 55 | + output = {} |
| 56 | + self.propagator.inject(dict.__setitem__, output, context=ctx) |
| 57 | + return output.get(_TRACE_CONTEXT_HEADER_NAME) |
| 58 | + |
| 59 | + def test_no_context_header(self): |
| 60 | + header = {} |
| 61 | + new_context = self.propagator.extract(get_dict_value, header) |
| 62 | + self.assertEqual( |
| 63 | + trace.get_current_span(new_context).get_context(), |
| 64 | + trace.INVALID_SPAN.get_context(), |
| 65 | + ) |
| 66 | + |
| 67 | + def test_empty_context_header(self): |
| 68 | + header = "" |
| 69 | + self.assertEqual( |
| 70 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 71 | + ) |
| 72 | + |
| 73 | + def test_valid_header(self): |
| 74 | + header = "{}/{};o=1".format( |
| 75 | + get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id |
| 76 | + ) |
| 77 | + new_span_context = self._extract(header) |
| 78 | + self.assertEqual(new_span_context.trace_id, self.valid_trace_id) |
| 79 | + self.assertEqual(new_span_context.span_id, self.valid_span_id) |
| 80 | + self.assertEqual(new_span_context.trace_flags, TraceFlags(1)) |
| 81 | + self.assertTrue(new_span_context.is_remote) |
| 82 | + |
| 83 | + header = "{}/{};o=10".format( |
| 84 | + get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id |
| 85 | + ) |
| 86 | + new_span_context = self._extract(header) |
| 87 | + self.assertEqual(new_span_context.trace_id, self.valid_trace_id) |
| 88 | + self.assertEqual(new_span_context.span_id, self.valid_span_id) |
| 89 | + self.assertEqual(new_span_context.trace_flags, TraceFlags(10)) |
| 90 | + self.assertTrue(new_span_context.is_remote) |
| 91 | + |
| 92 | + header = "{}/{};o=0".format( |
| 93 | + get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id |
| 94 | + ) |
| 95 | + new_span_context = self._extract(header) |
| 96 | + self.assertEqual(new_span_context.trace_id, self.valid_trace_id) |
| 97 | + self.assertEqual(new_span_context.span_id, self.valid_span_id) |
| 98 | + self.assertEqual(new_span_context.trace_flags, TraceFlags(0)) |
| 99 | + self.assertTrue(new_span_context.is_remote) |
| 100 | + |
| 101 | + header = "{}/{};o=0".format( |
| 102 | + get_hexadecimal_trace_id(self.valid_trace_id), 345 |
| 103 | + ) |
| 104 | + new_span_context = self._extract(header) |
| 105 | + self.assertEqual(new_span_context.trace_id, self.valid_trace_id) |
| 106 | + self.assertEqual(new_span_context.span_id, 345) |
| 107 | + self.assertEqual(new_span_context.trace_flags, TraceFlags(0)) |
| 108 | + self.assertTrue(new_span_context.is_remote) |
| 109 | + |
| 110 | + def test_invalid_header_format(self): |
| 111 | + header = "invalid_header" |
| 112 | + self.assertEqual( |
| 113 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 114 | + ) |
| 115 | + |
| 116 | + header = "{}/{};o=".format( |
| 117 | + get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id |
| 118 | + ) |
| 119 | + self.assertEqual( |
| 120 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 121 | + ) |
| 122 | + |
| 123 | + header = "extra_chars/{}/{};o=1".format( |
| 124 | + get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id |
| 125 | + ) |
| 126 | + self.assertEqual( |
| 127 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 128 | + ) |
| 129 | + |
| 130 | + header = "{}/{}extra_chars;o=1".format( |
| 131 | + get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id |
| 132 | + ) |
| 133 | + self.assertEqual( |
| 134 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 135 | + ) |
| 136 | + |
| 137 | + header = "{}/{};o=1extra_chars".format( |
| 138 | + get_hexadecimal_trace_id(self.valid_trace_id), self.valid_span_id |
| 139 | + ) |
| 140 | + self.assertEqual( |
| 141 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 142 | + ) |
| 143 | + |
| 144 | + header = "{}/;o=1".format( |
| 145 | + get_hexadecimal_trace_id(self.valid_trace_id) |
| 146 | + ) |
| 147 | + self.assertEqual( |
| 148 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 149 | + ) |
| 150 | + |
| 151 | + header = "/{};o=1".format(self.valid_span_id) |
| 152 | + self.assertEqual( |
| 153 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 154 | + ) |
| 155 | + |
| 156 | + header = "{}/{};o={}".format("123", "34", "4") |
| 157 | + self.assertEqual( |
| 158 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 159 | + ) |
| 160 | + |
| 161 | + def test_invalid_trace_id(self): |
| 162 | + header = "{}/{};o={}".format(INVALID_TRACE_ID, self.valid_span_id, 1) |
| 163 | + self.assertEqual( |
| 164 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 165 | + ) |
| 166 | + header = "{}/{};o={}".format("0" * 32, self.valid_span_id, 1) |
| 167 | + self.assertEqual( |
| 168 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 169 | + ) |
| 170 | + |
| 171 | + header = "0/{};o={}".format(self.valid_span_id, 1) |
| 172 | + self.assertEqual( |
| 173 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 174 | + ) |
| 175 | + |
| 176 | + header = "234/{};o={}".format(self.valid_span_id, 1) |
| 177 | + self.assertEqual( |
| 178 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 179 | + ) |
| 180 | + |
| 181 | + header = "{}/{};o={}".format(self.too_long_id, self.valid_span_id, 1) |
| 182 | + self.assertEqual( |
| 183 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 184 | + ) |
| 185 | + |
| 186 | + def test_invalid_span_id(self): |
| 187 | + header = "{}/{};o={}".format( |
| 188 | + get_hexadecimal_trace_id(self.valid_trace_id), INVALID_SPAN_ID, 1 |
| 189 | + ) |
| 190 | + self.assertEqual( |
| 191 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 192 | + ) |
| 193 | + |
| 194 | + header = "{}/{};o={}".format( |
| 195 | + get_hexadecimal_trace_id(self.valid_trace_id), "0" * 16, 1 |
| 196 | + ) |
| 197 | + self.assertEqual( |
| 198 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 199 | + ) |
| 200 | + |
| 201 | + header = "{}/{};o={}".format( |
| 202 | + get_hexadecimal_trace_id(self.valid_trace_id), "0", 1 |
| 203 | + ) |
| 204 | + self.assertEqual( |
| 205 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 206 | + ) |
| 207 | + |
| 208 | + header = "{}/{};o={}".format( |
| 209 | + get_hexadecimal_trace_id(self.valid_trace_id), self.too_long_id, 1 |
| 210 | + ) |
| 211 | + self.assertEqual( |
| 212 | + self._extract(header), trace.INVALID_SPAN.get_context() |
| 213 | + ) |
| 214 | + |
| 215 | + def test_inject_with_no_context(self): |
| 216 | + output = self._inject() |
| 217 | + self.assertIsNone(output) |
| 218 | + |
| 219 | + def test_inject_with_invalid_context(self): |
| 220 | + output = self._inject(trace.INVALID_SPAN) |
| 221 | + self.assertIsNone(output) |
| 222 | + |
| 223 | + def test_inject_with_valid_context(self): |
| 224 | + span_context = SpanContext( |
| 225 | + trace_id=self.valid_trace_id, |
| 226 | + span_id=self.valid_span_id, |
| 227 | + is_remote=True, |
| 228 | + trace_flags=TraceFlags(1), |
| 229 | + ) |
| 230 | + output = self._inject(trace.DefaultSpan(span_context)) |
| 231 | + self.assertEqual( |
| 232 | + output, |
| 233 | + "{}/{};o={}".format( |
| 234 | + get_hexadecimal_trace_id(self.valid_trace_id), |
| 235 | + self.valid_span_id, |
| 236 | + 1, |
| 237 | + ), |
| 238 | + ) |
0 commit comments