44This example shows how to use the PineconeInstrumentor to automatically
55trace Pinecone operations with OpenTelemetry semantic conventions.
66"""
7+
78import os
89
910from pinecone import Pinecone , ServerlessSpec
2627# Alternative: Manual instrumentation after initialization
2728# quotient.tracer.instrument_vector_dbs("pinecone")
2829
30+
2931@quotient .trace ()
3032def demonstrate_pinecone_operations ():
3133 """Demonstrate Pinecone operations with tracing."""
3234 with start_span ("pinecone_demo" ):
33- try :
35+ try :
3436 # Check for API key
3537 api_key = os .environ .get ("PINECONE_API_KEY" )
36-
38+
3739 if not api_key :
3840 print ("Pinecone API key not set." )
3941 print ("Set PINECONE_API_KEY environment variable." )
4042 print ("You can get this from https://app.pinecone.io/" )
4143 return
42-
44+
4345 pc = Pinecone (api_key = api_key )
44-
46+
4547 # List existing indexes
4648 indexes = pc .list_indexes ()
4749 print (f"Existing indexes: { indexes } " )
48-
50+
4951 # Create index (if it doesn't exist)
5052 index_name = "test-index"
5153 if index_name not in indexes :
52- spec = ServerlessSpec (
53- cloud = "aws" ,
54- region = "us-east-1"
55- )
54+ spec = ServerlessSpec (cloud = "aws" , region = "us-east-1" )
5655 pc .create_index (
5756 name = index_name ,
5857 spec = spec ,
5958 dimension = 128 ,
6059 metric = "cosine" ,
6160 )
6261 print (f"Created index: { index_name } " )
63-
62+
6463 # Get index
6564 index = pc .Index (index_name )
66-
65+
6766 # Upsert vectors
6867 vectors = [
6968 ("id1" , [0.1 ] * 128 , {"source" : "test" , "category" : "demo" }),
7069 ("id2" , [0.2 ] * 128 , {"source" : "test" , "category" : "demo" }),
71- ("id3" , [0.3 ] * 128 , {"source" : "test" , "category" : "demo" })
70+ ("id3" , [0.3 ] * 128 , {"source" : "test" , "category" : "demo" }),
7271 ]
7372 index .upsert (vectors = vectors )
7473 print ("Upserted vectors" )
75-
74+
7675 # Query vectors
7776 query_vector = [0.1 ] * 128
78- results = index .query (
79- vector = query_vector ,
80- top_k = 3 ,
81- include_metadata = True
82- )
77+ results = index .query (vector = query_vector , top_k = 3 , include_metadata = True )
8378 print (f"Query results: { results } " )
84-
79+
8580 # Fetch specific vectors
8681 fetch_results = index .fetch (ids = ["id1" , "id2" ])
8782 print (f"Fetch results: { fetch_results } " )
88-
83+
8984 # Update metadata
9085 index .update (
91- id = "id1" ,
92- set_metadata = {"source" : "updated" , "category" : "modified" }
86+ id = "id1" , set_metadata = {"source" : "updated" , "category" : "modified" }
9387 )
9488 print ("Updated metadata" )
95-
89+
9690 # Delete vectors
9791 index .delete (ids = ["id3" ])
9892 print ("Deleted vector id3" )
99-
93+
10094 except ImportError :
10195 print ("Pinecone not installed. Install with: pip install pinecone" )
10296 except Exception as e :
@@ -107,15 +101,17 @@ def demonstrate_pinecone_operations():
107101if __name__ == "__main__" :
108102 print ("Starting Pinecone Tracing Demo..." )
109103 print ("=" * 50 )
110-
104+
111105 # Run demonstrations
112106 demonstrate_pinecone_operations ()
113-
107+
114108 print ("=" * 50 )
115109 print ("Pinecone demo completed! Check your tracing dashboard for spans." )
116110 print ("\n Pinecone spans will include these semantic conventions:" )
117111 print ("- db.system.name: 'pinecone'" )
118- print ("- db.operation: 'create_index', 'upsert', 'query', 'fetch', 'update', 'delete', 'describe_index_stats'" )
112+ print (
113+ "- db.operation: 'create_index', 'upsert', 'query', 'fetch', 'update', 'delete', 'describe_index_stats'"
114+ )
119115 print ("- db.collection.name: index name" )
120116 print ("- db.ids_count: number of IDs processed" )
121117 print ("- db.vector_count: number of vectors processed" )
0 commit comments