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.15.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.15.tar.gz.

File metadata

  • Download URL: splunk_otel_instrumentation_langchain-0.1.15.tar.gz
  • Upload date:
  • Size: 52.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.17.1 {"ci":true,"cpu":"x86_64","distro":{"id":"trixie","libc":{"lib":"glibc","version":"2.41"},"name":"Debian GNU/Linux","version":"13"},"implementation":{"name":"CPython","version":"3.11.15"},"installer":{"name":"hatch","version":"1.17.1"},"openssl_version":"OpenSSL 3.5.6 7 Apr 2026","python":"3.11.15","system":{"name":"Linux","release":"5.15.0-1084-aws"}} HTTPX2/2.9.0

File hashes

Hashes for splunk_otel_instrumentation_langchain-0.1.15.tar.gz
Algorithm Hash digest
SHA256 a29b2ffabf73906fe491d4f62edb4bec234a47d817943dc8e40cf40a67b4106e
MD5 87557da292d0f6ef269f46a7c7a9a5aa
BLAKE2b-256 b8ef072e7d39b6881cd7b343a79df7bc5da432370cfa65599649dd7708dff9e3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: splunk_otel_instrumentation_langchain-0.1.15-py3-none-any.whl
  • Upload date:
  • Size: 31.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.17.1 {"ci":true,"cpu":"x86_64","distro":{"id":"trixie","libc":{"lib":"glibc","version":"2.41"},"name":"Debian GNU/Linux","version":"13"},"implementation":{"name":"CPython","version":"3.11.15"},"installer":{"name":"hatch","version":"1.17.1"},"openssl_version":"OpenSSL 3.5.6 7 Apr 2026","python":"3.11.15","system":{"name":"Linux","release":"5.15.0-1084-aws"}} HTTPX2/2.9.0

File hashes

Hashes for splunk_otel_instrumentation_langchain-0.1.15-py3-none-any.whl
Algorithm Hash digest
SHA256 ae4f5b849806b296eea0e884ebe9a7c9abd43cf6336ed2e612e410b8583d7bc5
MD5 728b776c4286c2da524f99cc606db05c
BLAKE2b-256 2d5d3d9babb3a198cc495a6ff9a231dfe6e32ee4ee024a5756e627b6086bbbb0

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