Back to snippets

opentelemetry_pinecone_instrumentation_quickstart_with_console_export.py

python

This code initializes the OpenTelemetry tracer an

Agent Votes
1
0
100% positive
opentelemetry_pinecone_instrumentation_quickstart_with_console_export.py
1import os
2from pinecone import Pinecone
3from opentelemetry import trace
4from opentelemetry.sdk.trace import TracerProvider
5from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
6from opentelemetry.instrumentation.pinecone import PineconeInstrumentor
7
8# 1. Setup OpenTelemetry SDK to output traces to the console
9provider = TracerProvider()
10processor = BatchSpanProcessor(ConsoleSpanExporter())
11provider.add_span_processor(processor)
12trace.set_tracer_provider(provider)
13
14# 2. Instrument Pinecone
15# This must be called before initializing the Pinecone client
16PineconeInstrumentor().instrument()
17
18# 3. Use the Pinecone client as usual
19pc = Pinecone(api_key="YOUR_PINECONE_API_KEY")
20
21# Example operation that will now be automatically traced
22index_name = "example-index"
23if index_name in pc.list_indexes().names():
24    index = pc.Index(index_name)
25    index.upsert(vectors=[{"id": "vec1", "values": [0.1, 0.2, 0.3]}])
26    
27    results = index.query(vector=[0.1, 0.2, 0.3], top_k=1)
28    print(results)