How to add custom fields (e.g. user_email) to BigQueryAgentAnalyticsPlugin events? #4295
-
|
I am using Current setupI am already using the plugin successfully with the default schema. Below is a simplified version of my current code. # my_bq_agent/agent.py
import os
import google.auth
from google.adk.apps import App
from google.adk.plugins.bigquery_agent_analytics_plugin import (
BigQueryAgentAnalyticsPlugin,
BigQueryLoggerConfig,
)
from google.adk.agents import Agent
from google.adk.models.google_llm import Gemini
# --- OpenTelemetry Initialization (Optional) ---
try:
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
trace.set_tracer_provider(TracerProvider())
except ImportError:
pass
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "your-gcp-project-id")
DATASET_ID = os.environ.get("BIG_QUERY_DATASET_ID", "your-big-query-dataset-id")
LOCATION = os.environ.get("GOOGLE_CLOUD_LOCATION", "US")
bq_config = BigQueryLoggerConfig(
enabled=True,
log_multi_modal_content=False,
max_content_length=500 * 1024,
batch_size=1,
shutdown_timeout=10.0,
)
bq_logging_plugin = BigQueryAgentAnalyticsPlugin(
project_id=PROJECT_ID,
dataset_id=DATASET_ID,
table_id="agent_events",
config=bq_config,
location=LOCATION,
)
llm = Gemini(model="gemini-2.5-flash")
root_agent = Agent(
model=llm,
name="dad_joke_agent",
instruction="You are a helpful assistant which only replies dad jokes.",
)
app = App(
name="app",
root_agent=root_agent,
plugins=[bq_logging_plugin],
)What I want to achieveAt request start, I store metadata like this in the session state: state["user_email"] = "user@example.com"
state["country"] = "IN"I would like these fields to appear as first class columns in BigQuery so that I can:
Questions
A minimal example of extending the plugin or enriching the event would be extremely helpful. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
How to Enrich BigQuery Agent Analytics EventsThe recommended way to attach custom fields (like This approach is flexible, avoids schema migration headaches, and is fully queryable in BigQuery. 1. Recommended Approach: Subclassing the PluginYou can subclass Implementation Examplefrom typing import Any
from google.adk.plugins.bigquery_agent_analytics_plugin import BigQueryAgentAnalyticsPlugin
from google.adk.agents.callback_context import CallbackContext
class EnrichedBigQueryPlugin(BigQueryAgentAnalyticsPlugin):
"""A custom BQ plugin that enriches events with session state metadata."""
async def _log_event(
self,
event_type: str,
callback_context: CallbackContext,
**kwargs: Any
) -> None:
"""Injects custom context from session state into event attributes."""
# Safely access the session state
if hasattr(callback_context, "session") and callback_context.session:
state = callback_context.session.state
# extract specific fields you want to promote to top-level attributes
# These will appear in the 'attributes' JSON column in BigQuery
if "user_email" in state:
kwargs["user_email"] = state["user_email"]
if "tenant_id" in state:
kwargs["tenant_id"] = state["tenant_id"]
if "country" in state:
kwargs["country"] = state["country"]
# Call the parent method to handle the actual logging
await super()._log_event(event_type, callback_context, **kwargs)UsageReplace the standard plugin with your custom class in your application setup: # Initialize your custom plugin
bq_logging_plugin = EnrichedBigQueryPlugin(
project_id=PROJECT_ID,
dataset_id=DATASET_ID,
table_id="agent_events", # You can use the same table
config=bq_config,
location=LOCATION,
)
# ... setup app ...
app = App(
name="app",
root_agent=root_agent,
plugins=[bq_logging_plugin],
)2. Querying the DataData injected into SELECT
timestamp,
event_type,
JSON_VALUE(attributes.user_email) AS user_email,
JSON_VALUE(attributes.tenant_id) AS tenant_id,
JSON_VALUE(attributes.country) AS country,
-- You can still filter on them efficiently
FROM
`your-project.your_dataset.agent_events`
WHERE
JSON_VALUE(attributes.tenant_id) = 'tenant-123'Answers to your specific questions
|
Beta Was this translation helpful? Give feedback.
How to Enrich BigQuery Agent Analytics Events
The recommended way to attach custom fields (like
user_email,tenant_id) to your BigQuery events is to leverage theattributesJSON column rather than extending the BigQuery table schema.This approach is flexible, avoids schema migration headaches, and is fully queryable in BigQuery.
1. Recommended Approach: Subclassing the Plugin
You can subclass
BigQueryAgentAnalyticsPluginand override the internal_log_eventmethod. This method acts as a central hook for all events (tool calls, user messages, model responses, etc.), allowing you to inject custom data from the session state into the event attributes.Implementation Example