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 17, 2017
1 parent 606a9d3 commit d156bde
Show file tree
Hide file tree
Showing 7 changed files with 434 additions and 63 deletions.
53 changes: 27 additions & 26 deletions monitoring/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>

# Stackdriver Monitoring Node.js Samples
# Stackdriver Monitoring: Node.js Samples

[![Build](https://storage.googleapis.com/cloud-docs-samples-badges/GoogleCloudPlatform/nodejs-docs-samples/nodejs-docs-samples-monitoring.svg)]()
[![Build](https://storage.googleapis.com/.svg)]()

[Stackdriver Monitoring](https://cloud.google.com/monitoring/docs) collects metrics, events, and metadata from Google Cloud Platform, Amazon Web Services (AWS), hosted uptime probes, application instrumentation, and a variety of common application components including Cassandra, Nginx, Apache Web Server, Elasticsearch and many others.

Expand All @@ -11,8 +11,7 @@
* [Setup](#setup)
* [Samples](#samples)
* [Metrics](#metrics)
* [Listing resources](#listing-resources)
* [Custom metrics](#custom-metrics)
* [Uptime Config](#uptime-config)
* [Running the tests](#running-the-tests)

## Setup
Expand Down Expand Up @@ -59,6 +58,7 @@ Commands:
get-resource <resourceType> [projectId] Get a monitored resource descriptor.
Options:
--version Show version number [boolean]
--help Show help [boolean]
--projectId, -p [string]
Expand All @@ -81,37 +81,38 @@ For more information, see https://cloud.google.com/monitoring/docs
[metrics_0_docs]: https://cloud.google.com/monitoring/docs
[metrics_0_code]: metrics.js

### Listing resources
### Uptime Config

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

`list_resources.js` is a command-line program to demonstrate connecting to the
Google Monitoring API to retrieve API data.
__Usage:__ `node uptime.js --help`

__Usage:__ `node list_resources <YOUR_PROJECT_ID>`

```
node list_resources my-cool-project
```
Commands:
create <gceInstanceId> [projectId] Creates an uptime check config.
list [projectId] Lists uptime check configs.
list-ips Lists uptime check config IPs.
get <uptimeCheckConfigId> [projectId] Gets an uptime check config.
delete <uptimeCheckConfigId> [projectId] Deletes an uptime check config.
[list_1_docs]: https://cloud.google.com/monitoring/demos/#hello-world
[list_1_code]: list_resources.js

### Custom metrics

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

`create_custom_metric.js` demonstrates how to create a custom metric, write a
timeseries value to it, and read it back.
Options:
--version Show version number [boolean]
--help Show help [boolean]
--projectId, -p [string]
__Usage:__ `node create_custom_metric <YOUR_PROJECT_ID>`
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-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/
```
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
[uptime_1_docs]: https://cloud.google.com/monitoring/docs
[uptime_1_code]: uptime.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
69 changes: 69 additions & 0 deletions monitoring/system-test/uptime.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* 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}"}}`));
});

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;
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 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 d156bde

Please sign in to comment.