开始使用 Google Cloud Monitoring





5.00/5 (1投票)
本文档演示了一种客户端方法,使用 Google JavaScript 客户端库检索监控数据,以帮助您开始使用 Google Cloud Monitoring API 的基础知识。
引言
本文档演示了一种客户端方法,使用 Google JavaScript 客户端库检索监控数据,以帮助您开始使用 Google Cloud Monitoring API 的基础知识。 示例中使用的 OAuth 流程是一个客户端流程,用户授予访问权限,以便从他们的项目中检索监控数据。 在您的应用程序中,您可能希望使用来自服务器端或后端应用程序的服务帐户来聚合数据,而不是使用 JavaScript 执行客户端检索或授权单个用户。
启用 API
在开始监控项目之前,您必须在 Google Developers Console 中启用 Google Cloud Monitoring API 并创建一个客户端 ID,以允许您的示例应用程序连接到 Google API。 本文档假设您正在 https://:8080
上测试示例。
- 如果您还没有 Google 帐户,请注册一个。
- 在开发者控制台中启用 Google Cloud Monitoring API。您可以选择现有的 Compute Engine 或 App Engine 项目,也可以创建一个新项目。
- 在 APIs & Auth 部分的凭据页面上,单击创建新客户端 ID。 选择Web 应用程序。 在授权的 JavaScript 来源中,输入您将用于提供此示例的主机,例如
https://:8080
。 单击创建客户端 ID。 - 记下项目中需要输入的以下信息,以便在后续步骤中使用
- 客户端 ID (
xxxxxx.apps.googleusercontent.com
)。 - 您希望监控的项目 ID。 您可以在Google Developers Console 中项目概览页面的顶部找到该 ID。 您也可以要求用户提供他们想要在您的应用程序中监控的项目 ID。
- 客户端 ID (
现在您的项目已启用 API,您将配置您的应用程序以代表用户访问 REST API。
授权请求
要访问项目的监控数据,您必须使用 OAuth 2.0 授权您的应用程序。 这些示例演示了一个简单的 OAuth 2.0 流程,使用 JavaScript 代表用户请求授权。 用户必须在 Google Developers Console 团队区域中拥有该项目的拥有者、编辑者或查看者访问权限。 OAuth 2.0 范围 https://www.googleapis.com/auth/monitoring.readonly
授予您的应用程序访问 Cloud Monitoring API 数据的权限。
在实际应用程序中,您可能选择使用服务帐户和后端代码来请求监控数据。
请求用户授权并获取访问令牌。
以下示例演示了一个简单的 JavaScript 流程,使用 Google JavaScript Client API 请求用户授权并获取访问令牌。
<button id="authButton">Authorize</button> <script type="text/javascript"> var authParams = { 'response_type' : 'token', // Retrieves an access token only 'client_id' : 'xxxxxxx.apps.googleusercontent.com', // Client ID from Developers Console 'immediate' : false, // For the demo, force the auth window every time 'scope' : ['https://www.googleapis.com/auth/monitoring.readonly'] // Array of scopes }; function myCallback(authResult){ if (authResult && authResult['access_token']) { // User granted authorization // Set the token for later API calls gapi.auth.setToken(authResult); ... // Next step, load API and query API } else { // Authorization failed or user declined } } // Attach a click even to an authorization button. Use clicks to trigger // the flow so that the pop-up window succeeds. document.getElementById('authButton').onclick = function(){ gapi.auth.authorize(authParams, myCallback) }; </script>
现在您的应用程序拥有访问令牌,您就可以查询 Cloud Monitoring API 来检索时间序列数据了。
获取时间序列数据点
现在您的应用程序拥有访问令牌,它可以检索项目的监控数据。 您需要决定要监控哪种类型的指标。 此快速示例直接查找时间序列,而不是使用 timeseriesDescriptor.list
方法发现可用资源。
<script type="text/javascript"> gapi.client.load('cloudmonitoring', 'v2beta1', function() { getTimeSeriesDataPoints(); } function getTimeSeriesDataPoints() { // Configure a request to the Timeseries.list API method and pass the // required and optional parameters var request = gapi.client.cloudmonitoring.timeseries.list({ 'project' : 'YOUR_PROJECT_NAME' ,'metric' : 'compute.googleapis.com/instance/uptime' //,'timespan' : '2w' // Add and adjust if you get timeouts, default is 4hr }); // Execute the request and provide a callback function request.execute(function(resp) { // Insert the response into a div for viewing. document.getElementById('response-payload').innerText = JSON.stringify(resp); }); } </script> <div id="response-payload"></div>
后续步骤
本文档中的简单示例演示了如何快速开始使用 Cloud Monitoring API。 您的应用程序可能需要执行更复杂的查询、存储结果,并可能在您自己的后端解析数据。 获取有关以下主题的更多信息