Skip to content

fix: [#90] Add OpenTelemetry tracing support as optional backend#389

Open
nandanosql wants to merge 1 commit into
bytedance:mainfrom
nandanosql:fix/issue-90-opentelemetry-tracing
Open

fix: [#90] Add OpenTelemetry tracing support as optional backend#389
nandanosql wants to merge 1 commit into
bytedance:mainfrom
nandanosql:fix/issue-90-opentelemetry-tracing

Conversation

@nandanosql
Copy link
Copy Markdown

Fixes #90

Problem

The current trajectory recording writes JSON files to disk. While useful for debugging, this approach cannot integrate with standard observability toolchains (Jaeger, Zipkin, Grafana Tempo, Datadog, etc.) that rely on distributed tracing.

Solution

Added OpenTelemetry (OTel) tracing as an optional parallel backend that runs alongside the existing JSON recorder. This is non-breaking — the JSON recorder continues to work exactly as before.

New module: trae_agent/utils/otel_recorder.py

  • setup_otel_tracing() — initializes OTLP gRPC exporter from OTEL_EXPORTER_OTLP_ENDPOINT env var
  • OTelTrajectoryRecorder — emits spans for:
    • agent.run (root span) with task, provider, model attributes
    • llm.call with token counts, model, finish reason
    • agent.step with step number, state, tool names, errors
  • Graceful no-op when opentelemetry packages are not installed

Integration in Agent

  • __init__ detects OTEL_EXPORTER_OTLP_ENDPOINT and initializes OTel if available
  • run() wraps execution with start_recording / finalize_recording

Dependencies

Added optional [otel] extra:

uv sync --extra otel

Usage

OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 trae-cli run "Fix the bug"

Spans will appear in any OTLP-compatible backend. The JSON trajectory file is still written alongside.

…kend

Add OpenTelemetry-based tracing alongside the existing JSON file
recorder. Key changes:

- New trae_agent/utils/otel_recorder.py module with:
  - setup_otel_tracing() for configuring OTLP exporter
  - OTelTrajectoryRecorder that creates spans for agent runs,
    LLM calls, and agent steps
  - Graceful fallback when opentelemetry packages are not installed

- Modified Agent.__init__() to detect OTEL_EXPORTER_OTLP_ENDPOINT
  env var and initialize OTel tracing when available

- Modified Agent.run() to start/finalize OTel traces around
  agent execution

- Added 'otel' optional dependency group in pyproject.toml:
    uv sync --extra otel

Usage:
  OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 trae-cli run "..."

The JSON file recorder continues to work unchanged - OTel is purely
additive and requires explicit opt-in via env var + installed packages.
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

Use OpenTelemetry for tracing instead of standalone JSON file

2 participants