Skip to content

feat(telemetry): Expose OpenTelemetry exporter init arguments in API #365

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 31 additions & 6 deletions src/strands/telemetry/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import logging
from importlib.metadata import version
from typing import Any

import opentelemetry.metrics as metrics_api
import opentelemetry.sdk.metrics as metrics_sdk
Expand Down Expand Up @@ -118,22 +119,46 @@ def _initialize_tracer(self) -> None:
)
)

def setup_console_exporter(self) -> "StrandsTelemetry":
"""Set up console exporter for the tracer provider."""
def setup_console_exporter(self, **kwargs: Any) -> "StrandsTelemetry":
"""Set up console exporter for the tracer provider.

Args:
**kwargs: Optional keyword arguments passed directly to
OpenTelemetry's ConsoleSpanExporter initializer.

Returns:
self: Enables method chaining.

This method configures a SimpleSpanProcessor with a ConsoleSpanExporter,
allowing trace data to be output to the console. Any additional keyword
arguments provided will be forwarded to the ConsoleSpanExporter.
"""
try:
logger.info("Enabling console export")
console_processor = SimpleSpanProcessor(ConsoleSpanExporter())
console_processor = SimpleSpanProcessor(ConsoleSpanExporter(**kwargs))
self.tracer_provider.add_span_processor(console_processor)
except Exception as e:
logger.exception("error=<%s> | Failed to configure console exporter", e)
return self

def setup_otlp_exporter(self) -> "StrandsTelemetry":
"""Set up OTLP exporter for the tracer provider."""
def setup_otlp_exporter(self, **kwargs: Any) -> "StrandsTelemetry":
"""Set up OTLP exporter for the tracer provider.

Args:
**kwargs: Optional keyword arguments passed directly to
OpenTelemetry's OTLPSpanExporter initializer.

Returns:
self: Enables method chaining.

This method configures a BatchSpanProcessor with an OTLPSpanExporter,
allowing trace data to be exported to an OTLP endpoint. Any additional
keyword arguments provided will be forwarded to the OTLPSpanExporter.
"""
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter

try:
otlp_exporter = OTLPSpanExporter()
otlp_exporter = OTLPSpanExporter(**kwargs)
batch_processor = BatchSpanProcessor(otlp_exporter)
self.tracer_provider.add_span_processor(batch_processor)
logger.info("OTLP exporter configured")
Expand Down
8 changes: 4 additions & 4 deletions tests/strands/telemetry/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def test_setup_console_exporter(mock_resource, mock_tracer_provider, mock_consol
telemetry = StrandsTelemetry()
# Set the tracer_provider directly
telemetry.tracer_provider = mock_tracer_provider.return_value
telemetry.setup_console_exporter()
telemetry.setup_console_exporter(foo="bar")

mock_console_exporter.assert_called_once()
mock_console_exporter.assert_called_once_with(foo="bar")
mock_simple_processor.assert_called_once_with(mock_console_exporter.return_value)

mock_tracer_provider.return_value.add_span_processor.assert_called()
Expand All @@ -182,9 +182,9 @@ def test_setup_otlp_exporter(mock_resource, mock_tracer_provider, mock_otlp_expo
telemetry = StrandsTelemetry()
# Set the tracer_provider directly
telemetry.tracer_provider = mock_tracer_provider.return_value
telemetry.setup_otlp_exporter()
telemetry.setup_otlp_exporter(foo="bar")

mock_otlp_exporter.assert_called_once()
mock_otlp_exporter.assert_called_once_with(foo="bar")
mock_batch_processor.assert_called_once_with(mock_otlp_exporter.return_value)

mock_tracer_provider.return_value.add_span_processor.assert_called()
Expand Down