BigqueryBQAlertsforSlotsandQueryconcurrency
创始人
2024-12-12 10:31:27
0

可以通过使用Bigquery API发出警报来监视Bigquery查询槽数量和查询并发量。

以下是如何设置监视查询槽数量的示例代码:

import google.auth
from google.cloud import bigquery

def check_slots():
    _, proj_id = google.auth.default()
    client = bigquery.Client(project=proj_id)
    
    query_job_config = bigquery.QueryJobConfig(dry_run=True, use_query_cache=False)
    query_job_config.dry_run = True
    query_job_config.use_query_cache = False
    query = (
        "SELECT CAST(IFNULL(sum(total_slot_ms), 0) / 1000 / 60 / 60 as INT64) AS used_hours "
        "FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT "
        "WHERE REGEXP_CONTAINS(project_id, r'^" + proj_id + "$') "
        "AND creation_time BETWEEN "
        "   TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 3 DAY) "
        "   AND CURRENT_TIMESTAMP() "
        "   AND state = 'DONE';"
    )

    query_job = client.query(query, job_config=query_job_config)
    results = query_job.result()

    for row in results:
        used_hours = row['used_hours']

    if used_hours > 100:
        send_alert('Slots usage is high. Used Hours: {}'.format(used_hours))

def send_alert(msg):
    # send an alert
    pass

以下是如何设置监视查询并发量的示例代码:

import google.auth
from google.cloud import monitoring_v3

def check_concurrency():
    _, proj_id = google.auth.default()
    client = monitoring_v3.MetricServiceClient()

    end_time = time.time()
    start_time = end_time - 3 * 60 * 60 # the past 3 hours
    project_resource = client.project_path(proj_id)

    duration = monitoring_v3.types.TimeInterval()
    duration.end_time.FromSeconds(int(end_time))
    duration.start_time.FromSeconds(int(start_time))

    aggregation = monitoring_v3.types.Aggregation()
    aggregation.alignment_period.seconds = 600 # 10 minutes
    aggregation.per_series_aligner = (monitoring_v3.enums.Aggregation.Aligner.ALIGN_MEAN)

    metric_type = 'bigquery.googleapis.com/query/count'
    filter = (
        'metric.type="{}" AND '
        'resource.type="global" AND '
        'resource.labels.project_id="{}"').format(
        metric_type, proj_id)

    response = client.list_time_series(
        project_resource,
        filter,
        aggregation,
        interval,
    )

    num_queries = 0
    for ts in response:
        for point in ts.points:

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...