我正在关注这个关于如何使用 Python API 将数据发送到 Google Stackdriver的 google-cloud-monitoring 教程。我只是复制粘贴了教程 python 片段
from google.cloud import monitoring_v3
import time
client = monitoring_v3.MetricServiceClient()
project = 'todag-239819'
project_name = client.project_path(project)
series = monitoring_v3.types.TimeSeries()
series.metric.type = 'custom.googleapis.com/my_metric'
series.resource.type = 'gce_instance'
series.resource.labels['instance_id'] = '1234567890123456789'
series.resource.labels['zone'] = 'us-central1-f'
point = series.points.add()
point.value.double_value = 3.14
now = time.time()
point.interval.end_time.seconds = int(now)
point.interval.end_time.nanos = int(
(now - point.interval.end_time.seconds) * 10**9)
client.create_time_series(project_name, [series])
print('Successfully wrote time series.')
我能够在本地成功执行 python 代码片段
$ python stackdriver/example.py
Successfully wrote time series.
在 Stackdriver上,我没有看到关于自定义指标的数据,并且收到以下警告Selecting a metric without a resource may have performance implications.
(我等了 30 分钟以确保它没有显示由于延迟)。
注册资源似乎有问题。我调查了一下,发现这似乎是一个常见问题,基于对Google Stackdriver 的 python 教程的代码内注释。