@@ -73,7 +73,9 @@ def mock_console_exporter():
7373
7474@pytest .fixture
7575def mock_resource ():
76- with mock .patch ("strands.telemetry.tracer.Resource" ) as mock_resource :
76+ with mock .patch ("strands.telemetry.tracer.get_otel_resource" ) as mock_resource :
77+ mock_resource_instance = mock .MagicMock ()
78+ mock_resource .return_value = mock_resource_instance
7779 yield mock_resource
7880
7981
@@ -175,14 +177,12 @@ def test_initialize_tracer_with_console(
175177):
176178 """Test initializing the tracer with console exporter."""
177179 mock_is_initialized .return_value = False
178- mock_resource_instance = mock .MagicMock ()
179- mock_resource .create .return_value = mock_resource_instance
180180
181181 # Initialize Tracer
182182 Tracer (enable_console_export = True )
183183
184184 # Verify the tracer provider was created with correct resource
185- mock_tracer_provider .assert_called_once_with (resource = mock_resource_instance )
185+ mock_tracer_provider .assert_called_once_with (resource = mock_resource . return_value )
186186
187187 # Verify console exporter was added
188188 mock_console_exporter .assert_called_once ()
@@ -198,9 +198,6 @@ def test_initialize_tracer_with_otlp(
198198 """Test initializing the tracer with OTLP exporter."""
199199 mock_is_initialized .return_value = False
200200
201- mock_resource_instance = mock .MagicMock ()
202- mock_resource .create .return_value = mock_resource_instance
203-
204201 # Initialize Tracer
205202 with (
206203 mock .patch ("strands.telemetry.tracer.HAS_OTEL_EXPORTER_MODULE" , True ),
@@ -209,7 +206,7 @@ def test_initialize_tracer_with_otlp(
209206 Tracer (otlp_endpoint = "http://test-endpoint" )
210207
211208 # Verify the tracer provider was created with correct resource
212- mock_tracer_provider .assert_called_once_with (resource = mock_resource_instance )
209+ mock_tracer_provider .assert_called_once_with (resource = mock_resource . return_value )
213210
214211 # Verify OTLP exporter was added with correct endpoint
215212 mock_otlp_exporter .assert_called_once ()
@@ -508,8 +505,6 @@ def test_initialize_tracer_with_invalid_otlp_endpoint(
508505 """Test initializing the tracer with an invalid OTLP endpoint."""
509506 mock_is_initialized .return_value = False
510507
511- mock_resource_instance = mock .MagicMock ()
512- mock_resource .create .return_value = mock_resource_instance
513508 mock_otlp_exporter .side_effect = Exception ("Connection error" )
514509
515510 # This should not raise an exception, but should log an error
@@ -522,7 +517,7 @@ def test_initialize_tracer_with_invalid_otlp_endpoint(
522517 Tracer (otlp_endpoint = "http://invalid-endpoint" )
523518
524519 # Verify the tracer provider was created with correct resource
525- mock_tracer_provider .assert_called_once_with (resource = mock_resource_instance )
520+ mock_tracer_provider .assert_called_once_with (resource = mock_resource . return_value )
526521
527522 # Verify OTLP exporter was attempted
528523 mock_otlp_exporter .assert_called_once ()
@@ -537,9 +532,6 @@ def test_initialize_tracer_with_missing_module(
537532 """Test initializing the tracer when the OTLP exporter module is missing."""
538533 mock_is_initialized .return_value = False
539534
540- mock_resource_instance = mock .MagicMock ()
541- mock_resource .create .return_value = mock_resource_instance
542-
543535 # Initialize Tracer with OTLP endpoint but missing module
544536 with (
545537 mock .patch ("strands.telemetry.tracer.HAS_OTEL_EXPORTER_MODULE" , False ),
@@ -552,13 +544,13 @@ def test_initialize_tracer_with_missing_module(
552544 assert "otel http exporting is currently DISABLED" in str (excinfo .value )
553545
554546 # Verify the tracer provider was created with correct resource
555- mock_tracer_provider .assert_called_once_with (resource = mock_resource_instance )
547+ mock_tracer_provider .assert_called_once_with (resource = mock_resource . return_value )
556548
557549 # Verify set_tracer_provider was not called since an exception was raised
558550 mock_set_tracer_provider .assert_not_called ()
559551
560552
561- def test_initialize_tracer_with_custom_tracer_provider (mock_get_tracer_provider , mock_resource ):
553+ def test_initialize_tracer_with_custom_tracer_provider (mock_is_initialized , mock_get_tracer_provider , mock_resource ):
562554 """Test initializing the tracer with NoOpTracerProvider."""
563555 mock_is_initialized .return_value = True
564556 tracer = Tracer (otlp_endpoint = "http://invalid-endpoint" )
0 commit comments