Skip to main content

OpenTelemetry Official Langchain instrumentation

Project description

This package provides OpenTelemetry instrumentation for LangChain LLM/chat workflows. It leverages Splunk distribution of opentelemetry-util-genai for producing telemetry in semantic convention. Core concepts, high-level usage and configuration

Status: Alpha (APIs and produced telemetry are subject to change).

Installation

Install from source:

pip install -e splunk-otel-instrumentation-langchain

This will pull in required OpenTelemetry core + opentelemetry-util-genai.

Quick Start

Manual Instrumentation (development/debugging)

from opentelemetry.instrumentation.langchain import LangChainInstrumentor
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage

# manual instrumentation, easy to debug in your IDE
LangChainInstrumentor().instrument()

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.0)
messages = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="What is the capital of France?"),
]
response = llm.invoke(messages)
print(response.content)

Zero-code instrumentation

in zero-code instrumentation mode, ensure you install opentelemetry-distribution and run you app with the OpenTelemetry LangChain Instrumentor enabled:

.. code:: bash

opentelemetry-instrument python your_langchain_app.py

from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.0)
messages = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="What is the capital of France?"),
]
response = llm.invoke(messages)
print(response.content)

Embedding Support

The instrumentation wraps embed_documents() and embed_query() methods for embedding classes (e.g., OpenAIEmbeddings, AzureOpenAIEmbeddings).

Token Counting: For token usage metrics, the instrumentation uses tiktoken to count input tokens client-side. This is necessary because:

  • LangChain’s embedding methods only return embedding vectors (list[list[float]])

  • The API’s usage response is discarded internally by LangChain

  • LangChain has no embedding callbacks (unlike LLM callbacks)

tiktoken is already a required dependency of langchain-openai, so no additional installation is needed when using OpenAI embeddings. If tiktoken is unavailable (e.g., for non-OpenAI providers), token metrics are gracefully omitted.

Interrupt/Resume Support

The instrumentation automatically detects LangGraph interrupt/resume patterns:

  • Error classificationGraphInterrupt, NodeInterrupt, and Interrupt exceptions are classified as interrupts (span status left as UNSET instead of ERROR). CancelledError and TaskCancelledError are classified as cancellations.

  • Conversation IDthread_id from LangGraph checkpoint metadata is extracted and set as gen_ai.conversation.id on root workflow/agent spans.

See the examples/multi_agent_travel_planner demo for interrupt/resume in action.

Known LangGraph Compatibility Issue

langgraph==1.1.7 introduced a breaking change that silently drops non-GraphCallbackHandler callback handlers. This prevents the instrumentation from receiving any callbacks. The issue was fixed in langgraph 1.1.8. This package excludes langgraph==1.1.7 via != 1.1.7 in its dependency specifier.

Testing

Run the package tests (from repository root or this directory):

pytest -k langchain instrumentation-genai/opentelemetry-instrumentation-langchain-alpha/tests

(Recorded cassettes or proper API keys may be required for full integration tests.)

Contributing

Issues / PRs welcome in the main otel-splunk-python-contrib repository. This module is alpha: feedback on attribute coverage, performance, and LangChain surface expansion is especially helpful.

License

Apache 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

splunk_otel_instrumentation_langchain-0.1.14.tar.gz (52.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file splunk_otel_instrumentation_langchain-0.1.14.tar.gz.

File metadata

File hashes

Hashes for splunk_otel_instrumentation_langchain-0.1.14.tar.gz
Algorithm Hash digest
SHA256 1fe03984bd005faea1720f1624ea8d4e706a8e501efd31396e2fdc7ddcbeb661
MD5 eb5b5e8fd65dc66933ea5c18a487d116
BLAKE2b-256 ff0acf77a3d85ff03d35ed8bf9950d03b64697d5cde9478d2d8209ffc606b542

See more details on using hashes here.

File details

Details for the file splunk_otel_instrumentation_langchain-0.1.14-py3-none-any.whl.

File metadata

File hashes

Hashes for splunk_otel_instrumentation_langchain-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 ebf5c7f16d73b1c676abbc9adfe5883c1c0e1f5bae4e2fef0c895b650dfcf5aa
MD5 c3a1df2315f8f65e18f0eb7bd57adc37
BLAKE2b-256 78ddc60fc415b8d399239c7d4b22229bad641f8ff2f4b49fb144e11aa8fc6722

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page