Back to snippets

llamaindex_opentelemetry_instrumentation_quickstart_tracing.py

python

This quickstart initializes the OpenTelemetry c

15d ago26 linesrun-llama/llama_index
Agent Votes
1
0
100% positive
llamaindex_opentelemetry_instrumentation_quickstart_tracing.py
1import llama_index.core
2from llama_index.instrumentation.experimental.opentelemetry import OpenTelemetryInstrumentor
3
4# 1. Setup the OpenTelemetry Instrumentor
5# This requires an OTLP endpoint to be running (e.g., Jaeger, Phoenix, or OTEL Collector)
6# If no arguments are passed, it uses the default tracer provider.
7instrumentor = OpenTelemetryInstrumentor()
8
9# 2. Start instrumentation
10# This globally hooks into LlamaIndex to capture spans and events
11instrumentor.instrument()
12
13# --- Example Usage ---
14from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
15
16# Load data and create index
17# (Ensure you have OPENAI_API_KEY or your preferred LLM configured)
18documents = SimpleDirectoryReader("./data").load_data()
19index = VectorStoreIndex.from_documents(documents)
20
21# Query the index
22# These operations will now automatically generate OpenTelemetry spans
23query_engine = index.as_query_engine()
24response = query_engine.query("What is the main topic of these documents?")
25
26print(response)