|
| 1 | +// Copyright 2015, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +/** |
| 15 | + * @fileoverview Simple command-line program to demonstrate creating a custom |
| 16 | + * metric with the Google Cloud Monitoring API, writing a random value to it, |
| 17 | + * and reading it back. |
| 18 | + */ |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +var google = require('googleapis'); |
| 22 | +var async = require('async'); |
| 23 | + |
| 24 | +var args = process.argv.slice(2); |
| 25 | +if (args.length !== 1) { |
| 26 | + console.log('Usage: node auth_and_list_env.js <project_id>'); |
| 27 | + process.exit(); |
| 28 | +} |
| 29 | + |
| 30 | +var monitoringScopes = [ |
| 31 | + 'https://www.googleapis.com/auth/cloud-platform', |
| 32 | + 'https://www.googleapis.com/auth/monitoring', |
| 33 | + 'https://www.googleapis.com/auth/monitoring.read', |
| 34 | + 'https://www.googleapis.com/auth/monitoring.write' |
| 35 | +]; |
| 36 | + |
| 37 | + |
| 38 | +/** The project resource created from the project ID */ |
| 39 | +var PROJECT_RESOURCE = 'projects/' + args[0]; |
| 40 | + |
| 41 | +/** This domain should be used for all custom metrics. */ |
| 42 | +var CUSTOM_METRIC_DOMAIN = 'custom.googleapis.com'; |
| 43 | + |
| 44 | +/** This is the type of the custom metric */ |
| 45 | +var CUSTOM_METRIC_TYPE = CUSTOM_METRIC_DOMAIN + '/custom_measurement'; |
| 46 | + |
| 47 | +/** This is the name of the custom metric */ |
| 48 | +var CUSTOM_METRIC_NAME = PROJECT_RESOURCE + '/metricDescriptors/' + CUSTOM_METRIC_TYPE; |
| 49 | + |
| 50 | +/** |
| 51 | + * Returns the current timestamp in RFC33339 with milliseconds format. |
| 52 | + */ |
| 53 | +function getNow() { |
| 54 | + var d = new Date(); |
| 55 | + return JSON.parse(JSON.stringify(d).replace('Z', '000Z')); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Returns an hour ago in RFC33339 with milliseconds format. This is used |
| 60 | + * to start the window to view the metric written in. |
| 61 | + */ |
| 62 | +function getStartTime() { |
| 63 | + var d = new Date(); |
| 64 | + d.setHours(d.getHours() - 1); |
| 65 | + return JSON.parse(JSON.stringify(d).replace('Z', '000Z')); |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Gets random integer between low and high (exclusive). Used to fill in a random value |
| 70 | + * for the measurement. |
| 71 | + */ |
| 72 | +function getRandomInt(low, high) { |
| 73 | + return Math.floor(Math.random() * (high - low) + low); |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +/** |
| 78 | + * Creates a custo metric. For demonstration purposes, this is a hypothetical measurement (measured in the |
| 79 | + * 'items' unit, with random values written to it. |
| 80 | + * @param authClient The authorized Monitoring client. |
| 81 | + * @param projectId |
| 82 | + * @param callback |
| 83 | + */ |
| 84 | +function createCustomMetric(authClient, projectResource, callback) { |
| 85 | + var monitoring = google.monitoring('v3'); |
| 86 | + |
| 87 | + monitoring.projects.metricDescriptors.create({ |
| 88 | + auth: authClient, |
| 89 | + name: projectResource, |
| 90 | + resource: { |
| 91 | + name: CUSTOM_METRIC_NAME, |
| 92 | + type: CUSTOM_METRIC_TYPE, |
| 93 | + labels: [ |
| 94 | + { |
| 95 | + key: 'environment', |
| 96 | + valueType: 'STRING', |
| 97 | + description: 'An abritrary measurement' |
| 98 | + } |
| 99 | + ], |
| 100 | + metricKind: 'GAUGE', |
| 101 | + valueType: 'INT64', |
| 102 | + unit: 'items', |
| 103 | + description: 'An arbitrary measurement.', |
| 104 | + displayName: 'Custom Metric' |
| 105 | + } |
| 106 | + }, function (error, customMetric) { |
| 107 | + if (error) { |
| 108 | + console.log('Error Creating Custom Metric', error); |
| 109 | + return; |
| 110 | + } |
| 111 | + console.log('createCustomMetric: '); |
| 112 | + console.log(customMetric); |
| 113 | + callback(); |
| 114 | + }); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Writes a time series value for the custom metric just created. It uses a GAUGE measurement |
| 119 | + * which indicates the value at this point in time. For demonstration purposes, this is a random |
| 120 | + * value. For GAUGE measurements, the start time and end time of the value must be the same. The |
| 121 | + * resource for this value is a hypothetical GCE instance. |
| 122 | + * @param authClient The authorized Google Cloud Monitoring API client |
| 123 | + * @param projectResource The project resource created from the project ID |
| 124 | + * @param callback |
| 125 | + */ |
| 126 | +function writeTimeSeriesForCustomMetric(client, projectResource, callback) { |
| 127 | + var monitoring = google.monitoring('v3'); |
| 128 | + var now = getNow(); |
| 129 | + monitoring.projects.timeSeries.create({ |
| 130 | + auth: client, |
| 131 | + name: projectResource, |
| 132 | + resource: { |
| 133 | + timeSeries: [{ |
| 134 | + metric: { |
| 135 | + type: CUSTOM_METRIC_TYPE, |
| 136 | + labels: { |
| 137 | + environment: 'STAGING' |
| 138 | + } |
| 139 | + }, |
| 140 | + resource: { |
| 141 | + type: 'gce_instance', |
| 142 | + labels: { |
| 143 | + instance_id: 'test_instance', |
| 144 | + zone: 'us-central1-f' |
| 145 | + } |
| 146 | + }, |
| 147 | + metricKind: 'GAUGE', |
| 148 | + valueType: 'INT64', |
| 149 | + points: { |
| 150 | + interval: { |
| 151 | + startTime: now, |
| 152 | + endTime: now |
| 153 | + }, |
| 154 | + value: { |
| 155 | + int64Value: getRandomInt(1, 20) |
| 156 | + } |
| 157 | + } |
| 158 | + }] |
| 159 | + } |
| 160 | + }, function (error, timeSeries) { |
| 161 | + if (error) { |
| 162 | + console.log('Error writing time series', error); |
| 163 | + return; |
| 164 | + } |
| 165 | + console.log('timeSeries: '); |
| 166 | + console.log(timeSeries); |
| 167 | + callback(); |
| 168 | + }); |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * Lists the time series written for the custom metric. The window to read the timeseries starts |
| 173 | + * an hour ago and extends unti the current time, so should include the metric value written by |
| 174 | + * the earlier calls. |
| 175 | + * @param authClient The authorized Google Cloud Monitoring API client |
| 176 | + * @param projectResource The project resource created from the project ID |
| 177 | + * @param callback |
| 178 | + */ |
| 179 | +function listTimeSeries(client, projectResource, callback) { |
| 180 | + var monitoring = google.monitoring('v3'); |
| 181 | + var startTime = getStartTime(); |
| 182 | + var endTime = getNow(); |
| 183 | + monitoring.projects.timeSeries.list({ |
| 184 | + auth: client, |
| 185 | + name: projectResource, |
| 186 | + filter: 'metric.type="' + CUSTOM_METRIC_TYPE + '"', |
| 187 | + pageSize: 3, |
| 188 | + 'interval.startTime': startTime, |
| 189 | + 'interval.endTime': endTime |
| 190 | + }, function (error, timeSeries) { |
| 191 | + if (error) { |
| 192 | + console.log('Error readTimeseries', error); |
| 193 | + return; |
| 194 | + } |
| 195 | + console.log('readTimeseries '); |
| 196 | + console.log(JSON.stringify(timeSeries)); |
| 197 | + callback(); |
| 198 | + }); |
| 199 | +} |
| 200 | + |
| 201 | +google.auth.getApplicationDefault(function (error, authClient) { |
| 202 | + if (error) { |
| 203 | + return callback(error); |
| 204 | + } |
| 205 | + |
| 206 | + // Depending on the environment that provides the default credentials |
| 207 | + // (e.g. Compute Engine, App Engine), the credentials retrieved may require |
| 208 | + // you to specify the scopes you need explicitly. |
| 209 | + // Check for this case, and inject the Cloud Storage scope if required. |
| 210 | + if (authClient.createScopedRequired && |
| 211 | + authClient.createScopedRequired()) { |
| 212 | + authClient = authClient.createScoped(monitoringScopes); |
| 213 | + } |
| 214 | + |
| 215 | + // Create the service object. |
| 216 | + async.series([ |
| 217 | + function (callback) { |
| 218 | + createCustomMetric(authClient, PROJECT_RESOURCE, callback); |
| 219 | + }, function (callback) { |
| 220 | + writeTimeSeriesForCustomMetric(authClient, PROJECT_RESOURCE, callback); |
| 221 | + }, function (callback) { |
| 222 | + // wait 2 seconds for the write to be received |
| 223 | + setTimeout(function () { |
| 224 | + listTimeSeries(authClient, PROJECT_RESOURCE, callback); |
| 225 | + }, 2000) |
| 226 | + }]); |
| 227 | +}); |
0 commit comments