Debian Codegraph @

opentracing/harness/api_check.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # Copyright The OpenTracing 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 __future__ import absolute_import
16
17 import mock
18 import time
19 import pytest
20
21 import opentracing
22 from opentracing import Format
23
24
25 class APICompatibilityCheckMixin(object):
26 """
27 A mixin class for validation that a given tracer implementation
28 satisfies the requirements of the OpenTracing API.
29 """
30
31 def tracer(self):
32 raise NotImplementedError('Subclass must implement tracer()')
33
34 def check_baggage_values(self):
35 """If true, the test will validate Baggage items by storing and
36 retrieving them from the trace context. If false, it will only attempt
37 to store and retrieve the Baggage items to check the API compliance,
38 but not actually validate stored values. The latter mode is only
39 useful for no-op tracer.
40 """
41 return True
42
43 def check_scope_manager(self):
44 """If true, the test suite will validate the `ScopeManager` propagation
45 to ensure correct parenting. If false, it will only use the API without
46 asserting. The latter mode is only useful for no-op tracer.
47 """
48 return True
49
50 def is_parent(self, parent, span):
51 """Utility method that must be defined by Tracer implementers to define
52 how the test suite can check when a `Span` is a parent of another one.
53 It depends by the underlying implementation that is not part of the
54 OpenTracing API.
55 """
56 return False
57
58 def test_active_span(self):
59 tracer = self.tracer()
60 span = tracer.start_span('Fry')
61
62 if self.check_scope_manager():
63 assert tracer.active_span is None
64 assert tracer.scope_manager.active is None
65
66 with tracer.scope_manager.activate(span, True):
67 assert tracer.active_span is span
68 assert tracer.scope_manager.active.span is span
69
70 def test_start_active_span(self):
71 # the first usage returns a `Scope` that wraps a root `Span`
72 tracer = self.tracer()
73 with tracer.start_active_span('Fry') as scope:
74 assert scope.span is not None
75 if self.check_scope_manager():
76 assert self.is_parent(None, scope.span)
77
78 def test_start_active_span_parent(self):
79 # ensure the `ScopeManager` provides the right parenting
80 tracer = self.tracer()
81 with tracer.start_active_span('Fry') as parent:
82 with tracer.start_active_span('Farnsworth') as child:
83 if self.check_scope_manager():
84 assert self.is_parent(parent.span, child.span)
85
86 def test_start_active_span_ignore_active_span(self):
87 # ensure the `ScopeManager` ignores the active `Scope`
88 # if the flag is set
89 tracer = self.tracer()
90 with tracer.start_active_span('Fry') as parent:
91 with tracer.start_active_span('Farnsworth',
92 ignore_active_span=True) as child:
93 if self.check_scope_manager():
94 assert not self.is_parent(parent.span, child.span)
95
96 def test_start_active_span_not_finish_on_close(self):
97 # ensure a `Span` is finished when the `Scope` close
98 tracer = self.tracer()
99 scope = tracer.start_active_span('Fry', finish_on_close=False)
100 with mock.patch.object(scope.span, 'finish') as finish:
101 scope.close()
102
103 assert finish.call_count == 0
104
105 def test_start_active_span_finish_on_close(self):
106 # a `Span` is not finished when the flag is set
107 tracer = self.tracer()
108 scope = tracer.start_active_span('Fry', finish_on_close=True)
109 with mock.patch.object(scope.span, 'finish') as finish:
110 scope.close()
111
112 if self.check_scope_manager():
113 assert finish.call_count == 1
114
115 def test_start_active_span_default_finish_on_close(self):
116 # a `Span` is finished when no flag is set
117 tracer = self.tracer()
118 scope = tracer.start_active_span('Fry')
119 with mock.patch.object(scope.span, 'finish') as finish:
120 scope.close()
121
122 if self.check_scope_manager():
123 assert finish.call_count == 1
124
125 def test_start_span(self):
126 tracer = self.tracer()
127 span = tracer.start_span(operation_name='Fry')
128 span.finish()
129 with tracer.start_span(operation_name='Fry',
130 tags={'birthday': 'August 14 1974'}) as span:
131 span.log_event('birthplace',
132 payload={'hospital': 'Brooklyn Pre-Med Hospital',
133 'city': 'Old New York'})
134
135 def test_start_span_propagation(self):
136 # `start_span` must inherit the current active `Scope` span
137 tracer = self.tracer()
138 with tracer.start_active_span('Fry') as parent:
139 with tracer.start_span(operation_name='Farnsworth') as child:
140 if self.check_scope_manager():
141 assert self.is_parent(parent.span, child)
142
143 def test_start_span_propagation_ignore_active_span(self):
144 # `start_span` doesn't inherit the current active `Scope` span
145 # if the flag is set
146 tracer = self.tracer()
147 with tracer.start_active_span('Fry') as parent:
148 with tracer.start_span(operation_name='Farnsworth',
149 ignore_active_span=True) as child:
150 if self.check_scope_manager():
151 assert not self.is_parent(parent.span, child)
152
153 def test_start_span_with_parent(self):
154 tracer = self.tracer()
155 parent_span = tracer.start_span(operation_name='parent')
156 assert parent_span is not None
157 span = tracer.start_span(
158 operation_name='Leela',
159 child_of=parent_span)
160 span.finish()
161 span = tracer.start_span(
162 operation_name='Leela',
163 references=[opentracing.follows_from(parent_span.context)],
164 tags={'birthplace': 'sewers'})
165 span.finish()
166 parent_span.finish()
167
168 def test_start_child_span(self):
169 tracer = self.tracer()
170 parent_span = tracer.start_span(operation_name='parent')
171 assert parent_span is not None
172 child_span = opentracing.start_child_span(
173 parent_span, operation_name='Leela')
174 child_span.finish()
175 parent_span.finish()
176
177 def test_set_operation_name(self):
178 span = self.tracer().start_span().set_operation_name('Farnsworth')
179 span.finish()
180
181 def test_span_as_context_manager(self):
182 tracer = self.tracer()
183 finish = {'called': False}
184
185 def mock_finish(*_):
186 finish['called'] = True
187
188 with tracer.start_span(operation_name='antiquing') as span:
189 setattr(span, 'finish', mock_finish)
190 assert finish['called'] is True
191
192 # now try with exception
193 finish['called'] = False
194 try:
195 with tracer.start_span(operation_name='antiquing') as span:
196 setattr(span, 'finish', mock_finish)
197 raise ValueError()
198 except ValueError:
199 assert finish['called'] is True
200 else:
201 raise AssertionError('Expected ValueError') # pragma: no cover
202
203 def test_span_tag_value_types(self):
204 with self.tracer().start_span(operation_name='ManyTypes') as span:
205 span. \
206 set_tag('an_int', 9). \
207 set_tag('a_bool', True). \
208 set_tag('a_string', 'aoeuidhtns')
209
210 def test_span_tags_with_chaining(self):
211 span = self.tracer().start_span(operation_name='Farnsworth')
212 span. \
213 set_tag('birthday', '9 April, 2841'). \
214 set_tag('loves', 'different lengths of wires')
215 span. \
216 set_tag('unicode_val', u'non-ascii: \u200b'). \
217 set_tag(u'unicode_key_\u200b', 'ascii val')
218 span.finish()
219
220 def test_span_logs(self):
221 span = self.tracer().start_span(operation_name='Fry')
222
223 # Newer API
224 span.log_kv(
225 {'frozen.year': 1999, 'frozen.place': 'Cryogenics Labs'})
226 span.log_kv(
227 {'defrosted.year': 2999, 'defrosted.place': 'Cryogenics Labs'},
228 time.time())
229
230 # Older API
231 span.\
232 log_event('frozen', {'year': 1999, 'place': 'Cryogenics Labs'}). \
233 log_event('defrosted', {'year': 2999}). \
234 log_event('became his own grandfather', 1947)
235 span.\
236 log(event='frozen'). \
237 log(payload={'year': 1999}). \
238 log(timestamp=time.time(),
239 event='frozen',
240 payload={'year': 1999}). \
241 log(timestamp=time.time(),
242 event='unfrozen',
243 payload={'year': 2999})
244
245 def test_span_baggage(self):
246 with self.tracer().start_span(operation_name='Fry') as span:
247 assert span.context.baggage == {}
248 span_ref = span.set_baggage_item('Kiff-loves', 'Amy')
249 assert span_ref is span
250 val = span.get_baggage_item('Kiff-loves')
251 if self.check_baggage_values():
252 assert 'Amy' == val
253 pass
254
255 def test_context_baggage(self):
256 with self.tracer().start_span(operation_name='Fry') as span:
257 assert span.context.baggage == {}
258 span.set_baggage_item('Kiff-loves', 'Amy')
259 if self.check_baggage_values():
260 assert span.context.baggage == {'Kiff-loves': 'Amy'}
261 pass
262
263 def test_text_propagation(self):
264 with self.tracer().start_span(operation_name='Bender') as span:
265 text_carrier = {}
266 self.tracer().inject(
267 span_context=span.context,
268 format=opentracing.Format.TEXT_MAP,
269 carrier=text_carrier)
270 extracted_ctx = self.tracer().extract(
271 format=opentracing.Format.TEXT_MAP,
272 carrier=text_carrier)
273 assert extracted_ctx.baggage == {}
274
275 def test_binary_propagation(self):
276 with self.tracer().start_span(operation_name='Bender') as span:
277 bin_carrier = bytearray()
278 self.tracer().inject(
279 span_context=span.context,
280 format=opentracing.Format.BINARY,
281 carrier=bin_carrier)
282 extracted_ctx = self.tracer().extract(
283 format=opentracing.Format.BINARY,
284 carrier=bin_carrier)
285 assert extracted_ctx.baggage == {}
286
287 def test_mandatory_formats(self):
288 formats = [
289 (Format.TEXT_MAP, {}),
290 (Format.HTTP_HEADERS, {}),
291 (Format.BINARY, bytearray()),
292 ]
293 with self.tracer().start_span(operation_name='Bender') as span:
294 for fmt, carrier in formats:
295 # expecting no exceptions
296 span.tracer.inject(span.context, fmt, carrier)
297 span.tracer.extract(fmt, carrier)
298
299 def test_unknown_format(self):
300 custom_format = 'kiss my shiny metal ...'
301 with self.tracer().start_span(operation_name='Bender') as span:
302 with pytest.raises(opentracing.UnsupportedFormatException):
303 span.tracer.inject(span.context, custom_format, {})
304 with pytest.raises(opentracing.UnsupportedFormatException):
305 span.tracer.extract(custom_format, {})
306
307 def test_tracer_start_active_span_scope(self):
308 # the Tracer ScopeManager should store the active Scope
309 tracer = self.tracer()
310 scope = tracer.start_active_span('Fry')
311
312 if self.check_scope_manager():
313 assert tracer.scope_manager.active == scope
314
315 scope.close()
316
317 def test_tracer_start_span_scope(self):
318 # the Tracer ScopeManager should not store the new Span
319 tracer = self.tracer()
320 span = tracer.start_span(operation_name='Fry')
321
322 if self.check_scope_manager():
323 assert tracer.scope_manager.active is None
324
325 span.finish()