1
1
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
# SPDX-License-Identifier: Apache-2.0
3
+ import importlib
3
4
import os
5
+ import sys
4
6
from importlib .metadata import PackageNotFoundError , version
5
7
from unittest import TestCase
6
8
from unittest .mock import patch
7
9
8
10
from amazon .opentelemetry .distro .aws_opentelemetry_distro import AwsOpenTelemetryDistro
11
+ from opentelemetry import propagate
12
+ from opentelemetry .propagators .composite import CompositePropagator
9
13
10
14
11
15
class TestAwsOpenTelemetryDistro (TestCase ):
@@ -40,6 +44,9 @@ def setUp(self):
40
44
if var in os .environ :
41
45
del os .environ [var ]
42
46
47
+ # Preserve the original sys.path
48
+ self .original_sys_path = sys .path .copy ()
49
+
43
50
def tearDown (self ):
44
51
# Clear all env vars first
45
52
for var in self .env_vars_to_check :
@@ -50,6 +57,9 @@ def tearDown(self):
50
57
for var , value in self .env_vars_to_restore .items ():
51
58
os .environ [var ] = value
52
59
60
+ # Restore the original sys.path
61
+ sys .path [:] = self .original_sys_path
62
+
53
63
def test_package_available (self ):
54
64
try :
55
65
version ("aws-opentelemetry-distro" )
@@ -65,7 +75,7 @@ def test_configure_sets_default_values(self, mock_super_configure, mock_apply_pa
65
75
66
76
# Check that default values are set
67
77
self .assertEqual (os .environ .get ("OTEL_EXPORTER_OTLP_PROTOCOL" ), "http/protobuf" )
68
- self .assertEqual (os .environ .get ("OTEL_PROPAGATORS" ), "xray,tracecontext,b3,b3multi " )
78
+ self .assertEqual (os .environ .get ("OTEL_PROPAGATORS" ), "baggage, xray,tracecontext" )
69
79
self .assertEqual (os .environ .get ("OTEL_PYTHON_ID_GENERATOR" ), "xray" )
70
80
self .assertEqual (
71
81
os .environ .get ("OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION" ),
@@ -188,16 +198,15 @@ def test_configure_preserves_existing_env_vars(
188
198
@patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches" )
189
199
@patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.OpenTelemetryDistro._configure" )
190
200
@patch ("os.getcwd" )
191
- @patch ("sys.path" , new_callable = list )
192
- def test_configure_adds_cwd_to_sys_path (self , mock_sys_path , mock_getcwd , mock_super_configure , mock_apply_patches ):
201
+ def test_configure_adds_cwd_to_sys_path (self , mock_getcwd , mock_super_configure , mock_apply_patches ):
193
202
"""Test that _configure adds current working directory to sys.path"""
194
203
mock_getcwd .return_value = "/test/working/directory"
195
204
196
205
distro = AwsOpenTelemetryDistro ()
197
206
distro ._configure ()
198
207
199
208
# Check that cwd was added to sys.path
200
- self .assertIn ("/test/working/directory" , mock_sys_path )
209
+ self .assertIn ("/test/working/directory" , sys . path )
201
210
202
211
@patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.get_aws_region" )
203
212
@patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.is_agent_observability_enabled" )
@@ -224,3 +233,47 @@ def test_configure_with_agent_observability_endpoints_already_set(
224
233
# And exporters are still set to otlp
225
234
self .assertEqual (os .environ .get ("OTEL_TRACES_EXPORTER" ), "otlp" )
226
235
self .assertEqual (os .environ .get ("OTEL_LOGS_EXPORTER" ), "otlp" )
236
+
237
+ @patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches" )
238
+ @patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.OpenTelemetryDistro._configure" )
239
+ def test_user_defined_propagators (self , mock_super_configure , mock_apply_patches ):
240
+ """Test that user-defined propagators are respected"""
241
+ # Set user-defined propagators
242
+ os .environ ["OTEL_PROPAGATORS" ] = "xray"
243
+
244
+ # Force the reload of the propagate module otherwise the above environment
245
+ # variable doesn't taker effect.
246
+ importlib .reload (propagate )
247
+
248
+ distro = AwsOpenTelemetryDistro ()
249
+ distro ._configure ()
250
+
251
+ # Verify that user-defined propagators are preserved
252
+ propagators = propagate .get_global_textmap ()
253
+ self .assertTrue (isinstance (propagators , CompositePropagator ))
254
+ expected_propagators = ["AwsXRayPropagator" ]
255
+ individual_propagators = propagators ._propagators
256
+ self .assertEqual (1 , len (individual_propagators ))
257
+ actual_propagators = []
258
+ for prop in individual_propagators :
259
+ actual_propagators .append (type (prop ).__name__ )
260
+ self .assertEqual (expected_propagators , actual_propagators )
261
+
262
+ @patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches" )
263
+ @patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.OpenTelemetryDistro._configure" )
264
+ def test_otel_propagators_added_when_not_user_defined (self , mock_super_configure , mock_apply_patches ):
265
+ distro = AwsOpenTelemetryDistro ()
266
+ distro ._configure ()
267
+
268
+ # Verify that the propagators are set correctly by ADOT
269
+ propagators = propagate .get_global_textmap ()
270
+
271
+ self .assertTrue (isinstance (propagators , CompositePropagator ))
272
+
273
+ expected_propagators = ["W3CBaggagePropagator" , "AwsXRayPropagator" , "TraceContextTextMapPropagator" ]
274
+ individual_propagators = propagators ._propagators
275
+ self .assertEqual (3 , len (individual_propagators ))
276
+ actual_propagators = []
277
+ for prop in individual_propagators :
278
+ actual_propagators .append (type (prop ).__name__ )
279
+ self .assertEqual (expected_propagators , actual_propagators )
0 commit comments