Splunk OpenTelemetry LlamaIndex instrumentation
Project description
This library provides automatic instrumentation for LlamaIndex applications using OpenTelemetry.
Installation
Development installation:
# Install the package in editable mode cd instrumentation-genai/opentelemetry-instrumentation-llamaindex pip install -e . # Install test dependencies pip install -e ".[test]" # Install util-genai (required for telemetry) cd ../../util/opentelemetry-util-genai pip install -e .
Quick Start
import os
from opentelemetry.instrumentation.llamaindex import LlamaindexInstrumentor
from opentelemetry import trace, metrics
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
# Enable metrics (default is spans only)
os.environ["OTEL_INSTRUMENTATION_GENAI_EMITTERS"] = "span_metric"
# Setup tracing
trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
SimpleSpanProcessor(ConsoleSpanExporter())
)
# Setup metrics
metric_reader = InMemoryMetricReader()
meter_provider = MeterProvider(metric_readers=[metric_reader])
metrics.set_meter_provider(meter_provider)
# Enable instrumentation with providers
LlamaindexInstrumentor().instrument(
tracer_provider=trace.get_tracer_provider(),
meter_provider=meter_provider
)
# Use LlamaIndex as normal
from llama_index.llms.openai import OpenAI
from llama_index.core.llms import ChatMessage, MessageRole
llm = OpenAI(model="gpt-3.5-turbo")
messages = [ChatMessage(role=MessageRole.USER, content="Hello")]
response = llm.chat(messages)
Running Tests
LLM Tests:
# Set environment variables
export OPENAI_API_KEY=your-api-key
export OTEL_INSTRUMENTATION_GENAI_EMITTERS=span_metric
# Run the test
cd tests
python test_llm_instrumentation.py
Embedding Tests:
# Set environment variables
export OPENAI_API_KEY=your-api-key
export OTEL_INSTRUMENTATION_GENAI_EMITTERS=span_metric
# Run the test
cd tests
python test_embedding_instrumentation.py
Expected Output
LLM Span Attributes:
{
"gen_ai.framework": "llamaindex",
"gen_ai.request.model": "gpt-3.5-turbo",
"gen_ai.operation.name": "chat",
"gen_ai.usage.input_tokens": 24,
"gen_ai.usage.output_tokens": 7
}
Embedding Span Attributes:
{
"gen_ai.operation.name": "embeddings",
"gen_ai.request.model": "text-embedding-3-small",
"gen_ai.provider.name": "openai",
"gen_ai.embeddings.dimension.count": 1536
}
Metrics:
Metric: gen_ai.client.operation.duration Duration: 0.6900 seconds Count: 1 Metric: gen_ai.client.token.usage Token type: input, Sum: 24, Count: 1 Token type: output, Sum: 7, Count: 1
Key Implementation Differences from LangChain
1. Event-Based Callbacks
LlamaIndex uses on_event_start(event_type, ...) and on_event_end(event_type, ...) instead of LangChain’s method-based callbacks (on_llm_start, on_llm_end).
Event types are dispatched via CBEventType enum:
CBEventType.LLM # LLM invocations (chat, complete) CBEventType.AGENT # Agent steps (not yet instrumented) CBEventType.EMBEDDING # Embedding operations (get_text_embedding, get_text_embedding_batch)
2. Handler Registration
LlamaIndex uses handlers list:
callback_manager.handlers.append(handler)
LangChain uses inheritable_handlers:
callback_manager.inheritable_handlers.append(handler)
3. Response Structure
LlamaIndex ChatMessage uses blocks (list of TextBlock objects):
message.content # Computed property from blocks[0].text
LangChain uses simple strings:
message.content # Direct string property
4. Token Usage
LlamaIndex returns objects (not dicts):
response.raw.usage.prompt_tokens # Object attribute response.raw.usage.completion_tokens # Object attribute
LangChain returns dicts:
response["usage"]["prompt_tokens"] # Dict key response["usage"]["completion_tokens"] # Dict key
Supported Features
LLM Operations
✅ Chat completion (llm.chat(), llm.stream_chat())
✅ Text completion (llm.complete(), llm.stream_complete())
✅ Token usage tracking
✅ Model name detection
✅ Framework attribution
Embedding Operations
✅ Single text embedding (embed_model.get_text_embedding())
✅ Batch embedding (embed_model.get_text_embedding_batch())
✅ Query embedding (embed_model.get_query_embedding())
✅ Provider detection (OpenAI, Azure, AWS Bedrock, Google, Cohere, HuggingFace, Ollama, and more)
✅ Dimension count tracking
✅ Input text capture
Provider Detection
Embedding instrumentation automatically detects the provider from class names:
OpenAI: OpenAIEmbedding
Azure: AzureOpenAIEmbedding
AWS: BedrockEmbedding
Google: GeminiEmbedding, VertexTextEmbedding, GooglePaLMEmbedding
Cohere: CohereEmbedding
HuggingFace: HuggingFaceEmbedding, HuggingFaceInferenceAPIEmbedding
Ollama: OllamaEmbedding
Anthropic: AnthropicEmbedding
MistralAI: MistralAIEmbedding
Together: TogetherEmbedding
Fireworks: FireworksEmbedding
Voyage: VoyageEmbedding
Jina: JinaEmbedding
References
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file splunk_otel_instrumentation_llamaindex-0.1.1.tar.gz.
File metadata
- Download URL: splunk_otel_instrumentation_llamaindex-0.1.1.tar.gz
- Upload date:
- Size: 26.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Hatch/1.16.3 cpython/3.11.14 HTTPX/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
649d429939a83dad47c9d2687c3b3a6a4304eea60de4fb5a8e9d18604cdfc41a
|
|
| MD5 |
76cc47ea5db3f9c839c4e6f07e017888
|
|
| BLAKE2b-256 |
d4ae5dff06d16abb0c678f15e0e9b10ed6df09841166700ff8e72af904e7d38f
|
File details
Details for the file splunk_otel_instrumentation_llamaindex-0.1.1-py3-none-any.whl.
File metadata
- Download URL: splunk_otel_instrumentation_llamaindex-0.1.1-py3-none-any.whl
- Upload date:
- Size: 20.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: Hatch/1.16.3 cpython/3.11.14 HTTPX/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5438d171cf18c4cc69d7c1b94b4cddd902cd840f238e3cb18e550b84a55ec40e
|
|
| MD5 |
fa19662f08eb8a9c6d06a4eb7aad3cda
|
|
| BLAKE2b-256 |
b63273922822e3f7b15dd87d4349a1239714ff45265a24a0c2e3ad1b3861a669
|