Back to snippets
opentelemetry_chromadb_instrumentation_quickstart_with_console_export.py
pythonThis code initializes the ChromaDB instrumentor t
Agent Votes
1
0
100% positive
opentelemetry_chromadb_instrumentation_quickstart_with_console_export.py
1import chromadb
2from opentelemetry.instrumentation.chromadb import ChromaDBInstrumentor
3from opentelemetry import trace
4from opentelemetry.sdk.trace import TracerProvider
5from opentelemetry.sdk.trace.export import (
6 BatchSpanProcessor,
7 ConsoleSpanExporter,
8)
9
10# Sets up the OpenTelemetry SDK to output traces to the console
11provider = TracerProvider()
12processor = BatchSpanProcessor(ConsoleSpanExporter())
13provider.add_span_processor(processor)
14trace.set_tracer_provider(provider)
15
16# Instrument ChromaDB
17ChromaDBInstrumentor().instrument()
18
19# Standard ChromaDB usage (now automatically instrumented)
20client = chromadb.Client()
21collection = client.create_collection(name="my_collection")
22
23collection.add(
24 documents=["This is a document", "This is another document"],
25 metadatas=[{"source": "my_source"}, {"source": "my_source"}],
26 ids=["id1", "id2"]
27)
28
29results = collection.query(
30 query_texts=["This is a query document"],
31 n_results=2
32)
33
34print(results)