LlamaIndex Instrumentation

0.5.0 · active · verified Thu Apr 09

The `llama-index-instrumentation` library provides robust observability tools for LlamaIndex applications, enabling tracing and logging capabilities, particularly through OpenTelemetry. It helps developers understand the flow and performance of their RAG pipelines. The current version is 0.5.0, and it's part of the frequently updated LlamaIndex ecosystem.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `OpenTelemetryInstrumentation` for a LlamaIndex application. It includes the essential OpenTelemetry configuration to ensure traces are emitted (in this case, to the console), then enables the LlamaIndex instrumentation, performs a simple LLM call, and finally disables the instrumentation. The `opentelemetry-sdk` and an exporter (like `ConsoleSpanExporter` or `OTLPSpanExporter`) must be installed for traces to be processed.

import os
from llama_index.core.llms import MockLLM
from llama_index.core.settings import Settings
from llama_index_instrumentation.opentelemetry import OpenTelemetryInstrumentation

# OpenTelemetry setup (CRITICAL for traces to be visible)
# For a real application, you'd configure an OTLP/Jaeger exporter, etc.
# This example prints traces to console.
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

resource = Resource.create({"service.name": "my-llama-app"})
provider = TracerProvider(resource=resource)
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)

# 1. Initialize OpenTelemetry Instrumentation
instrumentation = OpenTelemetryInstrumentation(tracer_provider=trace.get_tracer_provider())

# 2. Enable instrumentation
instrumentation.enable()

# 3. Configure LlamaIndex LLM
Settings.llm = MockLLM()

# 4. Perform a LlamaIndex operation
response = Settings.llm.complete("Tell me a short story about a dragon.")
print(f"LLM Response: {response.text[:50]}...")

# 5. (Optional) Disable instrumentation when no longer needed
instrumentation.disable()

view raw JSON →