Enhance LangChain GenAI semconv tracing#4389
Open
nagkumar91 wants to merge 4 commits intoopen-telemetry:mainfrom
Open
Enhance LangChain GenAI semconv tracing#4389nagkumar91 wants to merge 4 commits intoopen-telemetry:mainfrom
nagkumar91 wants to merge 4 commits intoopen-telemetry:mainfrom
Conversation
Add on_llm_new_token callback method to track streaming chunk timing: - Records time_to_first_chunk (TTFC) on first token arrival - Records time_per_output_chunk (TPOC) between subsequent tokens - Uses timeit.default_timer() consistent with TelemetryHandler timing - Builds metric attributes matching InvocationMetricsRecorder pattern - Cleans up streaming state in on_llm_end and on_llm_error Creates two new histograms using semconv metric names: - gen_ai.client.operation.time_to_first_chunk - gen_ai.client.operation.time_per_output_chunk Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Major enhancement to the LangChain instrumentor bringing it to parity with the GenAI semantic conventions for spans, metrics, and events. New callback handlers: - on_chain_start/end/error: invoke_agent and invoke_workflow spans - on_tool_start/end/error: execute_tool spans with tool attributes - on_retriever_start/end/error: execute_tool spans with retriever semantics - on_llm_start: text_completion spans for non-chat models - on_llm_new_token: streaming timing metrics (TTFC, TPOC) - on_agent_action/finish: agent lifecycle support New infrastructure: - operation_mapping.py: safe callback-to-semconv classification - semconv_attributes.py: per-operation attribute matrix - span_manager.py: full span lifecycle with hierarchy tracking - utils.py: provider detection, server address, W3C propagation - message_formatting.py: structured message serialization with redaction - content_recording.py: content capture policy integration - event_emitter.py: semconv-aligned events for non-LLM spans Key capabilities: - Removed ChatOpenAI/ChatBedrock restriction, supports any LLM - Full request/response attribute extraction per semconv - Parent-child hierarchy with LangGraph support (filtering, goto, stacks) - Token usage accumulation from LLM to agent spans - W3C trace context propagation (incoming traceparent/tracestate) - Cache token attributes (cache_read, cache_creation) - Streaming metrics (time_to_first_chunk, time_per_output_chunk) - 276 unit and e2e tests Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Apply the follow-up LangChain review fixes, wire non-LLM event emission, correct experimental content capture behavior, clean ignored-run state, and repair the package's VCR-based integration tests under the current pytest-recording setup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
|
Thanks for the PR @nagkumar91. At over 7k lines of code, this is nearly impossible to approve with confidence. Could you break this up in to smaller chunks? For example, your PR desc breaks down the changes into 4 distint areas, I'd suggest trying to use those as areas to focus on per PR. |
MikeGoldsmith
requested changes
Apr 8, 2026
Member
MikeGoldsmith
left a comment
There was a problem hiding this comment.
Forgot to add a review yesterday. I'd really like to see this PR broken down into smaller parts.
nagkumar91
added a commit
to nagkumar91/opentelemetry-python-contrib
that referenced
this pull request
Apr 16, 2026
…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>
3 tasks
nagkumar91
added a commit
to nagkumar91/opentelemetry-python-contrib
that referenced
this pull request
Apr 16, 2026
…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>
3 tasks
nagkumar91
added a commit
to nagkumar91/opentelemetry-python-contrib
that referenced
this pull request
Apr 16, 2026
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>
3 tasks
nagkumar91
added a commit
to nagkumar91/opentelemetry-python-contrib
that referenced
this pull request
Apr 16, 2026
…h wiring Wire all foundation modules into the callback handler and add comprehensive callback implementations: - callback_handler.py: Enhanced with model start/end/error (improved provider and server inference), chain start/end/error (with operation classification), agent action/finish, LLM new token (streaming chunk timing metrics), tool start/end/error, and retriever start/end/error callbacks. - __init__.py: Wire SpanManager, EventEmitter, and tracer into the instrumentor's _instrument method. - conftest.py: Updated test infrastructure for enhanced callback testing. Part 4 of a series breaking down open-telemetry#4389 into smaller reviewable PRs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
3 tasks
nagkumar91
added a commit
to nagkumar91/opentelemetry-python-contrib
that referenced
this pull request
Apr 16, 2026
Add remaining test coverage for the LangChain enhanced tracing: - test_tool_callbacks.py: Tool start/end/error callbacks, content redaction, tool definitions formatting, event emission - test_retriever_callbacks.py: Retriever start/end/error callbacks, document serialization, query capture, content redaction - test_e2e_scenarios.py: End-to-end scenarios covering multi-step agent workflows, nested chains, concurrent execution, error propagation Update CHANGELOG.md with the enhancement entry. Part 5 (final) of a series breaking down open-telemetry#4389 into smaller reviewable PRs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
4 tasks
Contributor
Author
PR Split into Smaller ReviewsThis PR has been broken down into 5 smaller, independently reviewable PRs to make the review process more manageable:
Merge order: 1 → 2 → 3 → 4 → 5 (each PR includes changes from previous PRs) Once the series is merged, this PR can be closed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR expands the LangChain instrumentor toward semconv-first GenAI coverage and folds in the review/test follow-up fixes needed to make the package consistent with contrib expectations.
The main changes are:
pytest-recordingType of change
How Has This Been Tested?
python3 -m pytest instrumentation-genai/opentelemetry-instrumentation-langchain/testsuv tool run ruff check src tests(frominstrumentation-genai/opentelemetry-instrumentation-langchain)tox -e typechecktox -e spellcheckDoes This PR Require a Core Repo Change?
Checklist:
Notes
instrumentation-genai/opentelemetry-instrumentation-langchain/CHANGELOG.md.test_geminiis skipped whentests/cassettes/test_gemini.yamlis absent and no real Google API key is available for recording.