11# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22# SPDX-License-Identifier: Apache-2.0
33import os
4+ import importlib
5+ import sys
46from importlib .metadata import PackageNotFoundError , version
57from unittest import TestCase
68from unittest .mock import patch
79
810from amazon .opentelemetry .distro .aws_opentelemetry_distro import AwsOpenTelemetryDistro
11+ from opentelemetry .propagators .composite import CompositePropagator
912
1013
1114class TestAwsOpenTelemetryDistro (TestCase ):
@@ -40,6 +43,9 @@ def setUp(self):
4043 if var in os .environ :
4144 del os .environ [var ]
4245
46+ # Preserve the original sys.path
47+ self .original_sys_path = sys .path .copy ()
48+
4349 def tearDown (self ):
4450 # Clear all env vars first
4551 for var in self .env_vars_to_check :
@@ -50,6 +56,9 @@ def tearDown(self):
5056 for var , value in self .env_vars_to_restore .items ():
5157 os .environ [var ] = value
5258
59+ # Restore the original sys.path
60+ sys .path [:] = self .original_sys_path
61+
5362 def test_package_available (self ):
5463 try :
5564 version ("aws-opentelemetry-distro" )
@@ -65,7 +74,7 @@ def test_configure_sets_default_values(self, mock_super_configure, mock_apply_pa
6574
6675 # Check that default values are set
6776 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 " )
6978 self .assertEqual (os .environ .get ("OTEL_PYTHON_ID_GENERATOR" ), "xray" )
7079 self .assertEqual (
7180 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
93102 @patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches" )
94103 @patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.OpenTelemetryDistro._configure" )
95104 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
97106 ):
98107 """Test that _configure sets agent observability defaults when enabled"""
99108 mock_is_agent_observability .return_value = True
@@ -188,16 +197,15 @@ def test_configure_preserves_existing_env_vars(
188197 @patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches" )
189198 @patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.OpenTelemetryDistro._configure" )
190199 @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 ):
193201 """Test that _configure adds current working directory to sys.path"""
194202 mock_getcwd .return_value = "/test/working/directory"
195203
196204 distro = AwsOpenTelemetryDistro ()
197205 distro ._configure ()
198206
199207 # 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 )
201209
202210 @patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.get_aws_region" )
203211 @patch ("amazon.opentelemetry.distro.aws_opentelemetry_distro.is_agent_observability_enabled" )
@@ -224,3 +232,45 @@ def test_configure_with_agent_observability_endpoints_already_set(
224232 # And exporters are still set to otlp
225233 self .assertEqual (os .environ .get ("OTEL_TRACES_EXPORTER" ), "otlp" )
226234 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