OpenTelemetry Instrumentation for Google Generative AI

OpenTelemetry auto-instrumentation for Google Gemini and Generative AI calls in Python

Library
PyPI
v0.62.3
7,383stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
81/100Excellent
Development Activity76
Maintenance88
Community72
Maturity48
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
80/100Excellent
Architecture80
Code Quality80
Innovation76
Learning Curve84

opentelemetry-instrumentation-google-generativeai is part of Traceloop’s OpenLLMetry suite and provides OpenTelemetry instrumentation for Google’s Generative AI (Gemini) Python library. Once enabled, it automatically traces prompts and completions, emitting spans and metrics that follow OpenTelemetry semantic conventions for LLM workloads, so Gemini usage shows up in any OTLP-compatible observability backend without manual span code.

Instrumentation is a single call, GoogleGenerativeAiInstrumentor().instrument(), after which model calls are wrapped transparently. It captures request and response content by default (which can be disabled for privacy), records token and latency metrics, and integrates with the broader OpenTelemetry Python ecosystem.

What You Get

  • One-line instrumentation of Google Gemini / Generative AI calls via GoogleGenerativeAiInstrumentor
  • Spans following OpenTelemetry GenAI semantic conventions for prompts and completions
  • Captured request/response content, configurable off for privacy via TRACELOOP_TRACE_CONTENT
  • Token usage and latency metrics emitted alongside traces
  • Registration as a standard OpenTelemetry instrumentor entry point for auto-loading

Common Use Cases

  • Observing and debugging Gemini-powered features in production
  • Measuring token consumption and latency of Google Generative AI calls
  • Feeding LLM traces into an existing OpenTelemetry/OTLP backend
  • Evaluating output quality by inspecting logged prompts and completions

Under The Hood

Architecture - The package lives under opentelemetry/instrumentation/google_generativeai/. __init__.py defines GoogleGenerativeAiInstrumentor (subclassing OpenTelemetry’s BaseInstrumentor), which patches the Gemini client’s generate-content methods; span_utils.py builds spans and sets GenAI attributes, event_emitter.py/event_handler.py/event_models.py handle the newer event-based semantic conventions, and config.py/utils.py cover content-capture toggles and helpers. It supports both sync and async call paths.

Tech Stack - Python 3.10+, built on opentelemetry-api, opentelemetry-instrumentation, and opentelemetry-semantic-conventions(-ai), with google-genai as an optional instruments extra. Packaged with a PEP 621 pyproject and managed in the OpenLLMetry monorepo via uv/poetry.

Code Quality - The package ships a real test suite (tests/) covering sync and async generation, finish reasons, metrics, and the new library instrumentation path, using pytest with VCR cassettes (pytest-recording) to replay real Gemini responses deterministically. Ruff is configured for linting.

API Design - Developer experience is excellent: a single instrument() call activates tracing with zero changes to model-call sites, privacy is one environment variable away, and it slots into standard OpenTelemetry auto-instrumentation through its instrumentor entry point, so it behaves like every other OTel instrumentation package.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search