8000 chore(otel): raise exception if exporter unavailable by jer96 · Pull Request #234 · strands-agents/sdk-python · GitHub
[go: up one dir, main page]

Skip to content

chore(otel): raise exception if exporter unavailable #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/strands/telemetry/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def _initialize_tracer(self) -> None:
except Exception as e:
logger.exception("error=<%s> | Failed to configure OTLP exporter", e)
elif self.otlp_endpoint and self.tracer_provider:
logger.warning(OTEL_EXPORTER_MODULE_ERROR)
raise ModuleNotFoundError(OTEL_EXPORTER_MODULE_ERROR)

# Set as global tracer provider
trace_api.set_tracer_provider(self.tracer_provider)
Expand Down
27 changes: 27 additions & 0 deletions tests/strands/telemetry/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,33 @@ def test_initialize_tracer_with_invalid_otlp_endpoint(
mock_set_tracer_provider.assert_called_once_with(mock_tracer_provider.return_value)


def test_initialize_tracer_with_missing_module(
mock_is_initialized, mock_tracer_provider, mock_set_tracer_provider, mock_resource
):
"""Test initializing the tracer when the OTLP exporter module is missing."""
mock_is_initialized.return_value = False

mock_resource_instance = mock.MagicMock()
mock_resource.create.return_value = mock_resource_instance

# Initialize Tracer with OTLP endpoint but missing module
with (
mock.patch("strands.telemetry.tracer.HAS_OTEL_EXPORTER_MODULE", False),
pytest.raises(ModuleNotFoundError) as excinfo,
):
Tracer(otlp_endpoint="http://test-endpoint")

# Verify the error message
assert "opentelemetry-exporter-otlp-proto-http not detected" in str(excinfo.value)
assert "otel http exporting is currently DISABLED" in str(excinfo.value)

# Verify the tracer provider was created with correct resource
mock_tracer_provider.assert_called_once_with(resource=mock_resource_instance)

# Verify set_tracer_provider was not called since an exception was raised
mock_set_tracer_provider.assert_not_called()


def test_initialize_tracer_with_custom_tracer_provider(mock_get_tracer_provider, mock_resource):
"""Test initializing the tracer with NoOpTracerProvider."""
mock_is_initialized.return_value = True
Expand Down
Loading
0