@@ -60,6 +60,50 @@ def mock_resource():
60
60
yield mock_resource
61
61
62
62
63
+ @pytest .fixture
64
+ def clean_env ():
65
+ """Fixture to provide a clean environment for each test."""
66
+ with mock .patch .dict (os .environ , {}, clear = True ):
67
+ yield
68
+
69
+
70
+ @pytest .fixture
71
+ def env_with_otlp ():
72
+ """Fixture with OTLP environment variables."""
73
+ with mock .patch .dict (
74
+ os .environ ,
75
+ {
76
+ "OTEL_EXPORTER_OTLP_ENDPOINT" : "http://env-endpoint" ,
77
+ },
78
+ ):
79
+ yield
80
+
81
+
82
+ @pytest .fixture
83
+ def env_with_console ():
84
+ """Fixture with console export environment variables."""
85
+ with mock .patch .dict (
86
+ os .environ ,
87
+ {
88
+ "STRANDS_OTEL_ENABLE_CONSOLE_EXPORT" : "true" ,
89
+ },
90
+ ):
91
+ yield
92
+
93
+
94
+ @pytest .fixture
95
+ def env_with_both ():
96
+ """Fixture with both OTLP and console export environment variables."""
97
+ with mock .patch .dict (
98
+ os .environ ,
99
+ {
100
+ "OTEL_EXPORTER_OTLP_ENDPOINT" : "http://env-endpoint" ,
101
+ "STRANDS_OTEL_ENABLE_CONSOLE_EXPORT" : "true" ,
102
+ },
103
+ ):
104
+ yield
105
+
106
+
63
107
def test_init_default ():
64
108
"""Test initializing the Tracer with default parameters."""
65
109
tracer = Tracer ()
@@ -681,3 +725,50 @@ def test_serialize_vs_json_dumps():
681
725
custom_result = serialize ({"text" : japanese_text })
682
726
assert japanese_text in custom_result
683
727
assert "\\ u" not in custom_result
728
+
729
+
730
+ def test_init_with_no_env_or_param (clean_env ):
731
+ """Test initializing with neither environment variable nor constructor parameter."""
732
+ tracer = Tracer ()
733
+ assert tracer .otlp_endpoint is None
734
+ assert tracer .enable_console_export is False
735
+
736
+ tracer = Tracer (otlp_endpoint = "http://param-endpoint" )
737
+ assert tracer .otlp_endpoint == "http://param-endpoint"
738
+
739
+ tracer = Tracer (enable_console_export = True )
740
+ assert tracer .enable_console_export is True
741
+
742
+
743
+ def test_constructor_params_with_otlp_env (env_with_otlp ):
744
+ """Test constructor parameters precedence over OTLP environment variable."""
745
+ # Constructor parameter should take precedence
746
+ tracer = Tracer (otlp_endpoint = "http://constructor-endpoint" )
747
+ assert tracer .otlp_endpoint == "http://constructor-endpoint"
748
+
749
+ # Without constructor parameter, should use env var
750
+ tracer = Tracer ()
751
+ assert tracer .otlp_endpoint == "http://env-endpoint"
752
+
753
+
754
+ def test_constructor_params_with_console_env (env_with_console ):
755
+ """Test constructor parameters precedence over console environment variable."""
756
+ # Constructor parameter should take precedence
757
+ tracer = Tracer (enable_console_export = False )
758
+ assert tracer .enable_console_export is False
759
+
760
+ # Without explicit constructor parameter, should use env var
761
+ tracer = Tracer ()
762
+ assert tracer .enable_console_export is True
763
+
764
+
765
+ def test_fallback_to_env_vars (env_with_both ):
766
+ """Test fallback to environment variables when no constructor parameters."""
767
+ tracer = Tracer ()
768
+ assert tracer .otlp_endpoint == "http://env-endpoint"
769
+ assert tracer .enable_console_export is True
770
+
771
+ # Constructor parameters should still take precedence
772
+ tracer = Tracer (otlp_endpoint = "http://constructor-endpoint" , enable_console_export = False )
773
+ assert tracer .otlp_endpoint == "http://constructor-endpoint"
774
+ assert tracer .enable_console_export is False
0 commit comments