OpenTelemetry Pinecone Instrumentation

0.58.0 · active · verified Fri Apr 10

This library provides OpenTelemetry instrumentation for the Pinecone vector database client. It automatically captures spans and traces for operations, adhering to OpenTelemetry's Generative AI Semantic Conventions. As part of the `openllmetry` project, it follows a rapid release cycle, with the current version being 0.58.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up basic OpenTelemetry console tracing and instrument the Pinecone client. Ensure you have the `pinecone` library (v2+) and OpenTelemetry SDK installed. Set `PINECONE_API_KEY` and `PINECONE_ENVIRONMENT` environment variables for a runnable example.

import os
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.instrumentation.pinecone import PineconeInstrumentor
import pinecone

# 1. Setup OpenTelemetry Tracer Provider (for console output)
resource = Resource.create({"service.name": "my-pinecone-app"})
provider = TracerProvider(resource=resource)
span_processor = SimpleSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(span_processor)
trace.set_tracer_provider(provider)

# 2. Instrument Pinecone client calls
PineconeInstrumentor().instrument()

# 3. Use Pinecone as usual
api_key = os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY")
environment = os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")

if api_key == "YOUR_API_KEY" or environment == "YOUR_ENVIRONMENT":
    print("Please set PINECONE_API_KEY and PINECONE_ENVIRONMENT environment variables.")
    print("Skipping Pinecone client interaction due to missing credentials.")
elif not pinecone.list_indexes: # Check if the package is the new pinecone library
    print("It appears you are using the old `pinecone-client` library. This instrumentation requires `pinecone` (v2+).")
    print("Skipping Pinecone client interaction.")
else:
    try:
        pinecone.init(api_key=api_key, environment=environment)
        
        # This operation will be traced
        indexes = pinecone.list_indexes()
        print(f"Pinecone Indexes: {indexes}")
        print("Check your console for OpenTelemetry traces!")
    except Exception as e:
        print(f"Error interacting with Pinecone: {e}")
        print("Ensure your Pinecone API Key and Environment are correct and that you are using `pinecone` v2+.")

view raw JSON →