Skip to main content

OpenInference Google ADK Instrumentation

Project description

OpenInference Google ADK Instrumentation

pypi

Python auto-instrumentation library for Google ADK.

The traces emitted by this instrumentation are fully OpenTelemetry compatible and can be sent to an OpenTelemetry collector for viewing, such as arize-phoenix

Installation

pip install openinference-instrumentation-google-adk

Quickstart

In this example we will instrument a small program that uses Gemini and observe the traces via arize-phoenix.

Install packages.

pip install openinference-instrumentation-google-adk arize-phoenix opentelemetry-sdk opentelemetry-exporter-otlp

Start the phoenix server so that it is ready to collect traces. The Phoenix server runs entirely on your machine and does not send data over the internet.

phoenix serve

In a python file, set up the GoogleADKInstrumentor and configure the tracer to send traces to Phoenix.

import asyncio

from google.adk.agents import Agent
from google.adk.runners import InMemoryRunner
from google.genai import types

from openinference.instrumentation.google_adk import GoogleADKInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
# Optionally, you can also print the spans to the console.
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))

GoogleADKInstrumentor().instrument(tracer_provider=tracer_provider)

def get_weather(city: str) -> dict:
    """Retrieves the current weather report for a specified city.

    Args:
        city (str): The name of the city for which to retrieve the weather report.

    Returns:
        dict: status and result or error msg.
    """
    if city.lower() == "new york":
        return {
            "status": "success",
            "report": (
                "The weather in New York is sunny with a temperature of 25 degrees"
                " Celsius (77 degrees Fahrenheit)."
            ),
        }
    else:
        return {
            "status": "error",
            "error_message": f"Weather information for '{city}' is not available.",
        }

agent = Agent(
   name="test_agent",
   model="gemini-2.0-flash-exp",
   description="Agent to answer questions using tools.",
   instruction="You must use the available tools to find an answer.",
   tools=[get_weather]
)

async def main():
    app_name = "test_instrumentation"
    user_id = "test_user"
    session_id = "test_session"
    runner = InMemoryRunner(agent=agent, app_name=app_name)
    session_service = runner.session_service
    await session_service.create_session(
        app_name=app_name,
        user_id=user_id,
        session_id=session_id
    )
    async for event in runner.run_async(
        user_id=user_id,
        session_id=session_id,
        new_message=types.Content(role="user", parts=[
            types.Part(text="What is the weather in New York?")]
        )
    ):
        if event.is_final_response():
            print(event.content.parts[0].text.strip())

if __name__ == "__main__":
    asyncio.run(main())

Since we are using Gemini, we must set the GOOGLE_API_KEY environment variable to authenticate with the Gemini API.

export GOOGLE_API_KEY=your-api-key

Now simply run the python file and observe the traces in Phoenix.

python your_file.py

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 openinference_instrumentation_google_adk-0.1.9.tar.gz.

File metadata

File hashes

Hashes for openinference_instrumentation_google_adk-0.1.9.tar.gz
Algorithm Hash digest
SHA256 9338c8bd3f0873f3ec0f77f8a696dd198094ea7942b7217ac91674ba030b25b5
MD5 a4f36b63353ab268c3104cebc6cf9be1
BLAKE2b-256 5c669ded61520b67d56c1f3202a7b28c4c72b831b92a5ece73705f49f6eb73ee

See more details on using hashes here.

File details

Details for the file openinference_instrumentation_google_adk-0.1.9-py3-none-any.whl.

File metadata

File hashes

Hashes for openinference_instrumentation_google_adk-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 341032df76f93c603cc0574912015e94341a3ccc82dc5598802b7529bcac0420
MD5 c5311c4e5df96195b5d0b917cd9d4478
BLAKE2b-256 1daa4284d1f5ecf1431e5907198f41b70bb257f19d76e51e39852e141d1f8750

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