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 606a9d3 commit 303ff63
Show file tree
Hide file tree
Showing 7 changed files with 450 additions and 64 deletions.
57 changes: 30 additions & 27 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,40 @@ 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 [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.
[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.

__Usage:__ `node create_custom_metric <YOUR_PROJECT_ID>`
Options:
--version Show version number [boolean]
--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/
```
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
Loading

0 comments on commit 303ff63

Please sign in to comment.