-
Notifications
You must be signed in to change notification settings - Fork 867
Expand file tree
/
Copy pathtest_tracer_provider.py
More file actions
440 lines (388 loc) · 16.2 KB
/
test_tracer_provider.py
File metadata and controls
440 lines (388 loc) · 16.2 KB
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
# 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.
# Tests access private members of SDK classes to assert correct configuration.
# pylint: disable=protected-access
import os
import sys
import unittest
from unittest.mock import MagicMock, patch
from opentelemetry.sdk._configuration._tracer_provider import (
configure_tracer_provider,
create_tracer_provider,
)
from opentelemetry.sdk._configuration.file._loader import ConfigurationError
from opentelemetry.sdk._configuration.models import (
BatchSpanProcessor as BatchSpanProcessorConfig,
)
from opentelemetry.sdk._configuration.models import (
OtlpGrpcExporter as OtlpGrpcExporterConfig,
)
from opentelemetry.sdk._configuration.models import (
OtlpHttpExporter as OtlpHttpExporterConfig,
)
from opentelemetry.sdk._configuration.models import (
ParentBasedSampler as ParentBasedSamplerConfig,
)
from opentelemetry.sdk._configuration.models import (
Sampler as SamplerConfig,
)
from opentelemetry.sdk._configuration.models import (
SimpleSpanProcessor as SimpleSpanProcessorConfig,
)
from opentelemetry.sdk._configuration.models import (
SpanExporter as SpanExporterConfig,
)
from opentelemetry.sdk._configuration.models import (
SpanLimits as SpanLimitsConfig,
)
from opentelemetry.sdk._configuration.models import (
SpanProcessor as SpanProcessorConfig,
)
from opentelemetry.sdk._configuration.models import (
TraceIdRatioBasedSampler as TraceIdRatioBasedConfig,
)
from opentelemetry.sdk._configuration.models import (
TracerProvider as TracerProviderConfig,
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
BatchSpanProcessor,
ConsoleSpanExporter,
SimpleSpanProcessor,
)
from opentelemetry.sdk.trace.sampling import (
ALWAYS_OFF,
ALWAYS_ON,
ParentBased,
TraceIdRatioBased,
)
class TestCreateTracerProviderBasic(unittest.TestCase):
def test_none_config_returns_provider(self):
resource = Resource({"service.name": "test"})
provider = create_tracer_provider(None, resource)
self.assertIsInstance(provider, TracerProvider)
def test_none_config_uses_supplied_resource(self):
resource = Resource({"service.name": "svc"})
provider = create_tracer_provider(None, resource)
self.assertIs(provider._resource, resource)
def test_none_config_uses_default_sampler(self):
provider = create_tracer_provider(None)
self.assertIsInstance(provider.sampler, ParentBased)
def test_none_config_no_processors(self):
provider = create_tracer_provider(None)
self.assertEqual(
len(provider._active_span_processor._span_processors), 0
)
def test_none_config_does_not_read_sampler_env_var(self):
with patch.dict(os.environ, {"OTEL_TRACES_SAMPLER": "always_off"}):
provider = create_tracer_provider(None)
self.assertIsInstance(provider.sampler, ParentBased)
def test_none_config_does_not_read_span_limit_env_var(self):
with patch.dict(os.environ, {"OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT": "1"}):
provider = create_tracer_provider(None)
self.assertEqual(provider._span_limits.max_span_attributes, 128)
def test_configure_none_does_not_set_global(self):
original = __import__(
"opentelemetry.trace", fromlist=["get_tracer_provider"]
).get_tracer_provider()
configure_tracer_provider(None)
after = __import__(
"opentelemetry.trace", fromlist=["get_tracer_provider"]
).get_tracer_provider()
self.assertIs(original, after)
def test_configure_with_config_sets_global(self):
config = TracerProviderConfig(processors=[])
with patch(
"opentelemetry.sdk._configuration._tracer_provider.trace.set_tracer_provider"
) as mock_set:
configure_tracer_provider(config)
mock_set.assert_called_once()
arg = mock_set.call_args[0][0]
self.assertIsInstance(arg, TracerProvider)
def test_processors_added_in_order(self):
mock_proc_a = MagicMock()
mock_proc_b = MagicMock()
config = TracerProviderConfig(processors=[])
provider = create_tracer_provider(config)
provider.add_span_processor(mock_proc_a)
provider.add_span_processor(mock_proc_b)
procs = provider._active_span_processor._span_processors
self.assertIs(procs[0], mock_proc_a)
self.assertIs(procs[1], mock_proc_b)
def test_span_limits_from_config(self):
config = TracerProviderConfig(
processors=[],
limits=SpanLimitsConfig(
attribute_count_limit=5,
event_count_limit=10,
link_count_limit=3,
),
)
provider = create_tracer_provider(config)
self.assertEqual(provider._span_limits.max_span_attributes, 5)
self.assertEqual(provider._span_limits.max_events, 10)
self.assertEqual(provider._span_limits.max_links, 3)
class TestCreateSampler(unittest.TestCase):
@staticmethod
def _make_provider(sampler_config):
return create_tracer_provider(
TracerProviderConfig(processors=[], sampler=sampler_config)
)
def test_always_on(self):
provider = self._make_provider(SamplerConfig(always_on={}))
self.assertIs(provider.sampler, ALWAYS_ON)
def test_always_off(self):
provider = self._make_provider(SamplerConfig(always_off={}))
self.assertIs(provider.sampler, ALWAYS_OFF)
def test_trace_id_ratio_based(self):
provider = self._make_provider(
SamplerConfig(
trace_id_ratio_based=TraceIdRatioBasedConfig(ratio=0.5)
)
)
self.assertIsInstance(provider.sampler, TraceIdRatioBased)
self.assertAlmostEqual(provider.sampler._rate, 0.5)
def test_trace_id_ratio_based_none_ratio_defaults_to_1(self):
provider = self._make_provider(
SamplerConfig(trace_id_ratio_based=TraceIdRatioBasedConfig())
)
self.assertIsInstance(provider.sampler, TraceIdRatioBased)
self.assertAlmostEqual(provider.sampler._rate, 1.0)
def test_parent_based_with_root(self):
provider = self._make_provider(
SamplerConfig(
parent_based=ParentBasedSamplerConfig(
root=SamplerConfig(always_on={})
)
)
)
self.assertIsInstance(provider.sampler, ParentBased)
def test_parent_based_no_root_defaults_to_always_on(self):
provider = self._make_provider(
SamplerConfig(parent_based=ParentBasedSamplerConfig())
)
self.assertIsInstance(provider.sampler, ParentBased)
self.assertIs(provider.sampler._root, ALWAYS_ON)
def test_parent_based_with_delegate_samplers(self):
provider = self._make_provider(
SamplerConfig(
parent_based=ParentBasedSamplerConfig(
root=SamplerConfig(always_on={}),
remote_parent_sampled=SamplerConfig(always_on={}),
remote_parent_not_sampled=SamplerConfig(always_off={}),
local_parent_sampled=SamplerConfig(always_on={}),
local_parent_not_sampled=SamplerConfig(always_off={}),
)
)
)
sampler = provider.sampler
self.assertIsInstance(sampler, ParentBased)
self.assertIs(sampler._remote_parent_sampled, ALWAYS_ON)
self.assertIs(sampler._remote_parent_not_sampled, ALWAYS_OFF)
self.assertIs(sampler._local_parent_sampled, ALWAYS_ON)
self.assertIs(sampler._local_parent_not_sampled, ALWAYS_OFF)
def test_unknown_sampler_raises_configuration_error(self):
with self.assertRaises(ConfigurationError):
create_tracer_provider(
TracerProviderConfig(processors=[], sampler=SamplerConfig())
)
class TestCreateSpanExporterAndProcessor(unittest.TestCase):
# pylint: disable=no-self-use
@staticmethod
def _make_batch_config(exporter_config):
return TracerProviderConfig(
processors=[
SpanProcessorConfig(
batch=BatchSpanProcessorConfig(exporter=exporter_config)
)
]
)
@staticmethod
def _make_simple_config(exporter_config):
return TracerProviderConfig(
processors=[
SpanProcessorConfig(
simple=SimpleSpanProcessorConfig(exporter=exporter_config)
)
]
)
def test_console_exporter_batch(self):
config = self._make_batch_config(SpanExporterConfig(console={}))
provider = create_tracer_provider(config)
procs = provider._active_span_processor._span_processors
self.assertEqual(len(procs), 1)
self.assertIsInstance(procs[0], BatchSpanProcessor)
self.assertIsInstance(procs[0].span_exporter, ConsoleSpanExporter)
def test_console_exporter_simple(self):
config = self._make_simple_config(SpanExporterConfig(console={}))
provider = create_tracer_provider(config)
procs = provider._active_span_processor._span_processors
self.assertIsInstance(procs[0], SimpleSpanProcessor)
self.assertIsInstance(procs[0].span_exporter, ConsoleSpanExporter)
def test_otlp_http_missing_package_raises(self):
config = self._make_batch_config(
SpanExporterConfig(otlp_http=OtlpHttpExporterConfig())
)
with patch.dict(
sys.modules,
{
"opentelemetry.exporter.otlp.proto.http.trace_exporter": None,
"opentelemetry.exporter.otlp.proto.http": None,
},
):
with self.assertRaises(ConfigurationError) as ctx:
create_tracer_provider(config)
self.assertIn("otlp-proto-http", str(ctx.exception))
def test_otlp_http_created_with_endpoint(self):
mock_exporter_cls = MagicMock()
mock_compression_cls = MagicMock()
mock_compression_cls.Gzip = "gzip_val"
mock_module = MagicMock()
mock_module.OTLPSpanExporter = mock_exporter_cls
mock_http_module = MagicMock()
mock_http_module.Compression = mock_compression_cls
with patch.dict(
sys.modules,
{
"opentelemetry.exporter.otlp.proto.http.trace_exporter": mock_module,
"opentelemetry.exporter.otlp.proto.http": mock_http_module,
},
):
config = self._make_batch_config(
SpanExporterConfig(
otlp_http=OtlpHttpExporterConfig(
endpoint="http://localhost:4318"
)
)
)
create_tracer_provider(config)
mock_exporter_cls.assert_called_once_with(
endpoint="http://localhost:4318",
headers=None,
timeout=None,
compression=None,
)
def test_otlp_http_created_with_deflate_compression(self):
mock_exporter_cls = MagicMock()
mock_compression_cls = MagicMock()
mock_compression_cls.Deflate = "deflate_val"
mock_module = MagicMock()
mock_module.OTLPSpanExporter = mock_exporter_cls
mock_http_module = MagicMock()
mock_http_module.Compression = mock_compression_cls
with patch.dict(
sys.modules,
{
"opentelemetry.exporter.otlp.proto.http.trace_exporter": mock_module,
"opentelemetry.exporter.otlp.proto.http": mock_http_module,
},
):
config = self._make_batch_config(
SpanExporterConfig(
otlp_http=OtlpHttpExporterConfig(compression="deflate")
)
)
create_tracer_provider(config)
_, kwargs = mock_exporter_cls.call_args
self.assertEqual(kwargs["compression"], "deflate_val")
def test_otlp_http_headers_list(self):
mock_exporter_cls = MagicMock()
mock_http_module = MagicMock()
mock_module = MagicMock()
mock_module.OTLPSpanExporter = mock_exporter_cls
with patch.dict(
sys.modules,
{
"opentelemetry.exporter.otlp.proto.http.trace_exporter": mock_module,
"opentelemetry.exporter.otlp.proto.http": mock_http_module,
},
):
config = self._make_batch_config(
SpanExporterConfig(
otlp_http=OtlpHttpExporterConfig(
headers_list="x-api-key=secret,env=prod"
)
)
)
create_tracer_provider(config)
_, kwargs = mock_exporter_cls.call_args
self.assertEqual(
kwargs["headers"], {"x-api-key": "secret", "env": "prod"}
)
def test_otlp_grpc_missing_package_raises(self):
config = self._make_batch_config(
SpanExporterConfig(otlp_grpc=OtlpGrpcExporterConfig())
)
with patch.dict(
sys.modules,
{
"opentelemetry.exporter.otlp.proto.grpc.trace_exporter": None,
"grpc": None,
},
):
with self.assertRaises(ConfigurationError) as ctx:
create_tracer_provider(config)
self.assertIn("otlp-proto-grpc", str(ctx.exception))
def test_no_processor_type_raises(self):
config = TracerProviderConfig(processors=[SpanProcessorConfig()])
with self.assertRaises(ConfigurationError):
create_tracer_provider(config)
def test_no_exporter_type_raises(self):
config = self._make_batch_config(SpanExporterConfig())
with self.assertRaises(ConfigurationError):
create_tracer_provider(config)
class TestCreateSpanLimits(unittest.TestCase):
# pylint: disable=no-self-use
@staticmethod
def _create_with_limits(limits_config):
return create_tracer_provider(
TracerProviderConfig(processors=[], limits=limits_config)
)
def test_explicit_attribute_count_limit(self):
provider = self._create_with_limits(
SpanLimitsConfig(attribute_count_limit=10)
)
self.assertEqual(provider._span_limits.max_span_attributes, 10)
def test_explicit_event_count_limit(self):
provider = self._create_with_limits(
SpanLimitsConfig(event_count_limit=5)
)
self.assertEqual(provider._span_limits.max_events, 5)
def test_explicit_link_count_limit(self):
provider = self._create_with_limits(
SpanLimitsConfig(link_count_limit=2)
)
self.assertEqual(provider._span_limits.max_links, 2)
def test_explicit_attribute_value_length_limit(self):
provider = self._create_with_limits(
SpanLimitsConfig(attribute_value_length_limit=64)
)
self.assertEqual(provider._span_limits.max_attribute_length, 64)
def test_absent_limits_use_spec_defaults(self):
provider = self._create_with_limits(SpanLimitsConfig())
self.assertEqual(provider._span_limits.max_span_attributes, 128)
self.assertEqual(provider._span_limits.max_events, 128)
self.assertEqual(provider._span_limits.max_links, 128)
def test_absent_limits_do_not_read_env_vars(self):
with patch.dict(
os.environ,
{
"OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT": "1",
"OTEL_SPAN_EVENT_COUNT_LIMIT": "2",
},
):
provider = self._create_with_limits(SpanLimitsConfig())
self.assertEqual(provider._span_limits.max_span_attributes, 128)
self.assertEqual(provider._span_limits.max_events, 128)