OpenTelemetry Instrumentation for Google GenAI

0.7b0 · active · verified Tue Apr 14

This OpenTelemetry instrumentation provides automatic tracing and metrics for applications using Google's Generative AI client library (`google-generativeai`). It captures details like model calls, prompts, and responses, aligning with OpenTelemetry's GenAI semantic conventions. As part of the `opentelemetry-python-contrib` project, it is currently in beta (`0.7b0`) and subject to rapid development and potential API changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to enable OpenTelemetry instrumentation for Google Generative AI. It sets up a basic OpenTelemetry SDK with a console exporter, then initializes the `GoogleGenAIInstrumentor`. Ensure `GEMINI_API_KEY` is set in your environment for the GenAI client to function.

import os
import google.generativeai as genai

from opentelemetry import trace
from opentelemetry.instrumentation.google_genai import GoogleGenAIInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

# 1. Configure OpenTelemetry SDK
resource = Resource.create(attributes={
    "service.name": "my-genai-app",
    "application.name": "google-gemini-example"
})
provider = TracerProvider(resource=resource)
# For local testing, use ConsoleSpanExporter to print traces to console
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)

# 2. Enable Google GenAI instrumentation
GoogleGenAIInstrumentor().instrument()

# 3. Configure Google Generative AI
# Ensure GEMINI_API_KEY environment variable is set or pass directly
api_key = os.environ.get('GEMINI_API_KEY', 'YOUR_API_KEY_HERE')
if not api_key or api_key == 'YOUR_API_KEY_HERE':
    print("Warning: GEMINI_API_KEY environment variable not set. API calls might fail.")
genai.configure(api_key=api_key)

# 4. Use Google GenAI - this will now be instrumented
print("\n--- Generating Content ---")
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("generate-story"): # Custom span to wrap GenAI call
    model = genai.GenerativeModel('gemini-pro')
    try:
        response = model.generate_content("Tell me a short, imaginative story about a cat who learns to fly.")
        print("Generated story:\n" + response.text)
    except Exception as e:
        print(f"Error generating content: {e}")

print("\n--- Traces (if configured) should appear above/below ---")

view raw JSON →