-
Notifications
You must be signed in to change notification settings - Fork 641
/
Copy pathtest_aws_xray_propagator.py
337 lines (271 loc) · 11.2 KB
/
test_aws_xray_propagator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from unittest.mock import Mock, patch
from requests.structures import CaseInsensitiveDict
import opentelemetry.trace as trace_api
from opentelemetry.context import Context
from opentelemetry.propagators.aws.aws_xray_propagator import (
TRACE_HEADER_KEY,
AwsXRayPropagator,
)
from opentelemetry.trace import (
DEFAULT_TRACE_OPTIONS,
DEFAULT_TRACE_STATE,
INVALID_SPAN_CONTEXT,
SpanContext,
TraceFlags,
TraceState,
set_span_in_context,
)
TRACE_ID_BASE16 = "8a3c60f7d188f8fa79d48a391a778fa6"
SPAN_ID_BASE16 = "53995c3f42cd8ad8"
# Propagators Usage Methods
def get_as_list(dict_object, key):
value = dict_object.get(key)
return [value] if value is not None else []
# Inject Methods
def build_test_current_context(
trace_id=int(TRACE_ID_BASE16, 16),
span_id=int(SPAN_ID_BASE16, 16),
is_remote=True,
trace_flags=DEFAULT_TRACE_OPTIONS,
trace_state=DEFAULT_TRACE_STATE,
):
return set_span_in_context(
trace_api.NonRecordingSpan(
build_test_span_context(
trace_id, span_id, is_remote, trace_flags, trace_state
)
)
)
# Extract Methods
def get_nested_span_context(parent_context):
return trace_api.get_current_span(parent_context).get_span_context()
# Helper Methods
def build_test_span_context(
trace_id=int(TRACE_ID_BASE16, 16),
span_id=int(SPAN_ID_BASE16, 16),
is_remote=True,
trace_flags=DEFAULT_TRACE_OPTIONS,
trace_state=DEFAULT_TRACE_STATE,
):
return SpanContext(
trace_id,
span_id,
is_remote,
trace_flags,
trace_state,
)
class AwsXRayPropagatorTest(unittest.TestCase):
XRAY_PROPAGATOR = AwsXRayPropagator()
# Inject Tests
def test_inject_into_non_sampled_context(self):
carrier = CaseInsensitiveDict()
AwsXRayPropagatorTest.XRAY_PROPAGATOR.inject(
carrier,
build_test_current_context(),
)
injected_items = set(carrier.items())
expected_items = set(
CaseInsensitiveDict(
{
TRACE_HEADER_KEY: "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=0"
}
).items()
)
self.assertEqual(injected_items, expected_items)
def test_inject_into_sampled_context(self):
carrier = CaseInsensitiveDict()
AwsXRayPropagatorTest.XRAY_PROPAGATOR.inject(
carrier,
build_test_current_context(
trace_flags=TraceFlags(TraceFlags.SAMPLED)
),
)
injected_items = set(carrier.items())
expected_items = set(
CaseInsensitiveDict(
{
TRACE_HEADER_KEY: "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=1"
}
).items()
)
self.assertEqual(injected_items, expected_items)
def test_inject_into_context_with_non_default_state(self):
carrier = CaseInsensitiveDict()
AwsXRayPropagatorTest.XRAY_PROPAGATOR.inject(
carrier,
build_test_current_context(
trace_state=TraceState([("foo", "bar")])
),
)
# TODO: (NathanielRN) Assert trace state when the propagator supports it
injected_items = set(carrier.items())
expected_items = set(
CaseInsensitiveDict(
{
TRACE_HEADER_KEY: "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=0"
}
).items()
)
self.assertEqual(injected_items, expected_items)
def test_inject_reported_fields_matches_carrier_fields(self):
carrier = CaseInsensitiveDict()
AwsXRayPropagatorTest.XRAY_PROPAGATOR.inject(
carrier,
build_test_current_context(),
)
injected_keys = set(carrier.keys())
self.assertEqual(
injected_keys, AwsXRayPropagatorTest.XRAY_PROPAGATOR.fields
)
# Extract Tests
def test_extract_empty_carrier_to_explicit_ctx(self):
orig_ctx = Context({"k1": "v1"})
context_with_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
CaseInsensitiveDict(), orig_ctx
)
self.assertDictEqual(orig_ctx, context_with_extracted)
def test_extract_empty_carrier_to_implicit_ctx(self):
context_with_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
CaseInsensitiveDict()
)
self.assertDictEqual(Context(), context_with_extracted)
def test_extract_not_sampled_context(self):
context_with_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
CaseInsensitiveDict(
{
TRACE_HEADER_KEY: "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=0"
}
),
)
self.assertEqual(
get_nested_span_context(context_with_extracted),
build_test_span_context(),
)
def test_extract_sampled_context(self):
context_with_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
CaseInsensitiveDict(
{
TRACE_HEADER_KEY: "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=1"
}
),
)
self.assertEqual(
get_nested_span_context(context_with_extracted),
build_test_span_context(
trace_flags=TraceFlags(TraceFlags.SAMPLED)
),
)
def test_extract_different_order(self):
context_with_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
CaseInsensitiveDict(
{
TRACE_HEADER_KEY: "Sampled=0;Parent=53995c3f42cd8ad8;Root=1-8a3c60f7-d188f8fa79d48a391a778fa6"
}
),
)
self.assertEqual(
get_nested_span_context(context_with_extracted),
build_test_span_context(),
)
def test_extract_with_additional_fields(self):
context_with_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
CaseInsensitiveDict(
{
TRACE_HEADER_KEY: "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=0;Foo=Bar"
}
),
)
self.assertEqual(
get_nested_span_context(context_with_extracted),
build_test_span_context(),
)
def test_extract_with_extra_whitespace(self):
context_with_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
CaseInsensitiveDict(
{
TRACE_HEADER_KEY: " Root = 1-8a3c60f7-d188f8fa79d48a391a778fa6 ; Parent = 53995c3f42cd8ad8 ; Sampled = 0 "
}
),
)
self.assertEqual(
get_nested_span_context(context_with_extracted),
build_test_span_context(),
)
def test_extract_invalid_xray_trace_header(self):
context_with_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
CaseInsensitiveDict({TRACE_HEADER_KEY: ""}),
)
self.assertEqual(
get_nested_span_context(context_with_extracted),
INVALID_SPAN_CONTEXT,
)
def test_extract_invalid_to_explicit_ctx(self):
trace_headers = [
"Root=1-12345678-abcdefghijklmnopqrstuvwx;Parent=53995c3f42cd8ad8;Sampled=0", # invalid trace id
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa600;Parent=53995c3f42cd8ad8;Sampled=0", # invalid size trace id
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=abcdefghijklmnop;Sampled=0", # invalid span id
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad800;Sampled=0" # invalid size span id
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=", # no sampled flag
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=011", # invalid size sampled
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=a", # non numeric sampled flag
]
for trace_header in trace_headers:
with self.subTest(trace_header=trace_header):
orig_ctx = Context({"k1": "v1"})
ctx = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
CaseInsensitiveDict({TRACE_HEADER_KEY: trace_header}),
orig_ctx,
)
self.assertDictEqual(orig_ctx, ctx)
def test_extract_invalid_to_implicit_ctx(self):
trace_headers = [
"Root=1-12345678-abcdefghijklmnopqrstuvwx;Parent=53995c3f42cd8ad8;Sampled=0", # invalid trace id
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa600;Parent=53995c3f42cd8ad8;Sampled=0", # invalid size trace id
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=abcdefghijklmnop;Sampled=0", # invalid span id
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad800;Sampled=0" # invalid size span id
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=", # no sampled flag
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=011", # invalid size sampled
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=a", # non numeric sampled flag
]
for trace_header in trace_headers:
with self.subTest(trace_header=trace_header):
ctx = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
CaseInsensitiveDict({TRACE_HEADER_KEY: trace_header}),
)
self.assertDictEqual(Context(), ctx)
@patch("opentelemetry.propagators.aws.aws_xray_propagator.trace")
def test_fields(self, mock_trace):
"""Make sure the fields attribute returns the fields used in inject"""
mock_trace.configure_mock(
**{
"get_current_span.return_value": Mock(
**{
"get_span_context.return_value": Mock(
**{"is_valid": True, "trace_id": 1, "span_id": 1}
)
}
)
}
)
mock_setter = Mock()
AwsXRayPropagatorTest.XRAY_PROPAGATOR.inject({}, setter=mock_setter)
inject_fields = set()
for call in mock_setter.mock_calls:
inject_fields.add(call[1][1])
self.assertEqual(
AwsXRayPropagatorTest.XRAY_PROPAGATOR.fields, inject_fields
)