|
| 1 | +# Agent Observability with Weave by WandB |
| 2 | + |
| 3 | +[Weave by Weights & Biases (WandB)](https://weave-docs.wandb.ai/) provides a powerful platform for logging and visualizing model calls. By integrating Google ADK with Weave, you can track and analyze your agent's performance and behavior using OpenTelemetry (OTEL) traces. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. Sign up for an account at [WandB](https://wandb.ai). |
| 8 | + |
| 9 | +2. Obtain your API key from [WandB Authorize](https://wandb.ai/authorize). |
| 10 | + |
| 11 | +3. Configure your environment with the required API keys: |
| 12 | + |
| 13 | + ```bash |
| 14 | + export WANDB_API_KEY=<your-wandb-api-key> |
| 15 | + export GOOGLE_API_KEY=<your-google-api-key> |
| 16 | + ``` |
| 17 | + |
| 18 | +## Install Dependencies |
| 19 | + |
| 20 | +Ensure you have the necessary packages installed: |
| 21 | + |
| 22 | +```bash |
| 23 | +pip install google-adk opentelemetry-sdk opentelemetry-exporter-otlp-proto-http |
| 24 | +``` |
| 25 | + |
| 26 | +## Sending Traces to Weave |
| 27 | + |
| 28 | +This example demonstrates how to configure OpenTelemetry to send Google ADK traces to Weave. |
| 29 | + |
| 30 | +```python |
| 31 | +# math_agent/agent.py |
| 32 | + |
| 33 | +import base64 |
| 34 | +import os |
| 35 | +from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter |
| 36 | +from opentelemetry.sdk import trace as trace_sdk |
| 37 | +from opentelemetry.sdk.trace.export import SimpleSpanProcessor |
| 38 | +from opentelemetry import trace |
| 39 | + |
| 40 | +from google.adk.agents import LlmAgent |
| 41 | +from google.adk.tools import FunctionTool |
| 42 | + |
| 43 | +from dotenv import load_dotenv |
| 44 | + |
| 45 | +load_dotenv() |
| 46 | + |
| 47 | +# Configure Weave endpoint and authentication |
| 48 | +WANDB_BASE_URL = "https://trace.wandb.ai" |
| 49 | +PROJECT_ID = "your-entity/your-project" # e.g., "teamid/projectid" |
| 50 | +OTEL_EXPORTER_OTLP_ENDPOINT = f"{WANDB_BASE_URL}/otel/v1/traces" |
| 51 | + |
| 52 | +# Set up authentication |
| 53 | +WANDB_API_KEY = os.getenv("WANDB_API_KEY") |
| 54 | +AUTH = base64.b64encode(f"api:{WANDB_API_KEY}".encode()).decode() |
| 55 | + |
| 56 | +OTEL_EXPORTER_OTLP_HEADERS = { |
| 57 | + "Authorization": f"Basic {AUTH}", |
| 58 | + "project_id": PROJECT_ID, |
| 59 | +} |
| 60 | + |
| 61 | +# Create the OTLP span exporter with endpoint and headers |
| 62 | +exporter = OTLPSpanExporter( |
| 63 | + endpoint=OTEL_EXPORTER_OTLP_ENDPOINT, |
| 64 | + headers=OTEL_EXPORTER_OTLP_HEADERS, |
| 65 | +) |
| 66 | + |
| 67 | +# Create a tracer provider and add the exporter |
| 68 | +tracer_provider = trace_sdk.TracerProvider() |
| 69 | +tracer_provider.add_span_processor(SimpleSpanProcessor(exporter)) |
| 70 | + |
| 71 | +# Set the global tracer provider BEFORE importing/using ADK |
| 72 | +trace.set_tracer_provider(tracer_provider) |
| 73 | + |
| 74 | +# Define a simple tool for demonstration |
| 75 | +def calculator(a: float, b: float) -> str: |
| 76 | + """Add two numbers and return the result. |
| 77 | +
|
| 78 | + Args: |
| 79 | + a: First number |
| 80 | + b: Second number |
| 81 | +
|
| 82 | + Returns: |
| 83 | + The sum of a and b |
| 84 | + """ |
| 85 | + return str(a + b) |
| 86 | + |
| 87 | +calculator_tool = FunctionTool(func=calculator) |
| 88 | + |
| 89 | +# Create an LLM agent |
| 90 | +root_agent = LlmAgent( |
| 91 | + name="MathAgent", |
| 92 | + model="gemini-2.0-flash-exp", |
| 93 | + instruction=( |
| 94 | + "You are a helpful assistant that can do math. " |
| 95 | + "When asked a math problem, use the calculator tool to solve it." |
| 96 | + ), |
| 97 | + tools=[calculator_tool], |
| 98 | +) |
| 99 | +``` |
| 100 | + |
| 101 | +## View Traces in Weave dashboard |
| 102 | + |
| 103 | +Once the agent runs, all its traces are logged to the corresponding project on [the Weave dashboard](https://wandb.ai/home). |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +You can view a timeline of calls that your ADK agent made during execution - |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +## Notes |
| 113 | + |
| 114 | +- **Environment Variables**: Ensure your environment variables are correctly set for both WandB and Google API keys. |
| 115 | +- **Project Configuration**: Replace `<your-entity>/<your-project>` with your actual WandB entity and project name. |
| 116 | +- **Entity Name**: You can find your entity name by visiting your [WandB dashboard](https://wandb.ai/home) and checking the **Teams** field in the left sidebar. |
| 117 | +- **Tracer Provider**: It's critical to set the global tracer provider before using any ADK components to ensure proper tracing. |
| 118 | + |
| 119 | +By following these steps, you can effectively integrate Google ADK with Weave, enabling comprehensive logging and visualization of your AI agents' model calls, tool invocations, and reasoning processes. |
| 120 | + |
| 121 | +## Resources |
| 122 | + |
| 123 | +- **[Send OpenTelemetry Traces to Weave](https://weave-docs.wandb.ai/guides/tracking/otel)** - Comprehensive guide on configuring OTEL with Weave, including authentication and advanced configuration options. |
| 124 | + |
| 125 | +- **[Navigate the Trace View](https://weave-docs.wandb.ai/guides/tracking/trace-tree)** - Learn how to effectively analyze and debug your traces in the Weave UI, including understanding trace hierarchies and span details. |
| 126 | + |
| 127 | +- **[Weave Integrations](https://weave-docs.wandb.ai/guides/integrations/)** - Explore other framework integrations and see how Weave can work with your entire AI stack. |
0 commit comments