Skip to content

feat(langchain): enhance span manager and add event emitter [3/5]#4452

Open
nagkumar91 wants to merge 3 commits intoopen-telemetry:mainfrom
nagkumar91:langchain/pr3-span-manager-events
Open

feat(langchain): enhance span manager and add event emitter [3/5]#4452
nagkumar91 wants to merge 3 commits intoopen-telemetry:mainfrom
nagkumar91:langchain/pr3-span-manager-events

Conversation

@nagkumar91
Copy link
Copy Markdown
Contributor

Description

Enhance the span lifecycle manager with advanced hierarchy tracking and add event emission for non-LLM GenAI operations.

Changes

span_manager.py (enhanced)

  • Ignored-run walkthrough: When internal LangChain plumbing runs are skipped, children are automatically re-parented to the nearest visible ancestor
  • Per-thread agent stacks: Tracks invoke_agent span hierarchies across concurrent execution threads
  • Token usage propagation: Accumulates input/output token counts from model spans up to parent agent spans
  • LangGraph goto support: Per-thread stacks for Command(goto=...) transitions to maintain correct parent context

event_emitter.py (new)

  • Emits semantic-convention-aligned LogRecord events for tool, agent, and retriever spans
  • Event types: gen_ai.tool.call, gen_ai.tool.result, gen_ai.agent.start, gen_ai.agent.finish, gen_ai.retriever.query, gen_ai.retriever.result
  • All emission gated behind the content policy (OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT / OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT)

Tests

  • test_span_hierarchy.py — ignored-run re-parenting, agent stack tracking, parent resolution
  • test_usage_propagation.py — token accumulation across span hierarchies
  • test_event_emitter.py — event emission with content recording on/off

PR Series

This is PR 3 of 5 breaking down #4389:

  1. ✅ Foundation modules (feat(langchain): add semconv attributes, operation mapping, and content recording modules [1/5] #4450)
  2. ✅ Provider inference, message formatting, W3C propagation (feat(langchain): add provider inference, message formatting, and W3C propagation utilities [2/5] #4451)
  3. Span manager enhancements + Event emitter (this PR)
  4. Callback handler — model, chain, agent callbacks + wiring
  5. Callback handler — tool, retriever callbacks + E2E tests

Depends on: #4451 (merge first)

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist

  • Followed the style guidelines of this project
  • Unit tests have been added

nagkumar91 and others added 3 commits April 16, 2026 14:59
…nt recording modules

Add foundation modules for enhanced LangChain GenAI semantic convention tracing:

- semconv_attributes.py: Per-operation attribute matrix based on OTel GenAI
  semantic conventions. Single source of truth for which attributes apply to
  which operations (chat, text_completion, invoke_agent, execute_tool,
  invoke_workflow, retrieval).

- operation_mapping.py: Callback-to-semconv operation mapping. Maps each
  LangChain callback to the correct GenAI semantic convention operation name.
  Includes heuristic classification for on_chain_start callbacks (agents vs
  workflows vs internal plumbing) and LangGraph marker recognition.

- content_recording.py: Thin integration layer over the shared genai content
  capture utilities. Provides clear APIs for the callback handler to decide
  what content should be recorded on spans and events.

Part 1 of a series breaking down open-telemetry#4389 into smaller reviewable PRs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…propagation utilities

Add utility modules for the LangChain instrumentor:

- utils.py: Provider name inference from LangChain callback data (ls_provider,
  base_url, class name, endpoint fields), server address/port extraction, and
  W3C trace context header extraction and propagation support.

- message_formatting.py: Message, tool, and document serialization with
  content-redaction support. Converts LangChain message objects into the
  compact JSON format expected by OpenTelemetry GenAI semantic conventions.

Part 2 of a series breaking down open-telemetry#4389 into smaller reviewable PRs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Enhance the span lifecycle manager and add event emission for non-LLM
GenAI operations:

- span_manager.py: Enhanced with ignored-run walkthrough (re-parenting
  children of skipped internal runs), per-thread agent stacks for hierarchy
  tracking, token usage accumulation and propagation to parent agent spans,
  and LangGraph Command(goto=...) transition support.

- event_emitter.py: Emits semantic-convention-aligned log-record events for
  tool, agent, and retriever spans. All event emission is gated behind the
  content policy. Uses LogRecord instances linked to the active span context.

Part 3 of a series breaking down open-telemetry#4389 into smaller reviewable PRs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@eternalcuriouslearner eternalcuriouslearner left a comment

Choose a reason for hiding this comment

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

Howdy @nagkumar91, I believe some of the functionality implemented in this PR is already being built in the util-genai package. Specifically, ToolInvocation metrics in #4443 and AgentInvocation type in #4274 overlap with the custom event_emitter.py and span_manager.py here. Can you please take a look at those PRs and see if we can reuse them to simplify this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants