/
test_context.py
449 lines (384 loc) · 16.1 KB
/
test_context.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import contextlib
import logging
import mock
import threading
from .base import BaseTestCase
from tests.test_tracer import get_dummy_tracer
import pytest
from ddtrace.span import Span
from ddtrace.context import Context
from ddtrace.constants import HOSTNAME_KEY
from ddtrace.ext.priority import USER_REJECT, AUTO_REJECT, AUTO_KEEP, USER_KEEP
@pytest.fixture
def tracer_with_debug_logging():
# All the tracers, dummy or not, shares the same logging object.
tracer = get_dummy_tracer()
level = tracer.log.level
tracer.log.setLevel(logging.DEBUG)
try:
yield tracer
finally:
tracer.log.setLevel(level)
@mock.patch('logging.Logger.debug')
def test_log_unfinished_spans(log, tracer_with_debug_logging):
# when the root parent is finished, notify if there are spans still pending
tracer = tracer_with_debug_logging
ctx = Context()
# manually create a root-child trace
root = Span(tracer=tracer, name='root')
child_1 = Span(tracer=tracer, name='child_1', trace_id=root.trace_id, parent_id=root.span_id)
child_2 = Span(tracer=tracer, name='child_2', trace_id=root.trace_id, parent_id=root.span_id)
child_1._parent = root
child_2._parent = root
ctx.add_span(root)
ctx.add_span(child_1)
ctx.add_span(child_2)
# close only the parent
root.finish()
unfinished_spans_log = log.call_args_list[-3][0][2]
child_1_log = log.call_args_list[-2][0][1]
child_2_log = log.call_args_list[-1][0][1]
assert 2 == unfinished_spans_log
assert 'name child_1' in child_1_log
assert 'name child_2' in child_2_log
assert 'duration 0.000000s' in child_1_log
assert 'duration 0.000000s' in child_2_log
class TestTracingContext(BaseTestCase):
"""
Tests related to the ``Context`` class that hosts the trace for the
current execution flow.
"""
@contextlib.contextmanager
def override_partial_flush(self, ctx, enabled, min_spans):
original_enabled = ctx._partial_flush_enabled
original_min_spans = ctx._partial_flush_min_spans
ctx._partial_flush_enabled = enabled
ctx._partial_flush_min_spans = min_spans
try:
yield
finally:
ctx._partial_flush_enabled = original_enabled
ctx._partial_flush_min_spans = original_min_spans
def test_add_span(self):
# it should add multiple spans
ctx = Context()
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
assert 1 == len(ctx._trace)
assert 'fake_span' == ctx._trace[0].name
assert ctx == span.context
def test_context_sampled(self):
# a context is sampled if the spans are sampled
ctx = Context()
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
span.finish()
trace, sampled = ctx.get()
assert sampled is True
assert ctx.sampling_priority is None
def test_context_priority(self):
# a context is sampled if the spans are sampled
ctx = Context()
for priority in [USER_REJECT, AUTO_REJECT, AUTO_KEEP, USER_KEEP, None, 999]:
ctx.sampling_priority = priority
span = Span(tracer=None, name=('fake_span_%s' % repr(priority)))
ctx.add_span(span)
span.finish()
# It's "normal" to have sampled be true even when priority sampling is
# set to 0 or -1. It would stay false even even with priority set to 2.
# The only criteria to send (or not) the spans to the agent should be
# this "sampled" attribute, as it's tightly related to the trace weight.
assert priority == ctx.sampling_priority
trace, sampled = ctx.get()
assert sampled is True, 'priority has no impact on sampled status'
def test_current_span(self):
# it should return the current active span
ctx = Context()
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
assert span == ctx.get_current_span()
def test_current_root_span_none(self):
# it should return none when there is no root span
ctx = Context()
assert ctx.get_current_root_span() is None
def test_current_root_span(self):
# it should return the current active root span
ctx = Context()
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
assert span == ctx.get_current_root_span()
def test_close_span(self):
# it should keep track of closed spans, moving
# the current active to it's parent
ctx = Context()
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
ctx.close_span(span)
assert ctx.get_current_span() is None
def test_get_trace(self):
# it should return the internal trace structure
# if the context is finished
ctx = Context()
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
span.finish()
trace, sampled = ctx.get()
assert [span] == trace
assert sampled is True
# the context should be empty
assert 0 == len(ctx._trace)
assert ctx._current_span is None
def test_get_trace_empty(self):
# it should return None if the Context is not finished
ctx = Context()
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
trace, sampled = ctx.get()
assert trace is None
assert sampled is None
@mock.patch('ddtrace.internal.hostname.get_hostname')
def test_get_report_hostname_enabled(self, get_hostname):
get_hostname.return_value = 'test-hostname'
with self.override_global_config(dict(report_hostname=True)):
# Create a context and add a span and finish it
ctx = Context()
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
span.finish()
# Assert that we have not added the tag to the span yet
assert span.get_tag(HOSTNAME_KEY) is None
# Assert that retrieving the trace sets the tag
trace, _ = ctx.get()
assert trace[0].get_tag(HOSTNAME_KEY) == 'test-hostname'
assert span.get_tag(HOSTNAME_KEY) == 'test-hostname'
@mock.patch('ddtrace.internal.hostname.get_hostname')
def test_get_report_hostname_disabled(self, get_hostname):
get_hostname.return_value = 'test-hostname'
with self.override_global_config(dict(report_hostname=False)):
# Create a context and add a span and finish it
ctx = Context()
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
span.finish()
# Assert that we have not added the tag to the span yet
assert span.get_tag(HOSTNAME_KEY) is None
# Assert that retrieving the trace does not set the tag
trace, _ = ctx.get()
assert trace[0].get_tag(HOSTNAME_KEY) is None
assert span.get_tag(HOSTNAME_KEY) is None
@mock.patch('ddtrace.internal.hostname.get_hostname')
def test_get_report_hostname_default(self, get_hostname):
get_hostname.return_value = 'test-hostname'
# Create a context and add a span and finish it
ctx = Context()
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
span.finish()
# Assert that we have not added the tag to the span yet
assert span.get_tag(HOSTNAME_KEY) is None
# Assert that retrieving the trace does not set the tag
trace, _ = ctx.get()
assert trace[0].get_tag(HOSTNAME_KEY) is None
assert span.get_tag(HOSTNAME_KEY) is None
def test_partial_flush(self):
"""
When calling `Context.get`
When partial flushing is enabled
When we have just enough finished spans to flush
We return the finished spans
"""
tracer = get_dummy_tracer()
ctx = Context()
# Create a root span with 5 children, all of the children are finished, the root is not
root = Span(tracer=tracer, name='root')
ctx.add_span(root)
for i in range(5):
child = Span(tracer=tracer, name='child_{}'.format(i), trace_id=root.trace_id, parent_id=root.span_id)
child._parent = root
child.finished = True
ctx.add_span(child)
ctx.close_span(child)
with self.override_partial_flush(ctx, enabled=True, min_spans=5):
trace, sampled = ctx.get()
self.assertIsNotNone(trace)
self.assertIsNotNone(sampled)
self.assertEqual(len(trace), 5)
self.assertEqual(
set(['child_0', 'child_1', 'child_2', 'child_3', 'child_4']),
set([span.name for span in trace])
)
# Ensure we clear/reset internal stats as expected
self.assertEqual(ctx._trace, [root])
with self.override_partial_flush(ctx, enabled=True, min_spans=5):
trace, sampled = ctx.get()
self.assertIsNone(trace)
self.assertIsNone(sampled)
def test_partial_flush_too_many(self):
"""
When calling `Context.get`
When partial flushing is enabled
When we have more than the minimum number of spans needed to flush
We return the finished spans
"""
tracer = get_dummy_tracer()
ctx = Context()
# Create a root span with 5 children, all of the children are finished, the root is not
root = Span(tracer=tracer, name='root')
ctx.add_span(root)
for i in range(5):
child = Span(tracer=tracer, name='child_{}'.format(i), trace_id=root.trace_id, parent_id=root.span_id)
child._parent = root
child.finished = True
ctx.add_span(child)
ctx.close_span(child)
with self.override_partial_flush(ctx, enabled=True, min_spans=1):
trace, sampled = ctx.get()
self.assertIsNotNone(trace)
self.assertIsNotNone(sampled)
self.assertEqual(len(trace), 5)
self.assertEqual(
set(['child_0', 'child_1', 'child_2', 'child_3', 'child_4']),
set([span.name for span in trace])
)
# Ensure we clear/reset internal stats as expected
self.assertEqual(ctx._trace, [root])
with self.override_partial_flush(ctx, enabled=True, min_spans=5):
trace, sampled = ctx.get()
self.assertIsNone(trace)
self.assertIsNone(sampled)
def test_partial_flush_too_few(self):
"""
When calling `Context.get`
When partial flushing is enabled
When we do not have enough finished spans to flush
We return no spans
"""
tracer = get_dummy_tracer()
ctx = Context()
# Create a root span with 5 children, all of the children are finished, the root is not
root = Span(tracer=tracer, name='root')
ctx.add_span(root)
for i in range(5):
child = Span(tracer=tracer, name='child_{}'.format(i), trace_id=root.trace_id, parent_id=root.span_id)
child._parent = root
child.finished = True
ctx.add_span(child)
ctx.close_span(child)
# Test with having 1 too few spans for partial flush
with self.override_partial_flush(ctx, enabled=True, min_spans=6):
trace, sampled = ctx.get()
self.assertIsNone(trace)
self.assertIsNone(sampled)
self.assertEqual(len(ctx._trace), 6)
self.assertEqual(
set(['root', 'child_0', 'child_1', 'child_2', 'child_3', 'child_4']),
set([span.name for span in ctx._trace])
)
def test_partial_flush_remaining(self):
"""
When calling `Context.get`
When partial flushing is enabled
When we have some unfinished spans
We keep the unfinished spans around
"""
tracer = get_dummy_tracer()
ctx = Context()
# Create a root span with 5 children, all of the children are finished, the root is not
root = Span(tracer=tracer, name='root')
ctx.add_span(root)
for i in range(10):
child = Span(tracer=tracer, name='child_{}'.format(i), trace_id=root.trace_id, parent_id=root.span_id)
child._parent = root
ctx.add_span(child)
# CLose the first 5 only
if i < 5:
child.finished = True
ctx.close_span(child)
with self.override_partial_flush(ctx, enabled=True, min_spans=5):
trace, sampled = ctx.get()
# Assert partially flushed spans
self.assertTrue(len(trace), 5)
self.assertIsNotNone(sampled)
self.assertEqual(
set(['child_0', 'child_1', 'child_2', 'child_3', 'child_4']),
set([span.name for span in trace])
)
# Assert remaining unclosed spans
self.assertEqual(len(ctx._trace), 6)
self.assertEqual(
set(['root', 'child_5', 'child_6', 'child_7', 'child_8', 'child_9']),
set([span.name for span in ctx._trace]),
)
def test_finished(self):
# a Context is finished if all spans inside are finished
ctx = Context()
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
ctx.close_span(span)
@mock.patch('logging.Logger.debug')
def test_log_unfinished_spans_disabled(self, log):
# the trace finished status logging is disabled
tracer = get_dummy_tracer()
ctx = Context()
# manually create a root-child trace
root = Span(tracer=tracer, name='root')
child_1 = Span(tracer=tracer, name='child_1', trace_id=root.trace_id, parent_id=root.span_id)
child_2 = Span(tracer=tracer, name='child_2', trace_id=root.trace_id, parent_id=root.span_id)
child_1._parent = root
child_2._parent = root
ctx.add_span(root)
ctx.add_span(child_1)
ctx.add_span(child_2)
# close only the parent
root.finish()
# the logger has never been invoked to print unfinished spans
for call, _ in log.call_args_list:
msg = call[0]
assert 'the trace has %d unfinished spans' not in msg
@mock.patch('logging.Logger.debug')
def test_log_unfinished_spans_when_ok(self, log):
# if the unfinished spans logging is enabled but the trace is finished, don't log anything
tracer = get_dummy_tracer()
ctx = Context()
# manually create a root-child trace
root = Span(tracer=tracer, name='root')
child = Span(tracer=tracer, name='child_1', trace_id=root.trace_id, parent_id=root.span_id)
child._parent = root
ctx.add_span(root)
ctx.add_span(child)
# close the trace
child.finish()
root.finish()
# the logger has never been invoked to print unfinished spans
for call, _ in log.call_args_list:
msg = call[0]
assert 'the trace has %d unfinished spans' not in msg
def test_thread_safe(self):
# the Context must be thread-safe
ctx = Context()
def _fill_ctx():
span = Span(tracer=None, name='fake_span')
ctx.add_span(span)
threads = [threading.Thread(target=_fill_ctx) for _ in range(100)]
for t in threads:
t.daemon = True
t.start()
for t in threads:
t.join()
assert 100 == len(ctx._trace)
def test_clone(self):
ctx = Context()
ctx.sampling_priority = 2
# manually create a root-child trace
root = Span(tracer=None, name='root')
child = Span(tracer=None, name='child_1', trace_id=root.trace_id, parent_id=root.span_id)
child._parent = root
ctx.add_span(root)
ctx.add_span(child)
cloned_ctx = ctx.clone()
assert cloned_ctx._parent_trace_id == ctx._parent_trace_id
assert cloned_ctx._parent_span_id == ctx._parent_span_id
assert cloned_ctx._sampling_priority == ctx._sampling_priority
assert cloned_ctx._dd_origin == ctx._dd_origin
assert cloned_ctx._current_span == ctx._current_span
assert cloned_ctx._trace == []