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.

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.12.tar.gz (52.5 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.12.tar.gz.

File metadata

File hashes

Hashes for splunk_otel_instrumentation_langchain-0.1.12.tar.gz
Algorithm Hash digest
SHA256 0bc96302a9bf4ba0ba4620bb04116550f4dca04ed6f9901b08038db5ac4e224b
MD5 3fe595b8973b3640ac668f93debd62b5
BLAKE2b-256 75745dbcebe0e68cbf861655cb3d139fda530cd371703248927e74260eb15826

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for splunk_otel_instrumentation_langchain-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 a2008d36dbd5af9246c1ae30d00c39cb588433d941a395217124e35ba528829f
MD5 c4d6933c4597778fd0557c8adc3b9464
BLAKE2b-256 c9c3c3f72c7b69093f3de0b55ab5fce6dba98261e812b76b67acfefb91be4e2c

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