openllmetry icon indicating copy to clipboard operation
openllmetry copied to clipboard

feat(crewai): emit OTel GenAI plan spans

Open Krishnachaitanyakc opened this issue 6 months ago • 2 comments

Summary

  • Emit OTel GenAI plan spans around CrewAI planning.
  • Annotate the active plan span with the CrewAI planning agent name/id when CrewAI creates the real planning agent.
  • Add a real CrewPlanner regression test that verifies the emitted span and ensures instrumentation does not leave an instance-level _create_planning_agent shadow behind.

Reference

OTel GenAI plan-span semantics define gen_ai.operation.name as plan for agent planning/task decomposition phases: https://github.com/open-telemetry/semantic-conventions-genai/blob/main/docs/gen-ai/gen-ai-agent-spans.md#plan-span

Tests

  • uv run --python 3.12 --group test pytest tests/: 176 passed.
  • uv run --python 3.12 --group dev ruff check .: passed.
  • git diff --check: passed.
  • Rebased onto latest traceloop/openllmetry:main before opening this PR.

Summary by CodeRabbit

  • New Features

    • Enhanced tracing for crew planning operations with explicit "plan" spans and improved span naming/attributes for planning agents
    • Better observability of planning agent details and accurate status updates for successes and failures
  • Tests

    • Added comprehensive tests validating plan-span creation, error recording, and instrumentation behavior without mutating planner state

Review Change Stack

Krishnachaitanyakc avatar May 25 '26 22:05 Krishnachaitanyakc

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar May 25 '26 22:05 CLAassistant

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 13eb06e6-170a-45d6-a3f2-0c021c129231

📥 Commits

Reviewing files that changed from the base of the PR and between a5dd117fe10bf99b56807ebc6487a34c8e84e202 and 8d4a735c2b2a3f0d577c3b1296f0e9a779d4c679.

📒 Files selected for processing (1)
  • packages/opentelemetry-instrumentation-crewai/opentelemetry/instrumentation/crewai/instrumentation.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/opentelemetry-instrumentation-crewai/opentelemetry/instrumentation/crewai/instrumentation.py

📝 Walkthrough

Walkthrough

Instruments CrewPlanner planning internals with OpenTelemetry: adds a ContextVar to track the active "plan" span, implements wrappers to create/enrich plan spans, wires optional instrumentation into the instrumentor, and adds unit and integration tests validating span lifecycle and attributes.

Changes

CrewAI Planning Instrumentation

Layer / File(s) Summary
Imports and planning ContextVar
packages/opentelemetry-instrumentation-crewai/opentelemetry/instrumentation/crewai/instrumentation.py
Adds ContextVar import, _GEN_AI_OPERATION_PLAN constant, and _crew_planning_span ContextVar to store the active plan span.
Instrumentor optional wiring
packages/opentelemetry-instrumentation-crewai/opentelemetry/instrumentation/crewai/instrumentation.py
Wires optional wrapping/unwrapping of CrewPlanner._handle_crew_planning and CrewPlanner._create_planning_agent into CrewAIInstrumentor._instrument() / _uninstrument().
Safe apply/remove helpers
packages/opentelemetry-instrumentation-crewai/opentelemetry/instrumentation/crewai/instrumentation.py
Adds helpers that catch missing-attribute/import errors when applying or removing wrappers.
Planning wrappers (create/enrich)
packages/opentelemetry-instrumentation-crewai/opentelemetry/instrumentation/crewai/instrumentation.py
Implements wrap_crew_planning to start a plan span and manage the plan-span ContextVar, and wrap_create_planning_agent to enrich and rename the stored plan span with agent role/id when present.
Test imports, fixtures, and mocks
packages/opentelemetry-instrumentation-crewai/tests/test_crewai_instrumentation.py
Extends imports for tracing assertions, adds disable_crewai_telemetry fixture, updates mock_instrumentor to MagicMock(spec=CrewAIInstrumentor), and wires mock_crew to depend on telemetry isolation.
Unit tests for wrap_crew_planning and agent enrichment
packages/opentelemetry-instrumentation-crewai/tests/test_crewai_instrumentation.py
Adds tests asserting wrap_crew_planning starts a plan span with gen_ai.provider.name=crewai and gen_ai.operation.name=plan, records ERROR on exceptions, and verifies wrap_create_planning_agent enriches the stored plan span or skips enrichment outside the plan-span context.
End-to-end planner instrumentation test
packages/opentelemetry-instrumentation-crewai/tests/test_crewai_instrumentation.py
Instruments a real CrewPlanner with CrewAIInstrumentor and InMemorySpanExporter, verifies planner return type, absence of _create_planning_agent state mutation, and that a single exported plan span contains expected gen_ai attributes including gen_ai.agent.id.

Sequence Diagram(s)

sequenceDiagram
  participant Instrumentor as CrewAIInstrumentor
  participant Wrapper as wrap_crew_planning
  participant Planner as CrewPlanner._handle_crew_planning
  participant CreateAgent as CrewPlanner._create_planning_agent
  participant PlanSpan as "plan span (_crew_planning_span)"

  Instrumentor->>Wrapper: apply wrapper to _handle_crew_planning
  Wrapper->>PlanSpan: start "plan" span (tracer.start_span)
  Wrapper->>Planner: invoke original handler (ContextVar set)
  Planner->>CreateAgent: call _create_planning_agent
  CreateAgent->>PlanSpan: if ContextVar set -> set gen_ai.agent attrs, rename span
  Planner-->>Wrapper: return result
  Wrapper->>PlanSpan: set status (OK/ERROR) and end span

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐇 I stitched a span around the plan,
A ContextVar to hold its hand,
Agent names whispered in due time,
Errors flagged in tidy rhyme,
Traces hop forward—soft and grand.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 4.55% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding OpenTelemetry GenAI plan span emission for CrewAI instrumentation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 25 '26 22:05 coderabbitai[bot]

@max-deygin-traceloop @OzBenSimhonTraceloop can you please review this PR?

Krishnachaitanyakc avatar Jun 03 '26 04:06 Krishnachaitanyakc