Skip to content

feat(observability): add OpenTelemetry tracing and metrics support#871

Open
minimAluminiumalism wants to merge 2 commits intoMoonshotAI:mainfrom
minimAluminiumalism:main
Open

feat(observability): add OpenTelemetry tracing and metrics support#871
minimAluminiumalism wants to merge 2 commits intoMoonshotAI:mainfrom
minimAluminiumalism:main

Conversation

@minimAluminiumalism
Copy link

@minimAluminiumalism minimAluminiumalism commented Feb 3, 2026

Related Issue

NA

Description

Add OpenTelemetry observability support.

  • Add trace spans for LLM calls, tool executions, and subagent tasks and metrics collection (token usage, latency, tool call counts)
  • Add configurable log_content option for prompt/response tracing
Clipboard_Screenshot_1770102492

Gemini CLI Telemetry - Reference implementation
Claude Code Monitoring

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked the related issue, if any.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have run make gen-changelog to update the changelog.
  • I have run make gen-docs to update the user documentation.

Open with Devin

Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 6 additional flags.

Open in Devin Review

Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

View issue and 11 additional flags in Devin Review.

Open in Devin Review

Comment on lines +131 to +147
def shutdown() -> None:
"""Shutdown the OpenTelemetry SDK and flush any pending data."""
global _initialized, _tracer_provider, _meter_provider

if not _initialized:
return

try:
if _tracer_provider is not None:
_tracer_provider.shutdown()
if _meter_provider is not None:
_meter_provider.shutdown()
logger.debug("Observability SDK shutdown complete")
except Exception as e:
logger.error("Error during observability SDK shutdown: {error}", error=e)

_initialized = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Metrics module state not reset on SDK shutdown causing silent data loss on re-initialization

When shutdown() is called, the SDK sets _initialized = False but doesn't reset the metrics module's _metrics_initialized flag or the metric instrument variables (_session_counter, _turn_counter, etc.).

Click to expand

Issue Flow

  1. initialize() with observability enabled → creates tracer/meter
  2. record_session_start()_ensure_metrics_initialized() creates metric instruments, sets _metrics_initialized = True
  3. shutdown() → sets SDK _initialized = False, but _metrics_initialized stays True
  4. initialize() again → creates NEW tracer/meter
  5. record_session_start()_ensure_metrics_initialized() sees _metrics_initialized = True, returns get_meter() is not None (True with new meter)
  6. But _session_counter etc. still point to OLD (shutdown) meter's instruments!

Relevant code in metrics.py:53-54:

if _metrics_initialized:
    return get_meter() is not None

This early return prevents re-creation of metric instruments when they were created from a now-shutdown meter.

Impact

Metrics recorded after SDK re-initialization will silently go to the old shut-down meter instead of the new one, causing data loss. The code won't crash due to defensive None checks (e.g., if _session_counter is not None), but metrics will be silently discarded.

Recommendation: Add a function to reset metrics module state and call it from shutdown(). In sdk.py, also reset _tracer, _meter, _config to None. Consider adding a reset_metrics() function in metrics.py that resets _metrics_initialized = False and all metric instruments to None.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

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.

1 participant