16
16
from unittest import TestCase
17
17
from unittest .mock import patch
18
18
19
- from opentelemetry .instrumentation .environment_variables import (
20
- OTEL_PYTHON_DISABLED_INSTRUMENTATIONS ,
19
+ from opentelemetry .sdk .environment_variables import (
20
+ OTEL_EXPERIMENTAL_RESOURCE_DETECTORS ,
21
+ OTEL_TRACES_SAMPLER_ARG ,
22
+ OTEL_TRACES_SAMPLER
21
23
)
24
+ from opentelemetry .instrumentation .environment_variables import (
25
+ OTEL_PYTHON_DISABLED_INSTRUMENTATIONS ,)
22
26
from azure .monitor .opentelemetry ._utils .configurations import (
23
- SAMPLING_RATIO_ENV_VAR ,
24
27
_get_configurations ,
25
28
)
29
+ from azure .monitor .opentelemetry ._constants import (
30
+ RATE_LIMITED_SAMPLER ,
31
+ FIXED_PERCENTAGE_SAMPLER ,
32
+ )
26
33
from opentelemetry .environment_variables import (
27
34
OTEL_LOGS_EXPORTER ,
28
35
OTEL_METRICS_EXPORTER ,
@@ -134,7 +141,7 @@ def test_get_configurations_defaults(self, resource_create_mock):
134
141
"os.environ" ,
135
142
{
136
143
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS : "flask,requests,fastapi,azure_sdk" ,
137
- SAMPLING_RATIO_ENV_VAR : "0.5" ,
144
+ OTEL_TRACES_SAMPLER_ARG : "0.5" ,
138
145
OTEL_TRACES_EXPORTER : "None" ,
139
146
OTEL_LOGS_EXPORTER : "none" ,
140
147
OTEL_METRICS_EXPORTER : "NONE" ,
@@ -166,12 +173,13 @@ def test_get_configurations_env_vars(self, resource_create_mock):
166
173
self .assertEqual (configurations ["resource" ].attributes , TEST_DEFAULT_RESOURCE .attributes )
167
174
self .assertEqual (environ [OTEL_EXPERIMENTAL_RESOURCE_DETECTORS ], "custom_resource_detector" )
168
175
resource_create_mock .assert_called_once_with ()
169
- self .assertEqual (configurations ["sampling_ratio" ], 0.5 )
176
+ self .assertEqual (configurations ["sampling_ratio" ], 1.0 )
170
177
171
178
@patch .dict (
172
179
"os.environ" ,
173
180
{
174
- SAMPLING_RATIO_ENV_VAR : "Half" ,
181
+ OTEL_TRACES_SAMPLER : FIXED_PERCENTAGE_SAMPLER ,
182
+ OTEL_TRACES_SAMPLER_ARG : "Half" ,
175
183
OTEL_TRACES_EXPORTER : "False" ,
176
184
OTEL_LOGS_EXPORTER : "no" ,
177
185
OTEL_METRICS_EXPORTER : "True" ,
@@ -181,7 +189,7 @@ def test_get_configurations_env_vars(self, resource_create_mock):
181
189
@patch ("opentelemetry.sdk.resources.Resource.create" , return_value = TEST_DEFAULT_RESOURCE )
182
190
def test_get_configurations_env_vars_validation (self , resource_create_mock ):
183
191
configurations = _get_configurations ()
184
-
192
+ print ( configurations )
185
193
self .assertTrue ("connection_string" not in configurations )
186
194
self .assertEqual (configurations ["disable_logging" ], False )
187
195
self .assertEqual (configurations ["disable_metrics" ], False )
@@ -260,3 +268,156 @@ def test_merge_instrumentation_options_extra_args(self, resource_create_mock):
260
268
"urllib3" : {"enabled" : True },
261
269
},
262
270
)
271
+ @patch .dict (
272
+ "os.environ" ,
273
+ {
274
+ OTEL_PYTHON_DISABLED_INSTRUMENTATIONS : "flask,requests,fastapi,azure_sdk" ,
275
+ OTEL_TRACES_SAMPLER : RATE_LIMITED_SAMPLER ,
276
+ OTEL_TRACES_SAMPLER_ARG : "0.5" ,
277
+ OTEL_TRACES_EXPORTER : "None" ,
278
+ OTEL_LOGS_EXPORTER : "none" ,
279
+ OTEL_METRICS_EXPORTER : "NONE" ,
280
+ OTEL_EXPERIMENTAL_RESOURCE_DETECTORS : "custom_resource_detector" ,
281
+ },
282
+ clear = True ,
283
+ )
284
+ @patch ("opentelemetry.sdk.resources.Resource.create" , return_value = TEST_DEFAULT_RESOURCE )
285
+ def test_get_configurations_env_vars_rate_limited (self , resource_create_mock ):
286
+ configurations = _get_configurations ()
287
+
288
+ self .assertTrue ("connection_string" not in configurations )
289
+ self .assertEqual (configurations ["disable_logging" ], True )
290
+ self .assertEqual (configurations ["disable_metrics" ], True )
291
+ self .assertEqual (configurations ["disable_tracing" ], True )
292
+ self .assertEqual (
293
+ configurations ["instrumentation_options" ],
294
+ {
295
+ "azure_sdk" : {"enabled" : False },
296
+ "django" : {"enabled" : True },
297
+ "fastapi" : {"enabled" : False },
298
+ "flask" : {"enabled" : False },
299
+ "psycopg2" : {"enabled" : True },
300
+ "requests" : {"enabled" : False },
301
+ "urllib" : {"enabled" : True },
302
+ "urllib3" : {"enabled" : True },
303
+ },
304
+ )
305
+ self .assertEqual (configurations ["resource" ].attributes , TEST_DEFAULT_RESOURCE .attributes )
306
+ self .assertEqual (environ [OTEL_EXPERIMENTAL_RESOURCE_DETECTORS ], "custom_resource_detector" )
307
+ resource_create_mock .assert_called_once_with ()
308
+ self .assertEqual (configurations ["sampling_traces_per_second" ], 0.5 )
309
+
310
+ @patch .dict (
311
+ "os.environ" ,
312
+ {
313
+ OTEL_PYTHON_DISABLED_INSTRUMENTATIONS : "flask,requests,fastapi,azure_sdk" ,
314
+ OTEL_TRACES_SAMPLER_ARG : "34" ,
315
+ OTEL_TRACES_EXPORTER : "None" ,
316
+ OTEL_LOGS_EXPORTER : "none" ,
317
+ OTEL_METRICS_EXPORTER : "NONE" ,
318
+ OTEL_EXPERIMENTAL_RESOURCE_DETECTORS : "custom_resource_detector" ,
319
+ },
320
+ clear = True ,
321
+ )
322
+ @patch ("opentelemetry.sdk.resources.Resource.create" , return_value = TEST_DEFAULT_RESOURCE )
323
+ def test_get_configurations_env_vars_no_preference (self , resource_create_mock ):
324
+ configurations = _get_configurations ()
325
+
326
+ self .assertTrue ("connection_string" not in configurations )
327
+ self .assertEqual (configurations ["disable_logging" ], True )
328
+ self .assertEqual (configurations ["disable_metrics" ], True )
329
+ self .assertEqual (configurations ["disable_tracing" ], True )
330
+ self .assertEqual (
331
+ configurations ["instrumentation_options" ],
332
+ {
333
+ "azure_sdk" : {"enabled" : False },
334
+ "django" : {"enabled" : True },
335
+ "fastapi" : {"enabled" : False },
336
+ "flask" : {"enabled" : False },
337
+ "psycopg2" : {"enabled" : True },
338
+ "requests" : {"enabled" : False },
339
+ "urllib" : {"enabled" : True },
340
+ "urllib3" : {"enabled" : True },
341
+ },
342
+ )
343
+ self .assertEqual (configurations ["resource" ].attributes , TEST_DEFAULT_RESOURCE .attributes )
344
+ self .assertEqual (environ [OTEL_EXPERIMENTAL_RESOURCE_DETECTORS ], "custom_resource_detector" )
345
+ resource_create_mock .assert_called_once_with ()
346
+ self .assertEqual (configurations ["sampling_ratio" ], 1.0 )
347
+
348
+ @patch .dict (
349
+ "os.environ" ,
350
+ {
351
+ OTEL_PYTHON_DISABLED_INSTRUMENTATIONS : "flask,requests,fastapi,azure_sdk" ,
352
+ OTEL_TRACES_SAMPLER_ARG : "2 traces per second" ,
353
+ OTEL_TRACES_EXPORTER : "None" ,
354
+ OTEL_LOGS_EXPORTER : "none" ,
355
+ OTEL_METRICS_EXPORTER : "NONE" ,
356
+ OTEL_EXPERIMENTAL_RESOURCE_DETECTORS : "custom_resource_detector" ,
357
+ },
358
+ clear = True ,
359
+ )
360
+ @patch ("opentelemetry.sdk.resources.Resource.create" , return_value = TEST_DEFAULT_RESOURCE )
361
+ def test_get_configurations_env_vars_check_default (self , resource_create_mock ):
362
+ configurations = _get_configurations ()
363
+
364
+ self .assertTrue ("connection_string" not in configurations )
365
+ self .assertEqual (configurations ["disable_logging" ], True )
366
+ self .assertEqual (configurations ["disable_metrics" ], True )
367
+ self .assertEqual (configurations ["disable_tracing" ], True )
368
+ self .assertEqual (
369
+ configurations ["instrumentation_options" ],
370
+ {
371
+ "azure_sdk" : {"enabled" : False },
372
+ "django" : {"enabled" : True },
373
+ "fastapi" : {"enabled" : False },
374
+ "flask" : {"enabled" : False },
375
+ "psycopg2" : {"enabled" : True },
376
+ "requests" : {"enabled" : False },
377
+ "urllib" : {"enabled" : True },
378
+ "urllib3" : {"enabled" : True },
379
+ },
380
+ )
381
+ self .assertEqual (configurations ["resource" ].attributes , TEST_DEFAULT_RESOURCE .attributes )
382
+ self .assertEqual (environ [OTEL_EXPERIMENTAL_RESOURCE_DETECTORS ], "custom_resource_detector" )
383
+ resource_create_mock .assert_called_once_with ()
384
+ self .assertEqual (configurations ["sampling_ratio" ], 1.0 )
385
+
386
+ @patch .dict (
387
+ "os.environ" ,
388
+ {
389
+ OTEL_PYTHON_DISABLED_INSTRUMENTATIONS : "flask,requests,fastapi,azure_sdk" ,
390
+ OTEL_TRACES_SAMPLER : FIXED_PERCENTAGE_SAMPLER ,
391
+ OTEL_TRACES_SAMPLER_ARG : "0.9" ,
392
+ OTEL_TRACES_EXPORTER : "None" ,
393
+ OTEL_LOGS_EXPORTER : "none" ,
394
+ OTEL_METRICS_EXPORTER : "NONE" ,
395
+ OTEL_EXPERIMENTAL_RESOURCE_DETECTORS : "custom_resource_detector" ,
396
+ },
397
+ clear = True ,
398
+ )
399
+ @patch ("opentelemetry.sdk.resources.Resource.create" , return_value = TEST_DEFAULT_RESOURCE )
400
+ def test_get_configurations_env_vars_fixed_percentage (self , resource_create_mock ):
401
+ configurations = _get_configurations ()
402
+
403
+ self .assertTrue ("connection_string" not in configurations )
404
+ self .assertEqual (configurations ["disable_logging" ], True )
405
+ self .assertEqual (configurations ["disable_metrics" ], True )
406
+ self .assertEqual (configurations ["disable_tracing" ], True )
407
+ self .assertEqual (
408
+ configurations ["instrumentation_options" ],
409
+ {
410
+ "azure_sdk" : {"enabled" : False },
411
+ "django" : {"enabled" : True },
412
+ "fastapi" : {"enabled" : False },
413
+ "flask" : {"enabled" : False },
414
+ "psycopg2" : {"enabled" : True },
415
+ "requests" : {"enabled" : False },
416
+ "urllib" : {"enabled" : True },
417
+ "urllib3" : {"enabled" : True },
418
+ },
419
+ )
420
+ self .assertEqual (configurations ["resource" ].attributes , TEST_DEFAULT_RESOURCE .attributes )
421
+ self .assertEqual (environ [OTEL_EXPERIMENTAL_RESOURCE_DETECTORS ], "custom_resource_detector" )
422
+ resource_create_mock .assert_called_once_with ()
423
+ self .assertEqual (configurations ["sampling_ratio" ], 0.9 )
0 commit comments