Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fuser-guides%2Fai%2Fotel-integration%2Fcode-examples%2Fgoogle-genai.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fuser-guides%2Fai%2Fotel-integration%2Fcode-examples%2Fgoogle-genai.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

# Google GenAI

Use the `GoogleGenAiSdkInstrumentor` from `opentelemetry-instrumentation-google-genai` to auto-instrument the Google GenAI SDK. Traces are exported to a **local OpenTelemetry Collector** over OTLP/gRPC; the collector forwards to Coralogix.

Before you start

These examples export to a local OpenTelemetry Collector over OTLP/gRPC. Deploy the collector first — see [Code examples → Deploy an OpenTelemetry Collector](https://docs-docusaurus.kinsta.page/user-guides/ai/otel-integration/code-examples/.md#prerequisites-deploy-an-opentelemetry-collector).

## Install[​](#install "Direct link to Install")

```
pip install google-genai opentelemetry-instrumentation-google-genai opentelemetry-sdk opentelemetry-exporter-otlp-proto-grpc
```

## Environment variables[​](#environment-variables "Direct link to Environment variables")

```
export GOOGLE_API_KEY="<YOUR_GOOGLE_API_KEY>"

# Point to your local OTel Collector (which forwards to Coralogix)

export OTEL_EXPORTER_OTLP_ENDPOINT="http://<COLLECTOR_HOST>:4317"

export OTEL_EXPORTER_OTLP_INSECURE="true"

export OTEL_SEMCONV_STABILITY_OPT_IN="gen_ai_latest_experimental"

export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT="SPAN_AND_EVENT"
```

## Script[​](#script "Direct link to Script")

```
import os

from google import genai



# --- OTel imports ---

from opentelemetry import trace

from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

from opentelemetry.instrumentation.google_genai import GoogleGenAiSdkInstrumentor

from opentelemetry.sdk.resources import SERVICE_NAME, Resource

from opentelemetry.sdk.trace import TracerProvider

from opentelemetry.sdk.trace.export import BatchSpanProcessor





# --- OTel setup: configure tracer provider and OTLP exporter ---

def configure_otel() -> TracerProvider:

    resource = Resource.create({

        SERVICE_NAME: "my-genai-service",

        "cx.application.name": "my-genai-app",

        "cx.subsystem.name": "my-service",

    })

    provider = TracerProvider(resource=resource)

    endpoint = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://<COLLECTOR_HOST>:4317")

    insecure = os.getenv("OTEL_EXPORTER_OTLP_INSECURE", "true").lower() == "true"

    provider.add_span_processor(

        BatchSpanProcessor(OTLPSpanExporter(endpoint=endpoint, insecure=insecure))

    )

    trace.set_tracer_provider(provider)

    return provider





def main():

    # OTel: initialize tracing, then auto-instrument the Google GenAI SDK

    provider = configure_otel()

    GoogleGenAiSdkInstrumentor().instrument()



    # Your app logic

    client = genai.Client()

    response = client.models.generate_content(

        model="gemini-2.0-flash",

        contents="What is OpenTelemetry in one sentence?",

    )

    print(f"Gemini response: {response.text}")



    # OTel: flush and shut down the tracer provider

    provider.force_flush()

    provider.shutdown()





if __name__ == "__main__":

    main()
```

## Expected span attributes[​](#expected-span-attributes "Direct link to Expected span attributes")

* `gen_ai.provider.name` = `"google_genai"`
* `gen_ai.request.model` = `"gemini-2.0-flash"`
* `gen_ai.operation.name` = `"chat"`
* `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`
* `gen_ai.input.messages`, `gen_ai.output.messages` (with content capture)

Tip

Put your Coralogix Send-Your-Data API key on the **collector** (e.g. `CORALOGIX_PRIVATE_KEY` in `otel-collector-config.yaml`), not in the Python exporter. The exporter only needs to reach the local collector.

## Next steps[​](#next-steps "Direct link to Next steps")

Look up which open-source library to use for your provider in [Compatibility matrix](https://docs-docusaurus.kinsta.page/user-guides/ai/otel-integration/providers/.md).
