通过 gcloud 或 python 获取 "logging.googleapis.com/billing/monthly_bytes_ingested" 项目信息

get "logging.googleapis.com/billing/monthly_bytes_ingested" project info by gcloud or python

我想通过带有 gcloud 的 cli 或脚本 (bash/python/javascript) 获取以下信息。

logging.googleapis.com/billing/monthly_bytes_ingested

可以在控制台的监控 > 指标资源管理器 > 基于日志的指标 > 每月摄入的日志字节数下找到(如下图)

虽然任何答案都很好,但我也想知道您通过什么搜索找到了它。我在一天的大部分时间里都在监视和日志中阅读了文档,但我似乎没有找到针对这些数据的明确的编程方法。我找到的最接近的是在 API 此处:https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.timeSeries/list

您无法使用 gcloud 查询指标。

NOTE Cloud Monitoring is the unification of Google's acquisition of Stackdriver and its internal time-series database, Monarch. Stackdriver didn't have a CLI and -- for some reason -- gcloud's never incorporated much monitoring functionality (although this has begun to change).

如您所见,您可以通过云监控查询底层时间序列API。历史上(!)这是使用例如projects.timeseries.list but with the availability of MQL, another approach is to use projects.timeseries.query.

如果您想使用 bashcurl 是与这些 API 和 Google 的 APIs Explorer 交互的绝佳工具所有 Google 的 APIs,为您提供“尝试此方法”功能(右侧),可以扩展该功能以提供等效的 curl、HTTP 和 JavaScript 调用.

Per @codeangler,Google 也为其所有 API 提供多种语言的库。 link @codeangler included Reading Timeseries 包含每种语言的代码示例。看起来(!?)库没有(还?)显示 API 的 timeseries.query 方法,因此您需要使用相当笨拙的 timeseries.list 机制。

NOTE Every Google service has companion so-called API Client Libraries (in several languages). For Google Cloud Platform services (only), Google promotes alternative so-called Cloud Client Libraries (in the same set of languages). See Client Libraries Explained. Google Cloud's documentation generally only references the Cloud Client Libraries but be careful with Google search results to ensure that code examples are using the client libraries that you want to use.

我们可以使用下面的命令在我们这一端获得输出。

GCLOUD

curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) "https://monitoring.googleapis.com/v3/projects/$PROJECT_ID/timeSeries?interval.startTime=YYYY-MM-DDT13:45:46-05:00&interval.endTime=YYYY-MM-DD:05:06-05:00&filter="metric.type="logging.googleapis.com/billing/monthly_bytes_ingested"""

请更改Project_ID间隔Start/End时间。

gcloud命令可以参考这个文档

Google Cloud Monitoring Metrics

Monitoring Filters

飞腾

from google.cloud import monitoring_v3
import time

client = monitoring_v3.MetricServiceClient()
project_name = "projects/$PROJECTID"
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 - 5000), "nanos": nanos},
    }
)

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

有关读取指标数据的更多详细信息和其他代码示例,您可以参考此link

这个答案扩展了原始问题并回答了它。按照上面的说明,我使用 jq 对最高值进行排序。所以我只得到每月日志摄取的总和。

为什么?这似乎是我在调用名为 monthly_bytes_ingested 的指标时想要的只是聚合数字。我确实意识到这个指标在时间序列区域下,因此它与给出滚动结果的时间序列概念一致。但是,当我想要每月总计时,我想要滚动还是只想要最大数额?

referenced docs

query.json

{ "query": "fetch global
| metric 'logging.googleapis.com/billing/monthly_bytes_ingested'
| group_by 3h,
    [value_monthly_bytes_ingested_mean: mean(value.monthly_bytes_ingested)]
| every 3h
| group_by [],
    [value_monthly_bytes_ingested_mean_aggregate:
       aggregate(value_monthly_bytes_ingested_mean)] | within 4w" }

我在控制台算了mql

curl + jq

curl -d @query.json -H "Authorization: Bearer $TOKEN" \                                                                                                 [14:38:23]
--header "Content-Type: application/json" -X POST \
https://monitoring.googleapis.com/v3/projects/${GCP_PROJECT}/timeSeries:query | jq '.timeSeriesData[].pointData | sort_by(.doubleValue) | .[-1]'

示例结果

{
  "values": [
    {
      "doubleValue": 39625036610082.664
    }
  ],
  "timeInterval": {
    "startTime": "2022-03-23T23:38:23.771566Z",
    "endTime": "2022-03-23T23:38:23.771566Z"
  }
}