Google 监控 API :获取值

Google Monitoring API : Get Values

我正在尝试使用 Google Monitoring API 检索有关我的云使用情况的指标。我正在为 Python 使用 Google 客户端库。

API 宣称能够访问 900 多个 Stackdriver 监控指标。我有兴趣访问一些 Google App Engine 指标,例如实例计数、总内存等。Google API 指标页面列出了我应该能够访问的所有指标访问。

我已按照 Google 客户端库页面上的指南进行操作,但我进行 API 调用的脚本并未打印指标,它只是打印指标说明。

如何使用 Google 监控 API 访问指标,而不是描述?

我的代码:

from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
...
response = monitor.projects().metricDescriptors().get(name='projects/{my-project-name}/metricDescriptors/appengine.googleapis.com/system/instance_count').execute()

print(json.dumps(response, sort_keys=True, indent=4))

My Output

我希望看到实际的实例数。我怎样才能做到这一点?

对于任何读到这篇文章的人,我发现了问题所在。我假设这些值将来自 api 中的 'metric descriptors' class,但这是一个糟糕的假设。

对于值,您需要使用 'timeSeries' 调用。对于此调用,您需要指定要监控的项目、开始时间、结束时间和过滤器(您想要的指标,例如cpu、内存等)

因此,要检索 App Engine 项目内存,上面的代码变为

request = monitor.projects().timeSeries().list(name='projects/my-appengine-project',
                                        interval_startTime='2016-05-02T15:01:23.045123456Z',
                                        interval_endTime='2016-06-02T15:01:23.045123456Z', 
                                        filter='metric.type="appengine.googleapis.com/system/memory/usage"')

response = request.execute()

这个例子有开始时间和结束时间来涵盖一个月的数据。