-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcontinuous_query_alerting.sql
More file actions
101 lines (93 loc) · 3.23 KB
/
Copy pathcontinuous_query_alerting.sql
File metadata and controls
101 lines (93 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
-- Copyright 2026 Google LLC
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-- Example: Real-time error alerting with continuous queries.
--
-- This example shows how to set up end-to-end real-time alerting
-- for agent errors using BigQuery continuous queries and Pub/Sub.
--
-- Architecture:
-- agent_events (APPENDS) → continuous query → Pub/Sub → alerting
--
-- Prerequisites:
-- 1. Enterprise reservation (see deploy/continuous_queries/setup_reservation.md)
-- 2. Pub/Sub topic: projects/PROJECT/topics/agent-errors
-- 3. Pub/Sub subscription connected to your alerting system
-- (e.g. Cloud Functions → Slack, PagerDuty, etc.)
--
-- Replace PROJECT, DATASET, and TOPIC with your values.
-- ------------------------------------------------------------------ --
-- Step 1: Create the Pub/Sub topic (run once via gcloud) --
-- ------------------------------------------------------------------ --
--
-- gcloud pubsub topics create agent-errors --project=PROJECT
-- gcloud pubsub subscriptions create agent-errors-sub \
-- --topic=agent-errors --project=PROJECT
-- ------------------------------------------------------------------ --
-- Step 2: Start the continuous query --
-- ------------------------------------------------------------------ --
--
-- Run this with: bq query --use_legacy_sql=false --continuous=true
EXPORT DATA
OPTIONS (
format = 'CLOUD_PUBSUB',
uri = 'projects/PROJECT/topics/agent-errors'
)
AS
SELECT
TO_JSON_STRING(
STRUCT(
session_id,
event_type,
agent,
error_message,
status,
timestamp,
-- Include trace context for debugging
trace_id,
span_id,
-- Classify severity based on event type
CASE
WHEN event_type = 'TOOL_ERROR' THEN 'high'
WHEN status = 'ERROR' THEN 'critical'
WHEN error_message IS NOT NULL THEN 'medium'
ELSE 'low'
END AS severity
)
) AS message
FROM
APPENDS(TABLE `PROJECT.DATASET.agent_events`)
WHERE
-- Match the SDK's error semantics:
-- TOOL_ERROR events, status='ERROR', or any row with error_message
event_type = 'TOOL_ERROR'
OR status = 'ERROR'
OR error_message IS NOT NULL;
-- ------------------------------------------------------------------ --
-- Step 3: Monitor the continuous query job --
-- ------------------------------------------------------------------ --
--
-- Check running continuous queries:
--
-- SELECT
-- job_id,
-- state,
-- creation_time,
-- TIMESTAMP_DIFF(CURRENT_TIMESTAMP(), creation_time, SECOND) AS running_secs
-- FROM
-- `region-REGION`.INFORMATION_SCHEMA.JOBS
-- WHERE
-- state = 'RUNNING'
-- AND configuration.query.continuous = true
-- ORDER BY
-- creation_time DESC;