How to add custom fields (e.g. user_email) to BigQueryAgentAnalyticsPlugin events? #4295
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.
Uh oh!
There was an error while loading. Please reload this page.
-
I am using
BigQueryAgentAnalyticsPluginfor production observability and would like to enrich the BigQuery events with custom per request fields such asuser_email,country, ortenant_id.Current setup
I am already using the plugin successfully with the default schema. Below is a simplified version of my current code.
What I want to achieve
At request start, I store metadata like this in the session state:
I would like these fields to appear as first class columns in BigQuery so that I can:
Questions
What is the recommended way to attach custom fields to events written by
BigQueryAgentAnalyticsPlugin?Is there a supported hook or callback to enrich the event payload before it is flushed to BigQuery?
Should this be done by:
BigQueryAgentAnalyticsPluginbefore_tool_callback,after_agent_callback)event.metadataorevent.attributesstructureIf the BigQuery schema needs to be extended:
ToolContext.state→ BigQuery columns?A minimal example of extending the plugin or enriching the event would be extremely helpful.
Beta Was this translation helpful? Give feedback.
All reactions