Skip to content

Commit 68ad245

Browse files
committed
Add Uptime Check Config API samples.
1 parent 6e63034 commit 68ad245

File tree

7 files changed

+460
-43
lines changed

7 files changed

+460
-43
lines changed

monitoring/README.md

+40-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [Setup](#setup)
1212
* [Samples](#samples)
1313
* [Metrics](#metrics)
14+
* [Uptime Config](#uptime-config)
1415
* [Listing resources](#listing-resources)
1516
* [Custom metrics](#custom-metrics)
1617
* [Running the tests](#running-the-tests)
@@ -81,9 +82,42 @@ For more information, see https://cloud.google.com/monitoring/docs
8182
[metrics_0_docs]: https://cloud.google.com/monitoring/docs
8283
[metrics_0_code]: metrics.js
8384

85+
### Uptime Config
86+
87+
View the [documentation][uptime_1_docs] or the [source code][uptime_1_code].
88+
89+
__Usage:__ `node uptime.js --help`
90+
91+
```
92+
Commands:
93+
create <gceInstanceId> [projectId] Creates an uptime check config.
94+
list [filter] [projectId] Lists uptime check configs, optionally filtering the results.
95+
list-ips Lists uptime check config IPs.
96+
get <uptimeCheckConfigId> [projectId] Gets an uptime check config.
97+
delete <uptimeCheckConfigId> [projectId] Deletes an uptime check config.
98+
99+
Options:
100+
--help Show help [boolean]
101+
--projectId, -p [string]
102+
103+
Examples:
104+
node uptime.js create my-instance Create an uptime check for a "my-instance" GCE instance.
105+
node uptime.js list List all uptime check configs.
106+
node uptime.js list "resource.type = gce_instance AND List all uptime check configs for a specific GCE
107+
resource.label.instance_id = mongodb" instance.
108+
node uptime.js list-ips
109+
node uptime.js get My-Uptime-Check
110+
node uptime.js delete My-Uptime-Check
111+
112+
For more information, see https://cloud.google.com/monitoring/uptime-checks/
113+
```
114+
115+
[uptime_1_docs]: https://cloud.google.com/monitoring/docs
116+
[uptime_1_code]: uptime.js
117+
84118
### Listing resources
85119

86-
View the [documentation][list_1_docs] or the [source code][list_1_code].
120+
View the [documentation][list_2_docs] or the [source code][list_2_code].
87121

88122
`list_resources.js` is a command-line program to demonstrate connecting to the
89123
Google Monitoring API to retrieve API data.
@@ -94,12 +128,12 @@ __Usage:__ `node list_resources <YOUR_PROJECT_ID>`
94128
node list_resources my-cool-project
95129
```
96130

97-
[list_1_docs]: https://cloud.google.com/monitoring/demos/#hello-world
98-
[list_1_code]: list_resources.js
131+
[list_2_docs]: https://cloud.google.com/monitoring/demos/#hello-world
132+
[list_2_code]: list_resources.js
99133

100134
### Custom metrics
101135

102-
View the [documentation][metrics_2_docs] or the [source code][metrics_2_code].
136+
View the [documentation][metrics_3_docs] or the [source code][metrics_3_code].
103137

104138
`create_custom_metric.js` demonstrates how to create a custom metric, write a
105139
timeseries value to it, and read it back.
@@ -110,8 +144,8 @@ __Usage:__ `node create_custom_metric <YOUR_PROJECT_ID>`
110144
node create_custom_metric my-cool-project
111145
```
112146

113-
[metrics_2_docs]: https://cloud.google.com/monitoring/demos/#custom_metrics
114-
[metrics_2_code]: create_custom_metric.js
147+
[metrics_3_docs]: https://cloud.google.com/monitoring/demos/#custom_metrics
148+
[metrics_3_code]: create_custom_metric.js
115149

116150
## Running the tests
117151

monitoring/metrics.js

+61-33
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ function createMetricDescriptor (projectId) {
2929
const Monitoring = require('@google-cloud/monitoring');
3030

3131
// Creates a client
32-
const client = Monitoring.v3.metric();
32+
const client = Monitoring.metric();
3333

34-
// The Google Cloud Platform project on which to execute the request
34+
/**
35+
* TODO(developer): Uncomment and edit the following lines of code.
36+
*/
3537
// const projectId = 'YOUR_PROJECT_ID';
3638

3739
const request = {
@@ -82,9 +84,11 @@ function listMetricDescriptors (projectId) {
8284
const Monitoring = require('@google-cloud/monitoring');
8385

8486
// Creates a client
85-
const client = Monitoring.v3.metric();
87+
const client = Monitoring.metric();
8688

87-
// The Google Cloud Platform project on which to execute the request
89+
/**
90+
* TODO(developer): Uncomment and edit the following lines of code.
91+
*/
8892
// const projectId = 'YOUR_PROJECT_ID';
8993

9094
const request = {
@@ -111,13 +115,13 @@ function getMetricDescriptor (projectId, metricId) {
111115
const Monitoring = require('@google-cloud/monitoring');
112116

113117
// Creates a client
114-
const client = Monitoring.v3.metric();
118+
const client = Monitoring.metric();
115119

116-
// The Google Cloud Platform project on which to execute the request
120+
/**
121+
* TODO(developer): Uncomment and edit the following lines of code.
122+
*/
117123
// const projectId = 'YOUR_PROJECT_ID';
118-
119-
// An example of "metricId" is "logging.googleapis.com/log_entry_count"
120-
// const metricId = 'some/metric/id';
124+
// const metricId = 'custom.googleapis.com/your/id';
121125

122126
const request = {
123127
name: client.metricDescriptorPath(projectId, metricId)
@@ -151,12 +155,12 @@ function deleteMetricDescriptor (projectId, metricId) {
151155
const Monitoring = require('@google-cloud/monitoring');
152156

153157
// Creates a client
154-
const client = Monitoring.v3.metric();
158+
const client = Monitoring.metric();
155159

156-
// The Google Cloud Platform project on which to execute the request
160+
/**
161+
* TODO(developer): Uncomment and edit the following lines of code.
162+
*/
157163
// const projectId = 'YOUR_PROJECT_ID';
158-
159-
// The ID of the Metric Descriptor to delete, e.g.
160164
// const metricId = 'custom.googleapis.com/stores/daily_sales';
161165

162166
const request = {
@@ -180,9 +184,11 @@ function writeTimeSeriesData (projectId, metricId) {
180184
const Monitoring = require('@google-cloud/monitoring');
181185

182186
// Creates a client
183-
const client = Monitoring.v3.metric();
187+
const client = Monitoring.metric();
184188

185-
// The Google Cloud Platform project on which to execute the request
189+
/**
190+
* TODO(developer): Uncomment and edit the following lines of code.
191+
*/
186192
// const projectId = 'YOUR_PROJECT_ID';
187193

188194
const dataPoint = {
@@ -238,12 +244,12 @@ function readTimeSeriesData (projectId, filter) {
238244
const Monitoring = require('@google-cloud/monitoring');
239245

240246
// Creates a client
241-
const client = Monitoring.v3.metric();
247+
const client = Monitoring.metric();
242248

243-
// The Google Cloud Platform project on which to execute the request
249+
/**
250+
* TODO(developer): Uncomment and edit the following lines of code.
251+
*/
244252
// const projectId = 'YOUR_PROJECT_ID';
245-
246-
// An example "filter" is 'metric.type="compute.googleapis.com/instance/cpu/utilization"'
247253
// const filter = 'metric.type="compute.googleapis.com/instance/cpu/utilization"';
248254

249255
const request = {
@@ -284,9 +290,11 @@ function readTimeSeriesFields (projectId) {
284290
const Monitoring = require('@google-cloud/monitoring');
285291

286292
// Creates a client
287-
const client = Monitoring.v3.metric();
293+
const client = Monitoring.metric();
288294

289-
// The Google Cloud Platform project on which to execute the request
295+
/**
296+
* TODO(developer): Uncomment and edit the following lines of code.
297+
*/
290298
// const projectId = 'YOUR_PROJECT_ID';
291299

292300
const request = {
@@ -328,9 +336,11 @@ function readTimeSeriesAggregate (projectId) {
328336
const Monitoring = require('@google-cloud/monitoring');
329337

330338
// Creates a client
331-
const client = Monitoring.v3.metric();
339+
const client = Monitoring.metric();
332340

333-
// The Google Cloud Platform project on which to execute the request
341+
/**
342+
* TODO(developer): Uncomment and edit the following lines of code.
343+
*/
334344
// const projectId = 'YOUR_PROJECT_ID';
335345

336346
const request = {
@@ -378,9 +388,11 @@ function readTimeSeriesReduce (projectId) {
378388
const Monitoring = require('@google-cloud/monitoring');
379389

380390
// Creates a client
381-
const client = Monitoring.v3.metric();
391+
const client = Monitoring.metric();
382392

383-
// The Google Cloud Platform project on which to execute the request
393+
/**
394+
* TODO(developer): Uncomment and edit the following lines of code.
395+
*/
384396
// const projectId = 'YOUR_PROJECT_ID';
385397

386398
const request = {
@@ -426,9 +438,11 @@ function listMonitoredResourceDescriptors (projectId) {
426438
const Monitoring = require('@google-cloud/monitoring');
427439

428440
// Creates a client
429-
const client = Monitoring.v3.metric();
441+
const client = Monitoring.metric();
430442

431-
// The Google Cloud Platform project on which to execute the request
443+
/**
444+
* TODO(developer): Uncomment and edit the following lines of code.
445+
*/
432446
// const projectId = 'YOUR_PROJECT_ID';
433447

434448
const request = {
@@ -441,7 +455,21 @@ function listMonitoredResourceDescriptors (projectId) {
441455
const descriptors = results[0];
442456

443457
console.log('Monitored Resource Descriptors:');
444-
descriptors.forEach((descriptor) => console.log(descriptor.name));
458+
descriptors.forEach((descriptor) => {
459+
if (descriptor.type === 'uptime_url') {
460+
console.log(JSON.stringify(descriptor, null, 2));
461+
} else {
462+
return;
463+
}
464+
console.log(descriptor.name);
465+
console.log(` Type: ${descriptor.type}`);
466+
if (descriptor.labels) {
467+
console.log(` Labels:`);
468+
descriptor.labels.forEach((label) => {
469+
console.log(` ${label.key} (${label.valueType}): ${label.description}`);
470+
});
471+
}
472+
});
445473
})
446474
.catch((err) => {
447475
console.error('ERROR:', err);
@@ -455,13 +483,13 @@ function getMonitoredResourceDescriptor (projectId, resourceType) {
455483
const Monitoring = require('@google-cloud/monitoring');
456484

457485
// Creates a client
458-
const client = Monitoring.v3.metric();
486+
const client = Monitoring.metric();
459487

460-
// The Google Cloud Platform project on which to execute the request
488+
/**
489+
* TODO(developer): Uncomment and edit the following lines of code.
490+
*/
461491
// const projectId = 'YOUR_PROJECT_ID';
462-
463-
// "resourceType" should be a predefined type, such as "cloudsql_database"
464-
// const resourceType = 'some_resource_type';
492+
// const resourceType = 'some_resource_type, e.g. cloudsql_database';
465493

466494
const request = {
467495
name: client.monitoredResourceDescriptorPath(projectId, resourceType)

monitoring/package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
"yargs": "9.0.1"
2222
},
2323
"devDependencies": {
24-
"@google-cloud/nodejs-repo-tools": "2.0.4",
24+
"@google-cloud/nodejs-repo-tools": "2.0.9",
2525
"ava": "0.22.0",
2626
"proxyquire": "1.8.0",
27-
"sinon": "4.0.0"
27+
"sinon": "4.0.1"
2828
},
2929
"cloud-repo-tools": {
3030
"requiresKeyFile": true,
@@ -37,6 +37,13 @@
3737
"file": "metrics.js",
3838
"docs_link": "https://cloud.google.com/monitoring/docs",
3939
"usage": "node metrics.js --help"
40+
},
41+
{
42+
"id": "uptime",
43+
"name": "Uptime Config",
44+
"file": "uptime.js",
45+
"docs_link": "https://cloud.google.com/monitoring/docs",
46+
"usage": "node uptime.js --help"
4047
}
4148
]
4249
}

monitoring/system-test/metrics.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
'use strict';
1717

18-
const client = require(`@google-cloud/monitoring`).v3.metric();
18+
const client = require(`@google-cloud/monitoring`).metric();
1919
const path = require(`path`);
2020
const test = require(`ava`);
2121
const tools = require(`@google-cloud/nodejs-repo-tools`);

monitoring/system-test/quickstart.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const sinon = require(`sinon`);
2020
const test = require(`ava`);
2121
const tools = require(`@google-cloud/nodejs-repo-tools`);
2222

23-
const client = proxyquire(`@google-cloud/monitoring`, {}).v3.metric();
23+
const client = proxyquire(`@google-cloud/monitoring`, {}).metric();
2424

2525
test.beforeEach(tools.stubConsole);
2626
test.afterEach.always(tools.restoreConsole);

monitoring/system-test/uptime.test.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const client = require(`@google-cloud/monitoring`).uptimeCheck();
19+
const path = require(`path`);
20+
const test = require(`ava`);
21+
const tools = require(`@google-cloud/nodejs-repo-tools`);
22+
23+
const cmd = `node uptime.js`;
24+
const cwd = path.join(__dirname, `..`);
25+
const projectId = process.env.GCLOUD_PROJECT;
26+
const instanceId = 'uptime-test-' + Date.now();
27+
28+
test.before(tools.checkCredentials);
29+
30+
test(`should get an uptime check`, async (t) => {
31+
t.regex(await tools.runAsync(`${cmd} list-ips`, cwd), /USA/);
32+
});
33+
34+
let id;
35+
36+
test.serial(`should create an uptime check`, async (t) => {
37+
const results = await tools.runAsyncWithIO(`${cmd} create ${instanceId}`, cwd);
38+
const output = results.stdout + results.stderr;
39+
const matches = output.match(new RegExp(`ID: projects/${projectId}/uptimeCheckConfigs/(.+)`));
40+
id = matches[1];
41+
t.regex(output, /Uptime check created:/);
42+
t.regex(output, new RegExp(`Resource: {"type":"gce_instance","labels":{"instance_id":"${instanceId}"}}`));
43+
t.regex(output, /Display Name: My GCE Instance Uptime Check/);
44+
});
45+
46+
test.serial(`should get an uptime check`, async (t) => {
47+
const results = await tools.runAsyncWithIO(`${cmd} get ${id}`, cwd);
48+
const output = results.stdout + results.stderr;
49+
t.regex(output, new RegExp(`Retrieving projects/${projectId}/uptimeCheckConfigs/${id}`));
50+
t.regex(output, new RegExp(`Resource: {"type":"gce_instance","labels":{"instance_id":"${instanceId}"}}`));
51+
});
52+
53+
let allLength;
54+
55+
test.serial(`should list uptime checks`, async (t) => {
56+
t.plan(0);
57+
await tools.tryTest(async (assert) => {
58+
const results = await tools.runAsyncWithIO(`${cmd} list`, cwd);
59+
const output = results.stdout + results.stderr;
60+
allLength = output.match(/ID:/gi).length;
61+
assert((new RegExp(`Resource: {"type":"gce_instance","labels":{"instance_id":"${instanceId}"}}`)).test(output));
62+
assert(/Display Name: My GCE Instance Uptime Check/.test(output));
63+
}).start();
64+
});
65+
66+
test.serial(`should list uptime checks with a filter`, async (t) => {
67+
const results = await tools.runAsyncWithIO(`${cmd} list "resource.type = gce_instance AND resource.label.instance_id = ${instanceId}"`, cwd);
68+
const output = results.stdout + results.stderr;
69+
t.true(output.match(/ID:/gi).length <= allLength);
70+
t.regex(output, new RegExp(`Resource: {"type":"gce_instance","labels":{"instance_id":"${instanceId}"}}`));
71+
t.regex(output, /Display Name: My GCE Instance Uptime Check/);
72+
});
73+
74+
test.serial(`should delete an uptime check`, async (t) => {
75+
const results = await tools.runAsyncWithIO(`${cmd} delete ${id}`, cwd);
76+
const output = results.stdout + results.stderr;
77+
t.regex(output, new RegExp(`Deleting projects/${projectId}/uptimeCheckConfigs/${id}`));
78+
t.regex(output, new RegExp(`projects/${projectId}/uptimeCheckConfigs/${id} deleted.`));
79+
});
80+

0 commit comments

Comments
 (0)