diff --git a/monitoring/README.md b/monitoring/README.md new file mode 100644 index 0000000000..109b3c2adc --- /dev/null +++ b/monitoring/README.md @@ -0,0 +1,27 @@ +# Cloud Monitoring Sample + +`list_resources.js` is a command-line program to demonstrate connecting to the Google +Monitoring API to retrieve API data. + +`create_custom_metric.js` demonstrates how to create a custom metric, write a timeseries value to it, +and read it back. + +## Prerequisites to run locally: + +Go to the [Google Developers Console](https://console.developer.google.com). + + * Go to API Manager -> Credentials and click New Credential in the Google Developers Console. + [or click here ]{https://console.developers.google.com/project/_/apiui/credential/serviceaccount) + Select a JSON-format key, which will be downloaded to your computer. The key file must be located on the computer(s) from which you will access the Monitoring API. Access to the file should be restricted so that only trusted people can read the key. + + ``` + export GOOGLE_APPLICATION_CREDENTIALS=~/Downloads/-0123456789abcdef.json + ``` + +# Run locally + + npm install + node list_resources.js + node create_custom_metric.js + + diff --git a/monitoring/create_custom_metric.js b/monitoring/create_custom_metric.js new file mode 100644 index 0000000000..596f16e7a3 --- /dev/null +++ b/monitoring/create_custom_metric.js @@ -0,0 +1,235 @@ +// Copyright 2015, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @fileoverview Simple command-line program to demonstrate creating a custom + * metric with the Google Cloud Monitoring API, writing a random value to it, + * and reading it back. + */ +'use strict'; + +/* jshint camelcase: false */ + +var google = require('googleapis'); +var async = require('async'); + +var args = process.argv.slice(2); +if (args.length !== 1) { + console.log('Usage: node auth_and_list_env.js '); + process.exit(); +} + +var monitoringScopes = [ + 'https://www.googleapis.com/auth/cloud-platform', + 'https://www.googleapis.com/auth/monitoring', + 'https://www.googleapis.com/auth/monitoring.read', + 'https://www.googleapis.com/auth/monitoring.write' +]; + + +/** The project resource created from the project ID */ +var PROJECT_RESOURCE = 'projects/' + args[0]; + +/** This domain should be used for all custom metrics. */ +var CUSTOM_METRIC_DOMAIN = 'custom.googleapis.com'; + +/** This is the type of the custom metric */ +var CUSTOM_METRIC_TYPE = CUSTOM_METRIC_DOMAIN + '/custom_measurement'; + +/** This is the name of the custom metric */ +var CUSTOM_METRIC_NAME = PROJECT_RESOURCE + '/metricDescriptors/' + + CUSTOM_METRIC_TYPE; + +/** + * Returns the current timestamp in RFC33339 with milliseconds format. + */ +function getNow() { + var d = new Date(); + return JSON.parse(JSON.stringify(d).replace('Z', '000Z')); +} + +/** + * Returns an hour ago in RFC33339 with milliseconds format. This is used + * to start the window to view the metric written in. + */ +function getStartTime() { + var d = new Date(); + d.setHours(d.getHours() - 1); + return JSON.parse(JSON.stringify(d).replace('Z', '000Z')); +} + +/** + * Gets random integer between low and high (exclusive). Used to fill in + * a random value for the measurement. + */ +function getRandomInt(low, high) { + return Math.floor(Math.random() * (high - low) + low); +} + + +/** + * Creates a custo metric. For demonstration purposes, this is a hypothetical + * measurement (measured in the 'items' unit, with random values written to + * it. + * @param authClient The authorized Monitoring client. + * @param projectId + * @param callback + */ +function createCustomMetric(authClient, projectResource, callback) { + var monitoring = google.monitoring('v3'); + + monitoring.projects.metricDescriptors.create({ + auth: authClient, + name: projectResource, + resource: { + name: CUSTOM_METRIC_NAME, + type: CUSTOM_METRIC_TYPE, + labels: [ + { + key: 'environment', + valueType: 'STRING', + description: 'An abritrary measurement' + } + ], + metricKind: 'GAUGE', + valueType: 'INT64', + unit: 'items', + description: 'An arbitrary measurement.', + displayName: 'Custom Metric' + } + }, function (error, customMetric) { + if (error) { + console.error('Error Creating Custom Metric', error); + return; + } + console.log('createCustomMetric: '); + console.log(customMetric); + callback(); + }); +} + +/** + * Writes a time series value for the custom metric just created. It uses a + * GAUGE measurement which indicates the value at this point in time. For + * demonstration purposes, this is a random value. For GAUGE measurements, + * the start time and end time of the value must be the same. The + * resource for this value is a hypothetical GCE instance. + * @param authClient The authorized Google Cloud Monitoring API client + * @param projectResource The project resource created from the project ID + * @param callback + */ +function writeTimeSeriesForCustomMetric(client, projectResource, callback) { + var monitoring = google.monitoring('v3'); + var now = getNow(); + monitoring.projects.timeSeries.create({ + auth: client, + name: projectResource, + resource: { + timeSeries: [{ + metric: { + type: CUSTOM_METRIC_TYPE, + labels: { + environment: 'STAGING' + } + }, + resource: { + type: 'gce_instance', + labels: { + instance_id: 'test_instance', + zone: 'us-central1-f' + } + }, + metricKind: 'GAUGE', + valueType: 'INT64', + points: { + interval: { + startTime: now, + endTime: now + }, + value: { + int64Value: getRandomInt(1, 20) + } + } + }] + } + }, function (error, timeSeries) { + if (error) { + console.error('Error writing time series', error); + return; + } + console.log('timeSeries: '); + console.log(timeSeries); + callback(); + }); +} + +/** + * Lists the time series written for the custom metric. The window + * to read the timeseries starts an hour ago and extends unti the current + * time, so should include the metric value written by + * the earlier calls. + * @param authClient The authorized Google Cloud Monitoring API client + * @param projectResource The project resource created from the project ID + * @param callback + */ +function listTimeSeries(client, projectResource, callback) { + var monitoring = google.monitoring('v3'); + var startTime = getStartTime(); + var endTime = getNow(); + monitoring.projects.timeSeries.list({ + auth: client, + name: projectResource, + filter: 'metric.type="' + CUSTOM_METRIC_TYPE + '"', + pageSize: 3, + 'interval.startTime': startTime, + 'interval.endTime': endTime + }, function (error, timeSeries) { + if (error) { + console.error('Error readTimeseries', error); + return; + } + console.log('readTimeseries '); + console.log(JSON.stringify(timeSeries)); + callback(); + }); +} + +google.auth.getApplicationDefault(function (error, authClient) { + if (error) { + console.error(error); + process.exit(1); + } + + // Depending on the environment that provides the default credentials + // (e.g. Compute Engine, App Engine), the credentials retrieved may + // require you to specify the scopes you need explicitly. + // Check for this case, and inject the Cloud Storage scope if required. + if (authClient.createScopedRequired && + authClient.createScopedRequired()) { + authClient = authClient.createScoped(monitoringScopes); + } + + // Create the service object. + async.series([ + function (callback) { + createCustomMetric(authClient, PROJECT_RESOURCE, callback); + }, function (callback) { + writeTimeSeriesForCustomMetric(authClient, + PROJECT_RESOURCE, callback); + }, function (callback) { + // wait 2 seconds for the write to be received + setTimeout(function () { + listTimeSeries(authClient, PROJECT_RESOURCE, callback); + }, 2000); + }]); +}); diff --git a/monitoring/list_resources.js b/monitoring/list_resources.js new file mode 100644 index 0000000000..8d76f6233e --- /dev/null +++ b/monitoring/list_resources.js @@ -0,0 +1,166 @@ +// Copyright 2015, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @fileoverview Simple command-line program to demonstrate connecting to the + * Google Monitoring API to retrieve API data. + */ +'use strict'; + +var google = require('googleapis'); +var async = require('async'); + +var args = process.argv.slice(2); +if (args.length !== 1) { + console.log('Usage: node list_resources.js '); + process.exit(); +} + +var monitoringScopes = [ + 'https://www.googleapis.com/auth/cloud-platform', + 'https://www.googleapis.com/auth/monitoring', + 'https://www.googleapis.com/auth/monitoring.read', + 'https://www.googleapis.com/auth/monitoring.write' +]; + +var PROJECT_ID = 'projects/' + args[0]; +var METRIC = 'compute.googleapis.com/instance/cpu/usage_time'; + + +/** + * Returns an hour ago minus 5 minutes in RFC33339 format. + */ +function getStartTime() { + var d = new Date(); + d.setHours(d.getHours() - 1); + d.setMinutes(d.getMinutes() - 5); + return JSON.parse(JSON.stringify(d).replace('Z', '000Z')); +} + +/** + * Returns an hour ago in RFC33339 format. + */ +function getEndTime() { + var d = new Date(); + d.setHours(d.getHours() - 1); + return JSON.parse(JSON.stringify(d).replace('Z', '000Z')); +} + +/** + * This Lists all the resources available to be monitored in the API. + * + * @param {googleAuthClient} authClient - The authenticated Google api client + * @param {String} projectId - the project id + * @param {requestCallback} callback - a function to be called when the server + * responds with the list of monitored resource descriptors + */ +function listMonitoredResourceDescriptors(authClient, projectId, callback) { + var monitoring = google.monitoring('v3'); + monitoring.projects.monitoredResourceDescriptors.list({ + auth: authClient, + name: projectId, + }, function (error, monitoredResources) { + if (error) { + console.error( + 'Error Retrieving Monitored Resource Descriptors', error); + return; + } + console.log('listMonitoredResourceDescriptors: '); + console.log(monitoredResources); + callback(); + }); +} + +/** + * This Lists the metric descriptors that start with our METRIC name, in this + * case the CPU usage time. + * @param {googleAuthClient} authClient - The authenticated Google api client + * @param {String} projectId - the project id + * @param {requestCallback} callback - a function to be called when the server + * responds with the list of monitored resource descriptors + */ +function listMetricDescriptors(authClient, projectId, callback) { + var monitoring = google.monitoring('v3'); + monitoring.projects.metricDescriptors.list({ + auth: authClient, + filter: 'metric.name="' + METRIC + '"', + name: projectId + }, function (error, metricDescriptors) { + if (error) { + console.error('Error Retrieving Metric Descriptors', error); + return; + } + console.log('listMetricDescriptors'); + console.log(metricDescriptors); + callback(); + }); +} + +/** + * This Lists all the timeseries created between START_TIME and END_TIME + * for our METRIC. + * @param {googleAuthClient} authClient - The authenticated Google api client + * @param {String} projectId - the project id + * @param {requestCallback} callback - a function to be called when the server + * responds with the list of monitored resource descriptors + */ +function listTimeseries(authClient, projectId, callback) { + var monitoring = google.monitoring('v3'); + var startTime = getStartTime(); + var endTime = getEndTime(); + + monitoring.projects.timeSeries.list({ + auth: authClient, + filter: 'metric.type="' + METRIC + '"', + pageSize: 3, + 'interval.startTime': startTime, + 'interval.endTime': endTime, + name: projectId + }, function (error, timeSeries) { + if (error) { + console.error('Error Retrieving Timeseries', error); + return; + } + console.log('listTimeseries'); + console.log(timeSeries); + callback(); + }); +} + + +google.auth.getApplicationDefault(function (error, authClient) { + if (error) { + console.error(error); + process.exit(); + } + + // Depending on the environment that provides the default credentials + // (e.g. Compute Engine, App Engine), the credentials retrieved may require + // you to specify the scopes you need explicitly. + // Check for this case, and inject the Cloud Storage scope if required. + if (authClient.createScopedRequired && + authClient.createScopedRequired()) { + authClient = authClient.createScoped(monitoringScopes); + } + + // Create the service object. + async.series([ + function (callback) { + listMonitoredResourceDescriptors(authClient, PROJECT_ID, callback); + }, function (callback) { + listMetricDescriptors(authClient, PROJECT_ID, callback); + }, function (callback) { + listTimeseries(authClient, PROJECT_ID, callback); + }] + ); +}); diff --git a/monitoring/package.json b/monitoring/package.json new file mode 100644 index 0000000000..55ac729f17 --- /dev/null +++ b/monitoring/package.json @@ -0,0 +1,14 @@ +{ + "name": "nodejs-docs-samples-monitoring", + "description": "Samples For Cloud Monitoring v3 API", + "version": "0.0.1", + "license": "Apache Version 2.0", + "author": "Google Inc.", + "engines": { + "node": ">=0.10.x" + }, + "dependencies": { + "async":"^1.5.0", + "googleapis": "^3.1.0" + } +}