Skip to main content

OpenTelemetry

Project description

pypi

This library adds instrumentation to the Google GenAI SDK library to emit telemetry data following Semantic Conventions for GenAI systems. It adds trace spans for GenAI operations, events/logs for recording prompts/responses, and emits metrics that describe the GenAI operations in aggregate.

Experimental

This package is still experimental. The instrumentation may not be complete or correct just yet.

Please see “TODOS.md” for a list of known defects/TODOs that are blockers to package stability.

Installation

If your application is already instrumented with OpenTelemetry, add this package to your requirements.

pip install opentelemetry-instrumentation-google-genai

If you don’t have a Google GenAI SDK application, yet, try our examples.

Check out zero-code example for a quick start.

Usage

This section describes how to set up Google GenAI SDK instrumentation if you’re setting OpenTelemetry up manually. Check out the manual example for more details.

Instrumenting all clients

When using the instrumentor, all clients will automatically trace GenAI generate_content operations. You can also optionally capture prompts and responses as log events.

Make sure to configure OpenTelemetry tracing, logging, metrics, and events to capture all telemetry emitted by the instrumentation.

from opentelemetry.instrumentation.google_genai import GoogleGenAiSdkInstrumentor
from google.genai import Client

GoogleGenAiSdkInstrumentor().instrument()

client = Client()
response = client.models.generate_content(
    model="gemini-1.5-flash-002",
    contents="Write a short poem on OpenTelemetry."
)

Limitations

When using the Google GenAI SDK with automatic function calling enabled, the OpenTelemetry instrumentation creates a span only for the top-level generate_content call.

Internal model or tool calls triggered automatically by the SDK are executed within the SDK internals and are not traced as separate spans.

Enabling message content

Message content such as the contents of the prompt and response are not captured by default. To capture message content as log events, set the environment variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to true.

Uninstrument

To uninstrument clients, call the uninstrument method:

from opentelemetry.instrumentation.google_genai import GoogleGenAiSdkInstrumentor

GoogleGenAiSdkInstrumentor().instrument()
# ...

# Uninstrument all clients
GoogleGenAiSdkInstrumentor().uninstrument()

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file opentelemetry_instrumentation_google_genai-0.7b1.tar.gz.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_google_genai-0.7b1.tar.gz
Algorithm Hash digest
SHA256 dc81249eb8a01184e2b3e0eb207864158e7147e3307b018dc9625b23973457a5
MD5 e8eef75f95bdd017aad3d550e6a53e7f
BLAKE2b-256 5acada1a8d598542a3bd343859efc6e6e89b74b5189887b8f7ef447267e5e9ed

See more details on using hashes here.

File details

Details for the file opentelemetry_instrumentation_google_genai-0.7b1-py3-none-any.whl.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_google_genai-0.7b1-py3-none-any.whl
Algorithm Hash digest
SHA256 b7b3cda48efc9b9550dee76e4437a2f5e3b6877abffc4a6c336966eceb00f8b8
MD5 9a60c62fc9ccd024472f3cc2eea227f1
BLAKE2b-256 52596c89d9c1c72ea321b2e3f6557fa58add59c61d0ba483d90955a7d9af4ec3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page