> ## Documentation Index
> Fetch the complete documentation index at: https://agent-observability-docs.splunk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Google ADK (OpenTelemetry)

> Learn how to integrate a Google ADK project with Splunk Agent Observability using OpenTelemetry and OpenInference

Splunk Agent Observability supports logging traces from Google ADK applications using OpenTelemetry and OpenInference.

<Tip>
  For a simpler integration with automatic tracing, consider using the [native Splunk Agent Observability-adk package](/sdk-api/third-party-integrations/google-adk/google-adk-native) instead.
</Tip>

## Set up OpenTelemetry

To log Google ADK agents using OpenInference, the first step is to set up OpenTelemetry.

<Steps>
  <Step title="Installation">
    Add the OpenTelemetry packages to your project:

    <CodeGroup>
      ```bash Terminal theme={null}
      pip install opentelemetry-api opentelemetry-sdk \
                  opentelemetry-exporter-otlp
      ```
    </CodeGroup>

    The `opentelemetry-api` and `opentelemetry-sdk` packages provide the core OpenTelemetry functionality. The `opentelemetry-exporter-otlp` package enables sending traces to Splunk Agent Observability's OTLP endpoint.
  </Step>

  <Step title="Create environment variables for your Splunk Agent Observability settings">
    Set environment variables for your Splunk Agent Observability settings, for example in a `.env` file.
    These environment variables are consumed by the `SplunkAOSpanProcessor`to authenticate
    and route traces to the correct Splunk Agent Observability Project and Agent Stream:

    <CodeGroup>
      ```ini .env theme={null}
      # Provide your Splunk Agent Observability
      # API key and URL if you are using an
      # on-premises, standalone, or custom deployment
      # SPLUNK_AO_API_KEY="your-splunk-ao-api-key"
      # SPLUNK_AO_CONSOLE_URL="your-splunk-ao-url"

      # Your Splunk Agent Observability project name
      SPLUNK_AO_PROJECT="your-splunk-ao-project-name"

      # The name of the Agent Stream you want to use for logging
      SPLUNK_AO_AGENT_STREAM="your-splunk-ao-agent-stream"
      ```
    </CodeGroup>
  </Step>

  <Step title="Self hosted deployments: Set the OTel endpoint">
    <Note>
      Skip this step if you are using a hosted version.
    </Note>

    The OTel endpoint is different from Splunk Agent Observability's regular API endpoint and is specifically designed to receive telemetry data in the OTLP format.

    If you are using:

    * A cloud deployment, then you don't need to provide a custom OTel endpoint.
      The default endpoint `<your-splunk-ao-api-url>/otel/traces` will be used automatically.

    * A **self-hosted deployment**, replace the `<your-splunk-ao-api-url>/otel/traces` endpoint with your deployment URL. The format of this URL is based on your console URL, appending `/otel/traces`.

    The convention is to store this in the `SPLUNK_AO_CONSOLE_URL` environment variable. For example:

    <CodeGroup>
      ```python Python theme={null}
      os.environ["SPLUNK_AO_CONSOLE_URL"] = "<your-splunk-ao-url>"
      ```
    </CodeGroup>
  </Step>

  <Step title="Initialize and create the Splunk Agent Observability span processor">
    The `SplunkAOSpanProcessor` automatically configures authentication
    and metadata using your environment variables. It also:

    * Auto-builds OTLP headers using your Splunk Agent Observability credentials
    * Configures the correct OTLP trace endpoint
    * Registers a batch span processor that exports traces to Splunk Agent Observability

    <CodeGroup>
      ```python Python theme={null}
      from splunk_ao import otel  

      # SplunkAOSpanProcessor (no manual OTLP config required) loads the env vars for 
      # the Splunk Agent Observability API key, Project, and Agent Stream. Make sure to set them first. 
      splunk_ao_span_processor = otel.SplunkAOSpanProcessor(
          # Optional parameters if not set, uses env var
          # project=os.environ["SPLUNK_AO_PROJECT"], 
          # agentstream=os.environ.get("SPLUNK_AO_AGENT_STREAM"),  
      )
      ```
    </CodeGroup>
  </Step>

  <Step title="Register the span processor">
    The span processor can now be registered with an OTel trace provider.

    <CodeGroup>
      ```python Python theme={null}
      from opentelemetry.sdk import trace as trace_sdk

      tracer_provider = trace_sdk.TracerProvider()
      tracer_provider.add_span_processor(span_processor)
      ```
    </CodeGroup>
  </Step>
</Steps>

## Log a Google ADK agent using OpenInference

Once OpenTelemetry is configured, you can use OpenInference to log traces.

<Steps>
  <Step title="Install the Google ADK OpenInference support">
    To support OpenInference with the Google ADK, you need to install the OpenInference instrumentor.

    <CodeGroup>
      ```bash Terminal theme={null}
      pip install openinference-instrumentation-google-adk
      ```
    </CodeGroup>
  </Step>

  <Step title="Configure the instrumentor">
    You can now create a `GoogleADKInstrumentor` instance and instrument it with the trace provider you created earlier.

    <CodeGroup>
      ```python Python theme={null}
      from openinference.instrumentation.google_adk import GoogleADKInstrumentor

      GoogleADKInstrumentor().instrument(tracer_provider=tracer_provider)
      ```
    </CodeGroup>

    When you run your Google ADK code, traces will be logged to Splunk Agent Observability.
  </Step>
</Steps>

## Full example

Here is a full example based off the [Google ADK quickstart](https://google.github.io/adk-docs/get-started/python/#create-an-agent-project). You can find this project in the [Splunk Agent Observability SDK examples repo](https://github.com/splunk/splunk-ao-python/tree/main/examples/agent/google-adk).

To run this example, create a `.env` file with the following values set, or set them as environment variables:

```ini .env theme={null}
# AWS environment variables
GOOGLE_API_KEY=your-google-api-key

# Splunk Agent Observability environment variables
SPLUNK_AO_API_ENDPOINT=your-splunk-ao-otel-api-endpoint
# Only required for on-premises, standalone, or custom deployments
# SPLUNK_AO_API_KEY=your-splunk-ao-api-key
SPLUNK_AO_PROJECT=your-splunk-ao-project
SPLUNK_AO_AGENT_STREAM=your-agent-stream
```

Remember to update these to match your [Splunk Agent Observability API key](/references/faqs/find-keys#splunk-ao-api-key), Google API key, [Splunk Agent Observability OTel endpoint](/sdk-api/third-party-integrations/opentelemetry-and-openinference#opentelemetry), project name, and Agent Stream name.

<CodeGroup>
  ```python Python theme={null}
  import os

  # Google ADK imports
  from google.adk.agents.llm_agent import Agent

  # OpenTelemetry imports
  from opentelemetry.sdk import trace as trace_sdk
  from opentelemetry.sdk.trace.export import BatchSpanProcessor
  from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
      OTLPSpanExporter
  )

  # OpenInference imports for Google ADK instrumentation
  from openinference.instrumentation.google_adk import (
      GoogleADKInstrumentor
  )

  # Load the Splunk Agent Observability API configuration from .env file
  from dotenv import load_dotenv
  load_dotenv()

  # Configure the OTel endpoint environment variable
  os.environ["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] = os.environ.get(
      "SPLUNK_AO_API_ENDPOINT", 
      "<your-splunk-ao-api-url>/otel/traces"
  )

  # Create the headers using the Splunk Agent Observability API key, project, and Agent Stream
  headers = {
     # Only required for on-premises, standalone, and custom deployments
     # "Splunk-AO-API-Key": os.getenv("SPLUNK_AO_API_KEY"),
     "project": os.getenv("SPLUNK_AO_PROJECT"),
     "agentstream": os.getenv("SPLUNK_AO_AGENT_STREAM")
  }

  # Store the headers in the appropriate environment variable
  os.environ['OTEL_EXPORTER_OTLP_HEADERS'] = ",".join(
      [f"{k}={v}" for k, v in headers.items()]
  )

  # Create and configure the OTLP span exporter
  exporter = OTLPSpanExporter()
  tracer_provider = trace_sdk.TracerProvider()
  tracer_provider.add_span_processor(BatchSpanProcessor(exporter))

  # Instrument the Google ADK with OpenTelemetry
  GoogleADKInstrumentor().instrument(tracer_provider=tracer_provider)

  # The following code is the example quickstart from the
  # Google ADK documentation
  # Mock tool implementation
  def get_current_time(city: str) -> dict:
      """Returns the current time in a specified city."""
      return {"status": "success", "city": city, "time": "10:30 AM"}

  # Agent definition
  root_agent = Agent(
      model='gemini-2.5-flash',
      name='root_agent',
      description="Tells the current time in a specified city.",
      instruction="""
  You are a helpful assistant that tells the current time in cities.
  Use the 'get_current_time' tool for this purpose.
  """,
      tools=[get_current_time],
  )
  ```
</CodeGroup>
