Skip to content

Add otel langchain instrumentation - #20

Closed
Radhika Gupta (rads-1996) wants to merge 20 commits into
microsoft:mainfrom
rads-1996:add-otel-langchain-instrumentation
Closed

Radhika Gupta (rads-1996) wants to merge 20 commits into
microsoft:mainfrom
rads-1996:add-otel-langchain-instrumentation

Conversation

@rads-1996

Copy link
Copy Markdown
Member

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a new opentelemetry-instrumentation-langchain package that instruments LangChain chat model executions (notably ChatOpenAI / ChatBedrock) using OpenTelemetry GenAI semantic conventions, along with a test suite that validates spans, metrics, and logs via VCR cassettes.

Changes:

  • Added a LangChain instrumentor that injects an OpenTelemetry callback handler into LangChain’s callback manager.
  • Implemented an invocation-state manager and callback handler logic to start/stop/fail LLM invocations and populate GenAI attributes.
  • Added pytest coverage (including VCR cassettes) to assert emitted spans/metrics/logs for success and error scenarios.

Reviewed changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated 19 comments.

Show a summary per file
File Description
src/_langchain/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/init.py Defines LangChainInstrumentor and wrap/unwrap wiring for callback manager injection
src/_langchain/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/callback_handler.py Implements LangChain callback handler to build GenAI invocation objects and stop/fail them
src/_langchain/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/invocation_manager.py Stores invocation state keyed by run_id with basic parent/child tracking
src/_langchain/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/span_manager.py Adds a span manager abstraction for run_id-based span lifecycle management
src/_langchain/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/package.py Declares instrumentation dependency metadata
src/_langchain/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/version.py Adds package version definition
src/_langchain/opentelemetry-instrumentation-langchain/tests/conftest.py Test fixtures for OTel SDK in-memory exporters + VCR configuration/scrubbing
src/_langchain/opentelemetry-instrumentation-langchain/tests/test_llm_call.py End-to-end assertions for spans/metrics/logs for ChatOpenAI invoke success/error
src/_langchain/opentelemetry-instrumentation-langchain/tests/test_invocation_manager.py Unit tests for _InvocationManager behavior
src/_langchain/opentelemetry-instrumentation-langchain/tests/cassettes/test_chat_openai_gpt_3_5_turbo_model_llm_call.yaml Recorded success response cassette
src/_langchain/opentelemetry-instrumentation-langchain/tests/cassettes/test_chat_openai_gpt_3_5_turbo_model_llm_call_with_error.yaml Recorded auth error response cassette
src/_langchain/opentelemetry-instrumentation-langchain/pyproject.toml Hatch-based packaging config for the new instrumentation package
src/_langchain/opentelemetry-instrumentation-langchain/dev_requirements.txt Dev/test requirements for the new package

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/_langchain/opentelemetry-instrumentation-langchain/tests/conftest.py Outdated
@rads-1996
Radhika Gupta (rads-1996) force-pushed the add-otel-langchain-instrumentation branch 2 times, most recently from f014441 to 664eb55 Compare April 7, 2026 21:20
@rads-1996
Radhika Gupta (rads-1996) force-pushed the add-otel-langchain-instrumentation branch from bad068e to f20a9f5 Compare April 8, 2026 00:10
@@ -0,0 +1,122 @@
# Copyright The OpenTelemetry Authors

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need to copy the code over or can we take a reference to OTel instrumentation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This instrumentation package in otel is not published yet, therefore we need to copy the code till the upstream package is released and in a good state. Until then we will maintain this internally in the distro.

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.

3 participants