fix(crewai): share singleton handler for OpenAI-v2 context inheritance#5
fix(crewai): share singleton handler for OpenAI-v2 context inheritance#5
Conversation
Switch CrewAI instrumentation to use the shared telemetry handler singleton and add regression coverage that verifies OpenAI-v2 chat spans and metrics inherit CrewAI agent context. Co-authored-by: Cursor <cursoragent@cursor.com>
Review Summary by QodoUse singleton handler for OpenAI-v2 context inheritance in CrewAI
WalkthroughsDescription• Switch CrewAI instrumentation to use shared singleton TelemetryHandler • Add regression test validating OpenAI-v2 chat spans inherit agent context • Update test mocks to use get_telemetry_handler instead of direct constructor • Ensure proper handler cleanup between tests via conftest fixture Diagramflowchart LR
A["CrewAI Instrumentation"] -->|"uses singleton"| B["get_telemetry_handler"]
B -->|"provides"| C["TelemetryHandler"]
C -->|"shared by"| D["OpenAI-v2 Chat Spans"]
D -->|"inherit"| E["Agent Context"]
File Changes1. instrumentation-genai/opentelemetry-instrumentation-crewai/src/opentelemetry/instrumentation/crewai/instrumentation.py
|
📝 WalkthroughWalkthroughThe changes introduce a factory function pattern for TelemetryHandler creation, replacing direct instantiation with Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Code Review by Qodo
1. Singleton ignores providers
|
| _handler = get_telemetry_handler( | ||
| tracer_provider=tracer_provider, meter_provider=meter_provider | ||
| ) |
There was a problem hiding this comment.
1. Singleton ignores providers 🐞 Bug ✓ Correctness
CrewAI now uses the shared get_telemetry_handler singleton; once any instrumentation initializes the singleton, later calls with different tracer/meter providers are silently ignored, making telemetry configuration order-dependent across instrumentations (e.g., OpenAI-v2 vs CrewAI). This can lead to spans/metrics being emitted through unexpected providers/exporters with no warning.
Agent Prompt
### Issue description
`get_telemetry_handler()` caches a singleton `TelemetryHandler` and ignores later `tracer_provider`/`meter_provider`/`logger_provider` arguments once `_default_handler` is set. After this PR, CrewAI also uses that singleton, so the first instrumentor to run effectively “wins” provider configuration for all other instrumentations that share the singleton.
### Issue Context
- CrewAI now does `_handler = get_telemetry_handler(tracer_provider=..., meter_provider=...)`.
- OpenAI-v2 also does `handler = get_telemetry_handler(tracer_provider=..., meter_provider=..., logger_provider=...)`.
- `get_telemetry_handler()` only constructs the handler on first call and returns the cached instance thereafter.
### Fix Focus Areas
- util/opentelemetry-util-genai/src/opentelemetry/util/genai/handler.py[1115-1133]
- instrumentation-genai/opentelemetry-instrumentation-crewai/src/opentelemetry/instrumentation/crewai/instrumentation.py[97-116]
- instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/__init__.py[68-79]
### Suggested approach
- Add a supported API path for deterministic initialization (e.g., `initialize_telemetry_handler(...)` / `set_default_telemetry_handler(handler)`), and have instrumentors use it.
- Additionally (or if you don’t want a new API), add mismatch detection in `get_telemetry_handler()`:
- If a cached handler exists and any provider argument is non-None, emit a WARNING that the singleton is already configured and the passed providers are being ignored.
- Document the “first-call wins” semantics clearly if that remains the intended behavior.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
Switch CrewAI instrumentation to use the shared telemetry handler singleton and add regression coverage that verifies OpenAI-v2 chat spans and metrics inherit CrewAI agent context.
Summary by CodeRabbit
Refactor
Tests