Skip to content

Commit

Permalink
Add Uptime Check Config API samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Oct 16, 2017
1 parent 6e63034 commit 68ad245
Show file tree
Hide file tree
Showing 7 changed files with 460 additions and 43 deletions.
46 changes: 40 additions & 6 deletions monitoring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [Setup](#setup)
* [Samples](#samples)
* [Metrics](#metrics)
* [Uptime Config](#uptime-config)
* [Listing resources](#listing-resources)
* [Custom metrics](#custom-metrics)
* [Running the tests](#running-the-tests)
Expand Down Expand Up @@ -81,9 +82,42 @@ For more information, see https://cloud.google.com/monitoring/docs
[metrics_0_docs]: https://cloud.google.com/monitoring/docs
[metrics_0_code]: metrics.js

### Uptime Config

View the [documentation][uptime_1_docs] or the [source code][uptime_1_code].

__Usage:__ `node uptime.js --help`

```
Commands:
create <gceInstanceId> [projectId] Creates an uptime check config.
list [filter] [projectId] Lists uptime check configs, optionally filtering the results.
list-ips Lists uptime check config IPs.
get <uptimeCheckConfigId> [projectId] Gets an uptime check config.
delete <uptimeCheckConfigId> [projectId] Deletes an uptime check config.
Options:
--help Show help [boolean]
--projectId, -p [string]
Examples:
node uptime.js create my-instance Create an uptime check for a "my-instance" GCE instance.
node uptime.js list List all uptime check configs.
node uptime.js list "resource.type = gce_instance AND List all uptime check configs for a specific GCE
resource.label.instance_id = mongodb" instance.
node uptime.js list-ips
node uptime.js get My-Uptime-Check
node uptime.js delete My-Uptime-Check
For more information, see https://cloud.google.com/monitoring/uptime-checks/
```

[uptime_1_docs]: https://cloud.google.com/monitoring/docs
[uptime_1_code]: uptime.js

### Listing resources

View the [documentation][list_1_docs] or the [source code][list_1_code].
View the [documentation][list_2_docs] or the [source code][list_2_code].

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

[list_1_docs]: https://cloud.google.com/monitoring/demos/#hello-world
[list_1_code]: list_resources.js
[list_2_docs]: https://cloud.google.com/monitoring/demos/#hello-world
[list_2_code]: list_resources.js

### Custom metrics

View the [documentation][metrics_2_docs] or the [source code][metrics_2_code].
View the [documentation][metrics_3_docs] or the [source code][metrics_3_code].

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

[metrics_2_docs]: https://cloud.google.com/monitoring/demos/#custom_metrics
[metrics_2_code]: create_custom_metric.js
[metrics_3_docs]: https://cloud.google.com/monitoring/demos/#custom_metrics
[metrics_3_code]: create_custom_metric.js

## Running the tests

Expand Down
94 changes: 61 additions & 33 deletions monitoring/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ function createMetricDescriptor (projectId) {
const Monitoring = require('@google-cloud/monitoring');

// Creates a client
const client = Monitoring.v3.metric();
const client = Monitoring.metric();

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

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

// Creates a client
const client = Monitoring.v3.metric();
const client = Monitoring.metric();

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

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

// Creates a client
const client = Monitoring.v3.metric();
const client = Monitoring.metric();

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

// An example of "metricId" is "logging.googleapis.com/log_entry_count"
// const metricId = 'some/metric/id';
// const metricId = 'custom.googleapis.com/your/id';

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

// Creates a client
const client = Monitoring.v3.metric();
const client = Monitoring.metric();

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

// The ID of the Metric Descriptor to delete, e.g.
// const metricId = 'custom.googleapis.com/stores/daily_sales';

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

// Creates a client
const client = Monitoring.v3.metric();
const client = Monitoring.metric();

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

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

// Creates a client
const client = Monitoring.v3.metric();
const client = Monitoring.metric();

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

// An example "filter" is 'metric.type="compute.googleapis.com/instance/cpu/utilization"'
// const filter = 'metric.type="compute.googleapis.com/instance/cpu/utilization"';

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

// Creates a client
const client = Monitoring.v3.metric();
const client = Monitoring.metric();

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

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

// Creates a client
const client = Monitoring.v3.metric();
const client = Monitoring.metric();

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

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

// Creates a client
const client = Monitoring.v3.metric();
const client = Monitoring.metric();

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

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

// Creates a client
const client = Monitoring.v3.metric();
const client = Monitoring.metric();

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

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

console.log('Monitored Resource Descriptors:');
descriptors.forEach((descriptor) => console.log(descriptor.name));
descriptors.forEach((descriptor) => {
if (descriptor.type === 'uptime_url') {
console.log(JSON.stringify(descriptor, null, 2));
} else {
return;
}
console.log(descriptor.name);
console.log(` Type: ${descriptor.type}`);
if (descriptor.labels) {
console.log(` Labels:`);
descriptor.labels.forEach((label) => {
console.log(` ${label.key} (${label.valueType}): ${label.description}`);
});
}
});
})
.catch((err) => {
console.error('ERROR:', err);
Expand All @@ -455,13 +483,13 @@ function getMonitoredResourceDescriptor (projectId, resourceType) {
const Monitoring = require('@google-cloud/monitoring');

// Creates a client
const client = Monitoring.v3.metric();
const client = Monitoring.metric();

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

// "resourceType" should be a predefined type, such as "cloudsql_database"
// const resourceType = 'some_resource_type';
// const resourceType = 'some_resource_type, e.g. cloudsql_database';

const request = {
name: client.monitoredResourceDescriptorPath(projectId, resourceType)
Expand Down
11 changes: 9 additions & 2 deletions monitoring/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"yargs": "9.0.1"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "2.0.4",
"@google-cloud/nodejs-repo-tools": "2.0.9",
"ava": "0.22.0",
"proxyquire": "1.8.0",
"sinon": "4.0.0"
"sinon": "4.0.1"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
Expand All @@ -37,6 +37,13 @@
"file": "metrics.js",
"docs_link": "https://cloud.google.com/monitoring/docs",
"usage": "node metrics.js --help"
},
{
"id": "uptime",
"name": "Uptime Config",
"file": "uptime.js",
"docs_link": "https://cloud.google.com/monitoring/docs",
"usage": "node uptime.js --help"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion monitoring/system-test/metrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

'use strict';

const client = require(`@google-cloud/monitoring`).v3.metric();
const client = require(`@google-cloud/monitoring`).metric();
const path = require(`path`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);
Expand Down
2 changes: 1 addition & 1 deletion monitoring/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const sinon = require(`sinon`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

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

test.beforeEach(tools.stubConsole);
test.afterEach.always(tools.restoreConsole);
Expand Down
80 changes: 80 additions & 0 deletions monitoring/system-test/uptime.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright 2017, 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.
*/

'use strict';

const client = require(`@google-cloud/monitoring`).uptimeCheck();
const path = require(`path`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

const cmd = `node uptime.js`;
const cwd = path.join(__dirname, `..`);
const projectId = process.env.GCLOUD_PROJECT;
const instanceId = 'uptime-test-' + Date.now();

test.before(tools.checkCredentials);

test(`should get an uptime check`, async (t) => {
t.regex(await tools.runAsync(`${cmd} list-ips`, cwd), /USA/);
});

let id;

test.serial(`should create an uptime check`, async (t) => {
const results = await tools.runAsyncWithIO(`${cmd} create ${instanceId}`, cwd);
const output = results.stdout + results.stderr;
const matches = output.match(new RegExp(`ID: projects/${projectId}/uptimeCheckConfigs/(.+)`));
id = matches[1];
t.regex(output, /Uptime check created:/);
t.regex(output, new RegExp(`Resource: {"type":"gce_instance","labels":{"instance_id":"${instanceId}"}}`));
t.regex(output, /Display Name: My GCE Instance Uptime Check/);
});

test.serial(`should get an uptime check`, async (t) => {
const results = await tools.runAsyncWithIO(`${cmd} get ${id}`, cwd);
const output = results.stdout + results.stderr;
t.regex(output, new RegExp(`Retrieving projects/${projectId}/uptimeCheckConfigs/${id}`));
t.regex(output, new RegExp(`Resource: {"type":"gce_instance","labels":{"instance_id":"${instanceId}"}}`));
});

let allLength;

test.serial(`should list uptime checks`, async (t) => {
t.plan(0);
await tools.tryTest(async (assert) => {
const results = await tools.runAsyncWithIO(`${cmd} list`, cwd);
const output = results.stdout + results.stderr;
allLength = output.match(/ID:/gi).length;
assert((new RegExp(`Resource: {"type":"gce_instance","labels":{"instance_id":"${instanceId}"}}`)).test(output));
assert(/Display Name: My GCE Instance Uptime Check/.test(output));
}).start();
});

test.serial(`should list uptime checks with a filter`, async (t) => {
const results = await tools.runAsyncWithIO(`${cmd} list "resource.type = gce_instance AND resource.label.instance_id = ${instanceId}"`, cwd);
const output = results.stdout + results.stderr;
t.true(output.match(/ID:/gi).length <= allLength);
t.regex(output, new RegExp(`Resource: {"type":"gce_instance","labels":{"instance_id":"${instanceId}"}}`));
t.regex(output, /Display Name: My GCE Instance Uptime Check/);
});

test.serial(`should delete an uptime check`, async (t) => {
const results = await tools.runAsyncWithIO(`${cmd} delete ${id}`, cwd);
const output = results.stdout + results.stderr;
t.regex(output, new RegExp(`Deleting projects/${projectId}/uptimeCheckConfigs/${id}`));
t.regex(output, new RegExp(`projects/${projectId}/uptimeCheckConfigs/${id} deleted.`));
});

Loading

0 comments on commit 68ad245

Please sign in to comment.