Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何设置BigQuery触发器以发送告警? #149

Open
aibangjuxin opened this issue Jun 15, 2024 · 0 comments
Open

如何设置BigQuery触发器以发送告警? #149

aibangjuxin opened this issue Jun 15, 2024 · 0 comments

Comments

@aibangjuxin
Copy link
Owner

BigQuery本身不支持直接设置触发器来响应查询结果的变化。但是,您可以使用Google Cloud Pub/Sub和Cloud Functions来实现类似的功能。以下是使用Pub/Sub和Cloud Functions设置基于BigQuery查询结果的告警的步骤:

  1. 创建Pub/Sub主题:首先,创建一个Pub/Sub主题,用于接收有关BigQuery查询结果变化的告警。
    gcloud pubsub topics create bigquery-alerts
  2. 配置BigQuery数据导出到Pub/Sub:使用BigQuery的数据导出功能,将查询结果导出到Pub/Sub。这需要使用BigQuery的Scheduled Queries功能。
    ○ 转到BigQuery控制台,点击“Scheduled Queries”选项卡,然后点击“Create Scheduled Query”。
    ○ 配置查询,输入您之前提到的查询语句。
    ○ 在“Destination”部分,选择“Pub/Sub Topic”,然后选择您在第1步中创建的Pub/Sub主题。
    ○ 设置查询的调度时间,例如每分钟运行一次。
    ○ 保存配置。
  3. 编写Cloud Function:创建一个Cloud Function,该函数将在Pub/Sub消息到达时执行,并根据消息内容发送告警。
    与之前的步骤类似,创建一个Cloud Function,但这次使用Pub/Sub触发器。在main.py中,编写如下代码:
    import os
from google.cloud import pubsub_v1

def process_alert(event, context):
 message = base64.b64decode(event['data']).decode('utf-8')
 print(f"收到告警消息:{message}")

 # 在这里解析消息内容,并根据需要发送告警
 # 例如,使用邮件、短信或其他通知服务

def listen_for_alerts(event, context):
 project_id = os.environ['GOOGLE_CLOUD_PROJECT']
 subscription_id = os.environ['PUBSUB_SUBSCRIPTION']
 subscriber = pubsub_v1.SubscriberClient()
 subscription_path = subscriber.subscription_path(project_id, subscription_id)

 def callback(message):
 process_alert(message, context)
 message.ack()

 streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
 print(f"监听Pub/Sub主题:projects/{project_id}/subscriptions/{subscription_id}")

 try:
 streaming_pull_future.result()
 except KeyboardInterrupt:
 streaming_pull_future.cancel()
    在requirements.txt中,列出所需的依赖项:
    google-cloud-pubsub==2.14.0
    部署Cloud Function:
    gcloud functions deploy listen_for_alerts \
 --runtime python310 \
 --trigger-resource projects/YOUR_PROJECT_ID/subscriptions/bigquery-alerts \
 --trigger-event google.pubsub.topic.publish
    将YOUR_PROJECT_ID替换为您的Google Cloud项目ID。
    现在,每当Scheduled Query执行并将结果导出到Pub/Sub时,Cloud Function都会接收到消息并根据消息内容发送告警。您可以根据需要调整查询条件和告警通知方式。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant