Skip to content

chore: refactor tracer #286

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

Conversation

poshinchen
Copy link
Contributor

@poshinchen poshinchen commented Jun 25, 2025

Description

We faced dependency conflict before. In order to prevent the issues happening again and allow users to configure exporters, this pr refactors the tracer and created StrandsTelemetry class for configurations.

Current behavior:

  1. User sets up the tracer and Strands-Agent will use it directly with "OTEL_EXPORTER_OTLP_ENDPOINT" or "STRANDS_OTEL_ENABLE_CONSOLE_EXPORT" flag is set
  2. User lets Strands-Agent to setup tracer when either "OTEL_EXPORTER_OTLP_ENDPOINT" or "STRANDS_OTEL_ENABLE_CONSOLE_EXPORT" flag is set

Expected Behaviors after the change:

  • Removed STRANDS_OTEL_ENABLE_CONSOLE_EXPORT
  • If tracer has been setup by user, Strands will use it directly

If user sets up the tracer already by themselves, using Agent will directly picks up the tracer (without any other envs set)

Example:

# Set up the tracer
resource = Resource.create({ResourceAttributes.SERVICE_NAME: "strands-otel-test", "environment": "test"})
provider = TracerProvider(resource=resource)
otlp_exporter = OTLPSpanExporter()
provider.add_span_processor(BatchSpanProcessor(otlp_exporter))
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("strands-otel-test")

# Use Agent
from strands import Agent
from strands_tools import calculator

agent = Agent(tools=[calculator])
result = agent("what is 16*16?")

If user wants Strands-Agent to setup tracer

All default choices (from Opentelemtry):

from strands import Agent
from strands.telemetry import StrandsTelemetry
from strands_tools import calculator

strands_telemetry = StrandsTelemetry()
# for otlp exporter
strands_telemetry.setup_otlp_exporter()
# for console exporter
strands_telemetry.setup_console_exporter()

agent = Agent(tools=[calculator])
result = agent("what is 16*16?")

Via Environment variables

LANGFUSE_AUTH = base64.b64encode(
    f"{os.environ.get('LANGFUSE_PUBLIC_KEY')}:{os.environ.get('LANGFUSE_SECRET_KEY')}".encode()
).decode()
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = os.environ.get("LANGFUSE_HOST") + "/api/public/otel"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {LANGFUSE_AUTH}"
from strands import Agent
from strands.telemetry import StrandsTelemetry
from strands_tools import calculator

strands_telemetry = StrandsTelemetry()
# for otlp exporter
strands_telemetry.setup_otlp_exporter()
# for console exporter
strands_telemetry.setup_console_exporter()
agent = Agent(tools=[calculator])
result = agent("what is 16*16?")

Related Issues

Will link them

Documentation PR

Will update the documentation

Type of Change

  • New feature
  • Breaking change

Testing

  • Manual testing
  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@poshinchen poshinchen marked this pull request as ready for review June 25, 2025 15:33
@poshinchen poshinchen requested review from zastrowm, jer96, awsarron and a team June 25, 2025 15:34
@poshinchen poshinchen force-pushed the chore/exporters-configurations branch from 272df02 to e1ddae0 Compare June 25, 2025 17:56
@poshinchen poshinchen force-pushed the chore/exporters-configurations branch from e1ddae0 to 9383776 Compare June 25, 2025 17:57
@poshinchen poshinchen requested a review from jer96 June 25, 2025 20:24
@jer96
Copy link
Contributor

jer96 commented Jun 25, 2025

changes are moving in a great direction, thank you! a couple notes:

  1. we need to check with Ishan to ensure that all relevant documentation which mentions observability is updated. this change makes observability an opt-in feature.
  2. we should include this in the testing for Genesis observability

@poshinchen poshinchen force-pushed the chore/exporters-configurations branch from ea9d066 to d953141 Compare June 25, 2025 21:27
@poshinchen poshinchen requested a review from jer96 June 25, 2025 21:29
@poshinchen poshinchen force-pushed the chore/exporters-configurations branch from d953141 to f854585 Compare June 26, 2025 13:34
@poshinchen poshinchen merged commit 6a1ccea into strands-agents:main Jun 26, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants