如何使用python在GCP中获取负载平衡器的https/后端请求计数度量?

2024-09-29 23:16:51 发布

您现在位置:Python中文网/ 问答频道 /正文

1条回答
网友
1楼 · 发布于 2024-09-29 23:16:51

是的,这是可能的。我遵循这个reading metric document并使用后端请求计数对其进行过滤

我所做的是获得过滤器,然后Google Cloud Metrics backend_request_count构建过滤器

The "metric type" strings in this table must be prefixed with loadbalancing.googleapis.com/. That prefix has been omitted from the entries in the table.

按照上述说明,过滤器如下所示:

metric.type = "loadbalancing.googleapis.com/https/backend_request_count"

在代码中应用此过滤器,代码将如下所示:

from google.cloud import monitoring_v3
import time

client = monitoring_v3.MetricServiceClient()
## Don't forget to replace this dummy project_id with your actual project_id ##
project_name = f"projects/your-project-id-here" 
interval = monitoring_v3.TimeInterval()

now = time.time()
seconds = int(now)
nanos = int((now - seconds) * 10 ** 9)
interval = monitoring_v3.TimeInterval(
    {
        "end_time": {"seconds": seconds, "nanos": nanos},
        "start_time": {"seconds": (seconds - 1200), "nanos": nanos},
    }
)

results = client.list_time_series(
    request={
        "name": project_name,
        "filter": 'metric.type = "loadbalancing.googleapis.com/https/backend_request_count"', 
        "interval": interval,
        "view": monitoring_v3.ListTimeSeriesRequest.TimeSeriesView.FULL,
    }
)
for result in results:
    print(result)

这将产生一个ListTimeSeriesResponse,下面是上面代码的片段响应

enter image description here

相关问题 更多 >

    热门问题