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