From 3a476266488a516c0984aa78fed9a8f4623d9989 Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Thu, 19 Oct 2017 07:09:30 -0700 Subject: [PATCH] Add Uptime Check Service, Migrate Repository (#2) --- monitoring/snippets/.eslintrc.yml | 3 + monitoring/snippets/README.md | 46 +-- monitoring/snippets/metrics.js | 315 +++++++++--------- monitoring/snippets/package.json | 30 +- monitoring/snippets/quickstart.js | 37 +- monitoring/snippets/system-test/.eslintrc.yml | 5 + .../snippets/system-test/metrics.test.js | 112 ++++--- .../snippets/system-test/quickstart.test.js | 52 +-- .../snippets/system-test/uptime.test.js | 59 ++-- monitoring/snippets/uptime.js | 175 ++++++---- 10 files changed, 431 insertions(+), 403 deletions(-) create mode 100644 monitoring/snippets/.eslintrc.yml create mode 100644 monitoring/snippets/system-test/.eslintrc.yml diff --git a/monitoring/snippets/.eslintrc.yml b/monitoring/snippets/.eslintrc.yml new file mode 100644 index 0000000000..282535f55f --- /dev/null +++ b/monitoring/snippets/.eslintrc.yml @@ -0,0 +1,3 @@ +--- +rules: + no-console: off diff --git a/monitoring/snippets/README.md b/monitoring/snippets/README.md index 47cacb7688..9fb9892c04 100644 --- a/monitoring/snippets/README.md +++ b/monitoring/snippets/README.md @@ -8,33 +8,22 @@ ## Table of Contents -* [Setup](#setup) +* [Before you begin](#before-you-begin) * [Samples](#samples) * [Metrics](#metrics) * [Uptime Config](#uptime-config) -* [Running the tests](#running-the-tests) -## Setup +## Before you begin -1. Read [Prerequisites][prereq] and [How to run a sample][run] first. -1. Install dependencies: - - With **npm**: - - npm install - - With **yarn**: - - yarn install - -[prereq]: ../README.md#prerequisites -[run]: ../README.md#how-to-run-a-sample +Before running the samples, make sure you've followed the steps in the +[Before you begin section](../README.md#before-you-begin) of the client +library's README. ## Samples ### Metrics -View the [documentation][metrics_0_docs] or the [source code][metrics_0_code]. +View the [source code][metrics_0_code]. __Usage:__ `node metrics.js --help` @@ -60,7 +49,7 @@ Commands: Options: --version Show version number [boolean] --help Show help [boolean] - --projectId, -p [string] + --projectId, -p [string] [default: "nodejs-docs-samples"] Examples: node metrics.js create @@ -83,7 +72,7 @@ For more information, see https://cloud.google.com/monitoring/docs ### Uptime Config -View the [documentation][uptime_1_docs] or the [source code][uptime_1_code]. +View the [source code][uptime_1_code]. __Usage:__ `node uptime.js --help` @@ -98,11 +87,13 @@ Commands: Options: --version Show version number [boolean] --help Show help [boolean] - --projectId, -p [string] + --projectId, -p [string] [default: "nodejs-docs-samples"] 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 @@ -112,18 +103,3 @@ 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 - - -## Running the tests - -1. Set the **GCLOUD_PROJECT** and **GOOGLE_APPLICATION_CREDENTIALS** environment variables. - -1. Run the tests: - - With **npm**: - - npm test - - With **yarn**: - - yarn test diff --git a/monitoring/snippets/metrics.js b/monitoring/snippets/metrics.js index 52d899d973..cc077efe6e 100644 --- a/monitoring/snippets/metrics.js +++ b/monitoring/snippets/metrics.js @@ -23,13 +23,13 @@ 'use strict'; -function createMetricDescriptor (projectId) { +function createMetricDescriptor(projectId) { // [START monitoring_create_metric] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.metric(); + const client = new monitoring.MetricServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -49,15 +49,16 @@ function createMetricDescriptor (projectId) { { key: 'store_id', valueType: 'STRING', - description: 'The ID of the store.' - } - ] - } + description: 'The ID of the store.', + }, + ], + }, }; // Creates a custom metric descriptor - client.createMetricDescriptor(request) - .then((results) => { + client + .createMetricDescriptor(request) + .then(results => { const descriptor = results[0]; console.log('Created custom Metric:\n'); @@ -68,23 +69,25 @@ function createMetricDescriptor (projectId) { console.log(`Value Type: ${descriptor.valueType}`); console.log(`Unit: ${descriptor.unit}`); console.log('Labels:'); - descriptor.labels.forEach((label) => { - console.log(` ${label.key} (${label.valueType}) - ${label.description}`); + descriptor.labels.forEach(label => { + console.log( + ` ${label.key} (${label.valueType}) - ${label.description}` + ); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_create_metric] } -function listMetricDescriptors (projectId) { +function listMetricDescriptors(projectId) { // [START monitoring_list_descriptors] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.metric(); + const client = new monitoring.MetricServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -92,30 +95,31 @@ function listMetricDescriptors (projectId) { // const projectId = 'YOUR_PROJECT_ID'; const request = { - name: client.projectPath(projectId) + name: client.projectPath(projectId), }; // Lists metric descriptors - client.listMetricDescriptors(request) - .then((results) => { + client + .listMetricDescriptors(request) + .then(results => { const descriptors = results[0]; console.log('Metric Descriptors:'); - descriptors.forEach((descriptor) => console.log(descriptor.name)); + descriptors.forEach(descriptor => console.log(descriptor.name)); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_list_descriptors] } -function getMetricDescriptor (projectId, metricId) { +function getMetricDescriptor(projectId, metricId) { // [START monitoring_get_descriptor] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.metric(); + const client = new monitoring.MetricServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -124,12 +128,13 @@ function getMetricDescriptor (projectId, metricId) { // const metricId = 'custom.googleapis.com/your/id'; const request = { - name: client.metricDescriptorPath(projectId, metricId) + name: client.metricDescriptorPath(projectId, metricId), }; // Retrieves a metric descriptor - client.getMetricDescriptor(request) - .then((results) => { + client + .getMetricDescriptor(request) + .then(results => { const descriptor = results[0]; console.log(`Name: ${descriptor.displayName}`); @@ -139,23 +144,25 @@ function getMetricDescriptor (projectId, metricId) { console.log(`Value Type: ${descriptor.valueType}`); console.log(`Unit: ${descriptor.unit}`); console.log('Labels:'); - descriptor.labels.forEach((label) => { - console.log(` ${label.key} (${label.valueType}) - ${label.description}`); + descriptor.labels.forEach(label => { + console.log( + ` ${label.key} (${label.valueType}) - ${label.description}` + ); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_get_descriptor] } -function deleteMetricDescriptor (projectId, metricId) { +function deleteMetricDescriptor(projectId, metricId) { // [START monitoring_delete_metric] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.metric(); + const client = new monitoring.MetricServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -164,27 +171,28 @@ function deleteMetricDescriptor (projectId, metricId) { // const metricId = 'custom.googleapis.com/stores/daily_sales'; const request = { - name: client.metricDescriptorPath(projectId, metricId) + name: client.metricDescriptorPath(projectId, metricId), }; // Deletes a metric descriptor - client.deleteMetricDescriptor(request) - .then((results) => { - console.log(`Deleted ${metricId}`); + client + .deleteMetricDescriptor(request) + .then(results => { + console.log(`Deleted ${metricId}`, results[0]); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_delete_metric] } -function writeTimeSeriesData (projectId, metricId) { +function writeTimeSeriesData(projectId) { // [START monitoring_write_timeseries] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.metric(); + const client = new monitoring.MetricServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -194,57 +202,54 @@ function writeTimeSeriesData (projectId, metricId) { const dataPoint = { interval: { endTime: { - seconds: Date.now() / 1000 - } + seconds: Date.now() / 1000, + }, }, value: { - doubleValue: 123.45 - } + doubleValue: 123.45, + }, }; const timeSeriesData = { metric: { type: 'custom.googleapis.com/stores/daily_sales', labels: { - store_id: 'Pittsburgh' - } + store_id: 'Pittsburgh', + }, }, resource: { type: 'global', labels: { - project_id: projectId - } + project_id: projectId, + }, }, - points: [ - dataPoint - ] + points: [dataPoint], }; const request = { name: client.projectPath(projectId), - timeSeries: [ - timeSeriesData - ] + timeSeries: [timeSeriesData], }; // Writes time series data - client.createTimeSeries(request) - .then((results) => { - console.log(`Done writing time series data.`); + client + .createTimeSeries(request) + .then(results => { + console.log(`Done writing time series data.`, results[0]); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_write_timeseries] } -function readTimeSeriesData (projectId, filter) { +function readTimeSeriesData(projectId, filter) { // [START monitoring_read_timeseries_simple] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.metric(); + const client = new monitoring.MetricServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -258,39 +263,40 @@ function readTimeSeriesData (projectId, filter) { interval: { startTime: { // Limit results to the last 20 minutes - seconds: (Date.now() / 1000) - (60 * 20) + seconds: Date.now() / 1000 - 60 * 20, }, endTime: { - seconds: Date.now() / 1000 - } - } + seconds: Date.now() / 1000, + }, + }, }; // Writes time series data - client.listTimeSeries(request) - .then((results) => { + client + .listTimeSeries(request) + .then(results => { const timeSeries = results[0]; - timeSeries.forEach((data) => { + timeSeries.forEach(data => { console.log(`${data.metric.labels.instance_name}:`); - data.points.forEach((point) => { + data.points.forEach(point => { console.log(JSON.stringify(point.value)); }); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_read_timeseries_simple] } -function readTimeSeriesFields (projectId) { +function readTimeSeriesFields(projectId) { // [START monitoring_read_timeseries_fields] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.metric(); + const client = new monitoring.MetricServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -303,40 +309,41 @@ function readTimeSeriesFields (projectId) { interval: { startTime: { // Limit results to the last 20 minutes - seconds: (Date.now() / 1000) - (60 * 20) + seconds: Date.now() / 1000 - 60 * 20, }, endTime: { - seconds: Date.now() / 1000 - } + seconds: Date.now() / 1000, + }, }, // Don't return time series data, instead just return information about // the metrics that match the filter - view: 'HEADERS' + view: 'HEADERS', }; // Writes time series data - client.listTimeSeries(request) - .then((results) => { + client + .listTimeSeries(request) + .then(results => { const timeSeries = results[0]; console.log('Found data points for the following instances:'); - timeSeries.forEach((data) => { + timeSeries.forEach(data => { console.log(data.metric.labels.instance_name); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_read_timeseries_fields] } -function readTimeSeriesAggregate (projectId) { +function readTimeSeriesAggregate(projectId) { // [START monitoring_read_timeseries_align] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.metric(); + const client = new monitoring.MetricServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -349,46 +356,47 @@ function readTimeSeriesAggregate (projectId) { interval: { startTime: { // Limit results to the last 20 minutes - seconds: (Date.now() / 1000) - (60 * 20) + seconds: Date.now() / 1000 - 60 * 20, }, endTime: { - seconds: Date.now() / 1000 - } + seconds: Date.now() / 1000, + }, }, // Aggregate results per matching instance aggregation: { alignmentPeriod: { - seconds: 600 + seconds: 600, }, - perSeriesAligner: 'ALIGN_MEAN' - } + perSeriesAligner: 'ALIGN_MEAN', + }, }; // Writes time series data - client.listTimeSeries(request) - .then((results) => { + client + .listTimeSeries(request) + .then(results => { const timeSeries = results[0]; console.log('CPU utilization:'); - timeSeries.forEach((data) => { + timeSeries.forEach(data => { console.log(data.metric.labels.instance_name); console.log(` Now: ${data.points[0].value.doubleValue}`); console.log(` 10 min ago: ${data.points[1].value.doubleValue}`); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_read_timeseries_align] } -function readTimeSeriesReduce (projectId) { +function readTimeSeriesReduce(projectId) { // [START monitoring_read_timeseries_reduce] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.metric(); + const client = new monitoring.MetricServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -401,44 +409,45 @@ function readTimeSeriesReduce (projectId) { interval: { startTime: { // Limit results to the last 20 minutes - seconds: (Date.now() / 1000) - (60 * 20) + seconds: Date.now() / 1000 - 60 * 20, }, endTime: { - seconds: Date.now() / 1000 - } + seconds: Date.now() / 1000, + }, }, // Aggregate results per matching instance aggregation: { alignmentPeriod: { - seconds: 600 + seconds: 600, }, crossSeriesReducer: 'REDUCE_MEAN', - perSeriesAligner: 'ALIGN_MEAN' - } + perSeriesAligner: 'ALIGN_MEAN', + }, }; // Writes time series data - client.listTimeSeries(request) - .then((results) => { + client + .listTimeSeries(request) + .then(results => { const reductions = results[0][0].points; console.log('Average CPU utilization across all GCE instances:'); console.log(` Last 10 min: ${reductions[0].value.doubleValue}`); console.log(` 10-20 min ago: ${reductions[0].value.doubleValue}`); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_read_timeseries_reduce] } -function listMonitoredResourceDescriptors (projectId) { +function listMonitoredResourceDescriptors(projectId) { // [START monitoring_list_resources] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.metric(); + const client = new monitoring.MetricServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -446,44 +455,43 @@ function listMonitoredResourceDescriptors (projectId) { // const projectId = 'YOUR_PROJECT_ID'; const request = { - name: client.projectPath(projectId) + name: client.projectPath(projectId), }; // Lists monitored resource descriptors - client.listMonitoredResourceDescriptors(request) - .then((results) => { + client + .listMonitoredResourceDescriptors(request) + .then(results => { const descriptors = results[0]; console.log('Monitored Resource Descriptors:'); - descriptors.forEach((descriptor) => { - if (descriptor.type === 'uptime_url') { - console.log(JSON.stringify(descriptor, null, 2)); - } else { - return; - } + descriptors.forEach(descriptor => { 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}`); + descriptor.labels.forEach(label => { + console.log( + ` ${label.key} (${label.valueType}): ${label.description}` + ); }); } + console.log(); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_list_resources] } -function getMonitoredResourceDescriptor (projectId, resourceType) { +function getMonitoredResourceDescriptor(projectId, resourceType) { // [START monitoring_get_resource] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.metric(); + const client = new monitoring.MetricServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -492,23 +500,26 @@ function getMonitoredResourceDescriptor (projectId, resourceType) { // const resourceType = 'some_resource_type, e.g. cloudsql_database'; const request = { - name: client.monitoredResourceDescriptorPath(projectId, resourceType) + name: client.monitoredResourceDescriptorPath(projectId, resourceType), }; // Lists monitored resource descriptors - client.getMonitoredResourceDescriptor(request) - .then((results) => { + client + .getMonitoredResourceDescriptor(request) + .then(results => { const descriptor = results[0]; console.log(`Name: ${descriptor.displayName}`); console.log(`Description: ${descriptor.description}`); console.log(`Type: ${descriptor.type}`); console.log('Labels:'); - descriptor.labels.forEach((label) => { - console.log(` ${label.key} (${label.valueType}) - ${label.description}`); + descriptor.labels.forEach(label => { + console.log( + ` ${label.key} (${label.valueType}) - ${label.description}` + ); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_get_resource] @@ -520,67 +531,61 @@ const cli = require(`yargs`) `create [projectId]`, `Creates an example 'custom.googleapis.com/stores/daily_sales' custom metric descriptor.`, {}, - (opts) => createMetricDescriptor(opts.projectId) + opts => createMetricDescriptor(opts.projectId) ) - .command( - `list [projectId]`, - `Lists metric descriptors.`, - {}, - (opts) => listMetricDescriptors(opts.projectId) + .command(`list [projectId]`, `Lists metric descriptors.`, {}, opts => + listMetricDescriptors(opts.projectId) ) - .command( - `get [projectId]`, - `Get a metric descriptor.`, - {}, - (opts) => getMetricDescriptor(opts.projectId, opts.metricId) + .command(`get [projectId]`, `Get a metric descriptor.`, {}, opts => + getMetricDescriptor(opts.projectId, opts.metricId) ) .command( `delete [projectId]`, `Deletes a custom metric descriptor.`, {}, - (opts) => deleteMetricDescriptor(opts.projectId, opts.metricId) + opts => deleteMetricDescriptor(opts.projectId, opts.metricId) ) .command( `write [projectId]`, `Writes example time series data to 'custom.googleapis.com/stores/daily_sales'.`, {}, - (opts) => writeTimeSeriesData(opts.projectId) + opts => writeTimeSeriesData(opts.projectId) ) .command( `read [projectId]`, `Reads time series data that matches the given filter.`, {}, - (opts) => readTimeSeriesData(opts.projectId, opts.filter) + opts => readTimeSeriesData(opts.projectId, opts.filter) ) .command( `read-fields [projectId]`, `Reads headers of time series data that matches 'compute.googleapis.com/instance/cpu/utilization'.`, {}, - (opts) => readTimeSeriesFields(opts.projectId) + opts => readTimeSeriesFields(opts.projectId) ) .command( `read-aggregate [projectId]`, `Aggregates time series data that matches 'compute.googleapis.com/instance/cpu/utilization'.`, {}, - (opts) => readTimeSeriesAggregate(opts.projectId) + opts => readTimeSeriesAggregate(opts.projectId) ) .command( `read-reduce [projectId]`, `Reduces time series data that matches 'compute.googleapis.com/instance/cpu/utilization'.`, {}, - (opts) => readTimeSeriesReduce(opts.projectId) + opts => readTimeSeriesReduce(opts.projectId) ) .command( `list-resources [projectId]`, `Lists monitored resource descriptors.`, {}, - (opts) => listMonitoredResourceDescriptors(opts.projectId) + opts => listMonitoredResourceDescriptors(opts.projectId) ) .command( `get-resource [projectId]`, `Get a monitored resource descriptor.`, {}, - (opts) => getMonitoredResourceDescriptor(opts.projectId, opts.resourceType) + opts => getMonitoredResourceDescriptor(opts.projectId, opts.resourceType) ) .options({ projectId: { @@ -588,8 +593,8 @@ const cli = require(`yargs`) default: process.env.GCLOUD_PROJECT, global: true, requiresArg: true, - type: 'string' - } + type: 'string', + }, }) .example(`node $0 create`) .example(`node $0 list`) @@ -598,13 +603,17 @@ const cli = require(`yargs`) .example(`node $0 list-resources`) .example(`node $0 get-resource cloudsql_database`) .example(`node $0 write`) - .example(`node $0 read 'metric.type="compute.googleapis.com/instance/cpu/utilization"'`) + .example( + `node $0 read 'metric.type="compute.googleapis.com/instance/cpu/utilization"'` + ) .example(`node $0 read-fields`) .example(`node $0 read-aggregate`) .example(`node $0 read-reduce`) .wrap(120) .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/monitoring/docs`); + .epilogue( + `For more information, see https://cloud.google.com/monitoring/docs` + ); if (module === require.main) { cli.help().strict().argv; // eslint-disable-line diff --git a/monitoring/snippets/package.json b/monitoring/snippets/package.json index f1278f12b5..3d503b18cf 100644 --- a/monitoring/snippets/package.json +++ b/monitoring/snippets/package.json @@ -4,16 +4,11 @@ "private": true, "license": "Apache-2.0", "author": "Google Inc.", - "repository": { - "type": "git", - "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" - }, + "repository": "googleapis/nodejs-monitoring", "engines": { "node": ">=4.3.2" }, "scripts": { - "lint": "repo-tools lint", - "pretest": "npm run lint", "test": "repo-tools test run --cmd ava -- -T 3m --verbose system-test/*.test.js" }, "dependencies": { @@ -21,30 +16,9 @@ "yargs": "9.0.1" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "2.0.9", + "@google-cloud/nodejs-repo-tools": "2.0.10", "ava": "0.22.0", "proxyquire": "1.8.0", "sinon": "4.0.1" - }, - "cloud-repo-tools": { - "requiresKeyFile": true, - "requiresProjectId": true, - "product": "monitoring", - "samples": [ - { - "id": "metrics", - "name": "Metrics", - "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" - } - ] } } diff --git a/monitoring/snippets/quickstart.js b/monitoring/snippets/quickstart.js index ca66561fbb..31c0e6a9bc 100644 --- a/monitoring/snippets/quickstart.js +++ b/monitoring/snippets/quickstart.js @@ -17,25 +17,25 @@ // [START monitoring_quickstart] // Imports the Google Cloud client library -const Monitoring = require('@google-cloud/monitoring'); +const monitoring = require('@google-cloud/monitoring'); // Your Google Cloud Platform project ID const projectId = 'YOUR_PROJECT_ID'; // Creates a client -const client = Monitoring.v3.metric(); +const client = new monitoring.MetricServiceClient(); // Prepares an individual data point const dataPoint = { interval: { endTime: { - seconds: Date.now() / 1000 - } + seconds: Date.now() / 1000, + }, }, value: { // The amount of sales - doubleValue: 123.45 - } + doubleValue: 123.45, + }, }; // Prepares the time series request @@ -47,28 +47,27 @@ const request = { metric: { type: 'custom.googleapis.com/stores/daily_sales', labels: { - store_id: 'Pittsburgh' - } + store_id: 'Pittsburgh', + }, }, resource: { type: 'global', labels: { - project_id: projectId - } + project_id: projectId, + }, }, - points: [ - dataPoint - ] - } - ] + points: [dataPoint], + }, + ], }; // Writes time series data -client.createTimeSeries(request) - .then((results) => { - console.log(`Done writing time series data.`); +client + .createTimeSeries(request) + .then(results => { + console.log(`Done writing time series data.`, results[0]); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_quickstart] diff --git a/monitoring/snippets/system-test/.eslintrc.yml b/monitoring/snippets/system-test/.eslintrc.yml new file mode 100644 index 0000000000..c0289282a6 --- /dev/null +++ b/monitoring/snippets/system-test/.eslintrc.yml @@ -0,0 +1,5 @@ +--- +rules: + node/no-unpublished-require: off + node/no-unsupported-features: off + no-empty: off diff --git a/monitoring/snippets/system-test/metrics.test.js b/monitoring/snippets/system-test/metrics.test.js index 614f1bcef6..e9e87d70e0 100644 --- a/monitoring/snippets/system-test/metrics.test.js +++ b/monitoring/snippets/system-test/metrics.test.js @@ -15,7 +15,8 @@ 'use strict'; -const client = require(`@google-cloud/monitoring`).metric(); +const monitoring = require(`@google-cloud/monitoring`); +const client = new monitoring.MetricServiceClient(); const path = require(`path`); const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); @@ -30,27 +31,30 @@ const resourceId = `cloudsql_database`; test.before(tools.checkCredentials); -test.serial(`should create a metric descriptors`, async (t) => { +test.serial(`should create a metric descriptors`, async t => { const output = await tools.runAsync(`${cmd} create`, cwd); t.true(output.includes(`Created custom Metric`)); t.true(output.includes(`Type: ${customMetricId}`)); }); -test.serial(`should list metric descriptors, including the new custom one`, async (t) => { - t.plan(0); - const attempt = tools.tryTest(async (assert) => { - const output = await tools.runAsync(`${cmd} list`, cwd); - assert(output.includes(customMetricId)); - assert(output.includes(computeMetricId)); - }); - attempt.tries(30); - attempt.timeout(120000); - await attempt.start(); -}); +test.serial( + `should list metric descriptors, including the new custom one`, + async t => { + t.plan(0); + const attempt = tools.tryTest(async assert => { + const output = await tools.runAsync(`${cmd} list`, cwd); + assert(output.includes(customMetricId)); + assert(output.includes(computeMetricId)); + }); + attempt.tries(30); + attempt.timeout(120000); + await attempt.start(); + } +); -test.serial(`should get a metric descriptor`, async (t) => { +test.serial(`should get a metric descriptor`, async t => { t.plan(0); - const attempt = tools.tryTest(async (assert) => { + const attempt = tools.tryTest(async assert => { const output = await tools.runAsync(`${cmd} get ${customMetricId}`, cwd); assert(output.includes(`Type: ${customMetricId}`)); }); @@ -59,124 +63,128 @@ test.serial(`should get a metric descriptor`, async (t) => { await attempt.start(); }); -test.serial(`should write time series data`, async (t) => { +test.serial(`should write time series data`, async t => { const output = await tools.runAsync(`${cmd} write`, cwd); t.true(output.includes(`Done writing time series data.`)); }); -test.serial(`should delete a metric descriptor`, async (t) => { +test.serial(`should delete a metric descriptor`, async t => { const output = await tools.runAsync(`${cmd} delete ${customMetricId}`, cwd); t.true(output.includes(`Deleted ${customMetricId}`)); }); -test(`should list monitored resource descriptors`, async (t) => { +test(`should list monitored resource descriptors`, async t => { const output = await tools.runAsync(`${cmd} list-resources`, cwd); - t.true(output.includes(`projects/${projectId}/monitoredResourceDescriptors/${resourceId}`)); + t.true( + output.includes( + `projects/${projectId}/monitoredResourceDescriptors/${resourceId}` + ) + ); }); -test(`should get a monitored resource descriptor`, async (t) => { +test(`should get a monitored resource descriptor`, async t => { const output = await tools.runAsync(`${cmd} get-resource ${resourceId}`, cwd); t.true(output.includes(`Type: ${resourceId}`)); }); -test(`should read time series data`, async (t) => { +test(`should read time series data`, async t => { const [timeSeries] = await client.listTimeSeries({ name: client.projectPath(projectId), filter: filter, interval: { startTime: { // Limit results to the last 20 minutes - seconds: (Date.now() / 1000) - (60 * 20) + seconds: Date.now() / 1000 - 60 * 20, }, endTime: { - seconds: Date.now() / 1000 - } - } + seconds: Date.now() / 1000, + }, + }, }); const output = await tools.runAsync(`${cmd} read '${filter}'`, cwd); - timeSeries.forEach((data) => { + timeSeries.forEach(data => { t.true(output.includes(`${data.metric.labels.instance_name}:`)); - data.points.forEach((point) => { + data.points.forEach(point => { t.true(output.includes(JSON.stringify(point.value))); }); }); }); -test(`should read time series data fields`, async (t) => { +test(`should read time series data fields`, async t => { const [timeSeries] = await client.listTimeSeries({ name: client.projectPath(projectId), filter: filter, interval: { startTime: { // Limit results to the last 20 minutes - seconds: (Date.now() / 1000) - (60 * 20) + seconds: Date.now() / 1000 - 60 * 20, }, endTime: { - seconds: Date.now() / 1000 - } + seconds: Date.now() / 1000, + }, }, // Don't return time series data, instead just return information about // the metrics that match the filter - view: `HEADERS` + view: `HEADERS`, }); const output = await tools.runAsync(`${cmd} read-fields`, cwd); t.true(output.includes(`Found data points for the following instances:`)); - timeSeries.forEach((data) => { + timeSeries.forEach(data => { t.true(output.includes(data.metric.labels.instance_name)); }); }); -test(`should read time series data aggregated`, async (t) => { +test(`should read time series data aggregated`, async t => { const [timeSeries] = await client.listTimeSeries({ name: client.projectPath(projectId), filter: filter, interval: { startTime: { // Limit results to the last 20 minutes - seconds: (Date.now() / 1000) - (60 * 20) + seconds: Date.now() / 1000 - 60 * 20, }, endTime: { - seconds: Date.now() / 1000 - } + seconds: Date.now() / 1000, + }, }, // Aggregate results per matching instance aggregation: { alignmentPeriod: { - seconds: 600 + seconds: 600, }, - perSeriesAligner: `ALIGN_MEAN` - } + perSeriesAligner: `ALIGN_MEAN`, + }, }); const output = await tools.runAsync(`${cmd} read-aggregate`, cwd); - t.true(output.includes(`CPU utilization:`)); - timeSeries.forEach((data) => { - t.true(output.includes(data.metric.labels.instance_name)); - t.true(output.includes(` Now: ${data.points[0].value.doubleValue}`)); - t.true(output.includes(` 10 min ago: ${data.points[1].value.doubleValue}`)); + t.regex(output, /CPU utilization:/); + timeSeries.forEach(data => { + t.regex(output, new RegExp(data.metric.labels.instance_name)); + t.regex(output, / Now: 0\.\d+/); + t.regex(output, / 10 min ago: 0\.\d+/); }); }); -test(`should read time series data reduced`, async (t) => { +test(`should read time series data reduced`, async t => { await client.listTimeSeries({ name: client.projectPath(projectId), filter: filter, interval: { startTime: { // Limit results to the last 20 minutes - seconds: (Date.now() / 1000) - (60 * 20) + seconds: Date.now() / 1000 - 60 * 20, }, endTime: { - seconds: Date.now() / 1000 - } + seconds: Date.now() / 1000, + }, }, // Aggregate results per matching instance aggregation: { alignmentPeriod: { - seconds: 600 + seconds: 600, }, crossSeriesReducer: `REDUCE_MEAN`, - perSeriesAligner: `ALIGN_MEAN` - } + perSeriesAligner: `ALIGN_MEAN`, + }, }); const output = await tools.runAsync(`${cmd} read-reduce`, cwd); t.true(output.includes(`Average CPU utilization across all GCE instances:`)); diff --git a/monitoring/snippets/system-test/quickstart.test.js b/monitoring/snippets/system-test/quickstart.test.js index 47502637d1..990318706d 100644 --- a/monitoring/snippets/system-test/quickstart.test.js +++ b/monitoring/snippets/system-test/quickstart.test.js @@ -20,40 +20,42 @@ const sinon = require(`sinon`); const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); -const client = proxyquire(`@google-cloud/monitoring`, {}).metric(); +const monitoring = proxyquire(`@google-cloud/monitoring`, {}); +const client = new monitoring.MetricServiceClient(); test.beforeEach(tools.stubConsole); test.afterEach.always(tools.restoreConsole); -test.cb(`should list time series`, (t) => { +test.cb(`should list time series`, t => { const clientMock = { - projectPath: (projectId) => client.projectPath(projectId), - createTimeSeries: (_request) => { + projectPath: projectId => client.projectPath(projectId), + createTimeSeries: _request => { _request.name = client.projectPath(process.env.GCLOUD_PROJECT); - _request.timeSeries[0].resource.labels.project_id = process.env.GCLOUD_PROJECT; - - return client.createTimeSeries(_request) - .then((result) => { - setTimeout(() => { - try { - t.is(console.log.callCount, 1); - t.deepEqual(console.log.getCall(0).args, [`Done writing time series data.`]); - t.end(); - } catch (err) { - t.end(err); - } - }, 200); - - return result; - }); - } + _request.timeSeries[0].resource.labels.project_id = + process.env.GCLOUD_PROJECT; + + return client.createTimeSeries(_request).then(result => { + setTimeout(() => { + try { + t.is(console.log.callCount, 1); + t.deepEqual(console.log.getCall(0).args, [ + `Done writing time series data.`, + {}, + ]); + t.end(); + } catch (err) { + t.end(err); + } + }, 200); + + return result; + }); + }, }; proxyquire(`../quickstart`, { '@google-cloud/monitoring': { - v3: { - metric: sinon.stub().returns(clientMock) - } - } + MetricServiceClient: sinon.stub().returns(clientMock), + }, }); }); diff --git a/monitoring/snippets/system-test/uptime.test.js b/monitoring/snippets/system-test/uptime.test.js index 35f7f8001a..d41e1090f3 100644 --- a/monitoring/snippets/system-test/uptime.test.js +++ b/monitoring/snippets/system-test/uptime.test.js @@ -15,7 +15,6 @@ '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`); @@ -27,43 +26,63 @@ const instanceId = 'uptime-test-' + Date.now(); test.before(tools.checkCredentials); -test(`should get an uptime check`, async (t) => { +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); +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/(.+)`)); + 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, /"type":"gce_instance"/); + t.regex(output, new RegExp(`"labels":{"instance_id":"${instanceId}"}`)); t.regex(output, /Display Name: My GCE Instance Uptime Check/); }); -test.serial(`should get an uptime check`, async (t) => { +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}"}}`)); + t.regex( + output, + new RegExp(`Retrieving projects/${projectId}/uptimeCheckConfigs/${id}`) + ); + t.regex(output, /"type":"gce_instance"/); + t.regex(output, new RegExp(`"labels":{"instance_id":"${instanceId}"}`)); }); -test.serial(`should list uptime checks`, async (t) => { +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(); + await tools + .tryTest(async assert => { + const results = await tools.runAsyncWithIO(`${cmd} list`, cwd); + const output = results.stdout + results.stderr; + assert(/"type":"gce_instance"/.test(output)); + assert( + new RegExp(`"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) => { +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.`)); + t.regex( + output, + new RegExp(`Deleting projects/${projectId}/uptimeCheckConfigs/${id}`) + ); + t.regex( + output, + new RegExp(`projects/${projectId}/uptimeCheckConfigs/${id} deleted.`) + ); }); - diff --git a/monitoring/snippets/uptime.js b/monitoring/snippets/uptime.js index e8a687e2c8..0b51f2ffa0 100644 --- a/monitoring/snippets/uptime.js +++ b/monitoring/snippets/uptime.js @@ -23,13 +23,13 @@ 'use strict'; -function createUptimeCheckConfig (projectId, gceInstanceId) { +function createUptimeCheckConfig(projectId, gceInstanceId) { // [START monitoring_uptime_check_create] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.uptimeCheck(); + const client = new monitoring.UptimeCheckServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -42,46 +42,54 @@ function createUptimeCheckConfig (projectId, gceInstanceId) { parent: client.projectPath(projectId), uptimeCheckConfig: { displayName: 'My GCE Instance Uptime Check', - resource: { + monitoredResource: { // See the Uptime Check docs for supported MonitoredResource types type: 'gce_instance', - labels: { instance_id: gceInstanceId } + labels: {instance_id: gceInstanceId}, }, - httpCheck: { path: '/', port: 80 }, - timeout: { seconds: 10 }, - period: { seconds: 300 } - } + httpCheck: {path: '/', port: 80}, + timeout: {seconds: 10}, + period: {seconds: 300}, + }, }; // Creates an uptime check config for a GCE instance - client.createUptimeCheckConfig(request) - .then((results) => { + client + .createUptimeCheckConfig(request) + .then(results => { const uptimeCheckConfig = results[0]; console.log('Uptime check created:'); console.log(`ID: ${uptimeCheckConfig.name}`); console.log(`Display Name: ${uptimeCheckConfig.displayName}`); - console.log(`Resource: %j`, uptimeCheckConfig.resource); + console.log(`Resource: %j`, uptimeCheckConfig.monitoredResource); console.log(`Period: %j`, uptimeCheckConfig.period); console.log(`Timeout: %j`, uptimeCheckConfig.timeout); console.log(`Check type: ${uptimeCheckConfig.check_request_type}`); - console.log(`Check: %j`, uptimeCheckConfig.httpCheck || uptimeCheckConfig.tcpCheck); - console.log(`Content matchers: ${uptimeCheckConfig.contentMatchers.map((matcher) => matcher.content).join(', ')}`); + console.log( + `Check: %j`, + uptimeCheckConfig.httpCheck || uptimeCheckConfig.tcpCheck + ); + console.log( + `Content matchers: ${uptimeCheckConfig.contentMatchers + .map(matcher => matcher.content) + .join(', ')}` + ); console.log(`Regions: ${uptimeCheckConfig.selectedRegions.join(', ')}`); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_uptime_check_create] } -function listUptimeCheckConfigs (projectId) { +function listUptimeCheckConfigs(projectId) { // [START monitoring_uptime_check_list_configs] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.uptimeCheck(); + const client = new monitoring.UptimeCheckServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -89,61 +97,76 @@ function listUptimeCheckConfigs (projectId) { // const projectId = 'YOUR_PROJECT_ID'; const request = { - parent: client.projectPath(projectId) + parent: client.projectPath(projectId), }; // Retrieves an uptime check config - client.listUptimeCheckConfigs(request) - .then((results) => { + client + .listUptimeCheckConfigs(request) + .then(results => { const uptimeCheckConfigs = results[0]; - uptimeCheckConfigs.forEach((uptimeCheckConfig) => { + uptimeCheckConfigs.forEach(uptimeCheckConfig => { console.log(`ID: ${uptimeCheckConfig.name}`); console.log(` Display Name: ${uptimeCheckConfig.displayName}`); - console.log(` Resource: %j`, uptimeCheckConfig.resource); + console.log(` Resource: %j`, uptimeCheckConfig.monitoredResource); console.log(` Period: %j`, uptimeCheckConfig.period); console.log(` Timeout: %j`, uptimeCheckConfig.timeout); console.log(` Check type: ${uptimeCheckConfig.check_request_type}`); - console.log(` Check: %j`, uptimeCheckConfig.httpCheck || uptimeCheckConfig.tcpCheck); - console.log(` Content matchers: ${uptimeCheckConfig.contentMatchers.map((matcher) => matcher.content).join(', ')}`); - console.log(` Regions: ${uptimeCheckConfig.selectedRegions.join(', ')}`); + console.log( + ` Check: %j`, + uptimeCheckConfig.httpCheck || uptimeCheckConfig.tcpCheck + ); + console.log( + ` Content matchers: ${uptimeCheckConfig.contentMatchers + .map(matcher => matcher.content) + .join(', ')}` + ); + console.log( + ` Regions: ${uptimeCheckConfig.selectedRegions.join(', ')}` + ); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_uptime_check_list_configs] } -function listUptimeCheckIps () { +function listUptimeCheckIps() { // [START monitoring_uptime_check_list_ips] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.uptimeCheck(); + const client = new monitoring.UptimeCheckServiceClient(); // List uptime check IPs - client.listUptimeCheckIps() - .then((results) => { + client + .listUptimeCheckIps() + .then(results => { const uptimeCheckIps = results[0]; - uptimeCheckIps.forEach((uptimeCheckIp) => { - console.log(uptimeCheckIp.region, uptimeCheckIp.location, uptimeCheckIp.ipAddress); + uptimeCheckIps.forEach(uptimeCheckIp => { + console.log( + uptimeCheckIp.region, + uptimeCheckIp.location, + uptimeCheckIp.ipAddress + ); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_uptime_check_list_ips] } -function getUptimeCheckConfig (projectId, uptimeCheckConfigId) { +function getUptimeCheckConfig(projectId, uptimeCheckConfigId) { // [START monitoring_uptime_check_get] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.uptimeCheck(); + const client = new monitoring.UptimeCheckServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -153,39 +176,47 @@ function getUptimeCheckConfig (projectId, uptimeCheckConfigId) { const request = { // i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check - name: client.uptimeCheckConfigPath(projectId, uptimeCheckConfigId) + name: client.uptimeCheckConfigPath(projectId, uptimeCheckConfigId), }; console.log(`Retrieving ${request.name}`); // Retrieves an uptime check config - client.getUptimeCheckConfig(request) - .then((results) => { + client + .getUptimeCheckConfig(request) + .then(results => { const uptimeCheckConfig = results[0]; console.log(`ID: ${uptimeCheckConfig.name}`); console.log(`Display Name: ${uptimeCheckConfig.displayName}`); - console.log(`Resource: %j`, uptimeCheckConfig.resource); + console.log(`Resource: %j`, uptimeCheckConfig.monitoredResource); console.log(`Period: %j`, uptimeCheckConfig.period); console.log(`Timeout: %j`, uptimeCheckConfig.timeout); console.log(`Check type: ${uptimeCheckConfig.check_request_type}`); - console.log(`Check: %j`, uptimeCheckConfig.httpCheck || uptimeCheckConfig.tcpCheck); - console.log(`Content matchers: ${uptimeCheckConfig.contentMatchers.map((matcher) => matcher.content).join(', ')}`); + console.log( + `Check: %j`, + uptimeCheckConfig.httpCheck || uptimeCheckConfig.tcpCheck + ); + console.log( + `Content matchers: ${uptimeCheckConfig.contentMatchers + .map(matcher => matcher.content) + .join(', ')}` + ); console.log(`Regions: ${uptimeCheckConfig.selectedRegions.join(', ')}`); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_uptime_check_get] } -function deleteUptimeCheckConfig (projectId, uptimeCheckConfigId) { +function deleteUptimeCheckConfig(projectId, uptimeCheckConfigId) { // [START monitoring_uptime_check_delete] // Imports the Google Cloud client library - const Monitoring = require('@google-cloud/monitoring'); + const monitoring = require('@google-cloud/monitoring'); // Creates a client - const client = Monitoring.uptimeCheck(); + const client = new monitoring.UptimeCheckServiceClient(); /** * TODO(developer): Uncomment and edit the following lines of code. @@ -195,17 +226,18 @@ function deleteUptimeCheckConfig (projectId, uptimeCheckConfigId) { const request = { // i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check - name: client.uptimeCheckConfigPath(projectId, uptimeCheckConfigId) + name: client.uptimeCheckConfigPath(projectId, uptimeCheckConfigId), }; console.log(`Deleting ${request.name}`); // Delete an uptime check config - client.deleteUptimeCheckConfig(request) + client + .deleteUptimeCheckConfig(request) .then(() => { console.log(`${request.name} deleted.`); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END monitoring_uptime_check_delete] @@ -217,31 +249,25 @@ require(`yargs`) `create [projectId]`, `Creates an uptime check config.`, {}, - (opts) => createUptimeCheckConfig(opts.projectId, '' + opts.gceInstanceId) + opts => createUptimeCheckConfig(opts.projectId, '' + opts.gceInstanceId) ) - .command( - `list [projectId]`, - `Lists uptime check configs.`, - {}, - (opts) => listUptimeCheckConfigs(opts.projectId) + .command(`list [projectId]`, `Lists uptime check configs.`, {}, opts => + listUptimeCheckConfigs(opts.projectId) ) - .command( - `list-ips`, - `Lists uptime check config IPs.`, - {}, - (opts) => listUptimeCheckIps() + .command(`list-ips`, `Lists uptime check config IPs.`, {}, () => + listUptimeCheckIps() ) .command( `get [projectId]`, `Gets an uptime check config.`, {}, - (opts) => getUptimeCheckConfig(opts.projectId, opts.uptimeCheckConfigId) + opts => getUptimeCheckConfig(opts.projectId, opts.uptimeCheckConfigId) ) .command( `delete [projectId]`, `Deletes an uptime check config.`, {}, - (opts) => deleteUptimeCheckConfig(opts.projectId, opts.uptimeCheckConfigId) + opts => deleteUptimeCheckConfig(opts.projectId, opts.uptimeCheckConfigId) ) .options({ projectId: { @@ -249,18 +275,25 @@ require(`yargs`) default: process.env.GCLOUD_PROJECT, global: true, requiresArg: true, - type: 'string' - } + type: 'string', + }, }) - .example(`node $0 create my-instance`, 'Create an uptime check for a "my-instance" GCE instance.') + .example( + `node $0 create my-instance`, + 'Create an uptime check for a "my-instance" GCE instance.' + ) .example(`node $0 list`, 'List all uptime check configs.') - .example(`node $0 list "resource.type = gce_instance AND resource.label.instance_id = mongodb"`, 'List all uptime check configs for a specific GCE instance.') + .example( + `node $0 list "resource.type = gce_instance AND resource.label.instance_id = mongodb"`, + 'List all uptime check configs for a specific GCE instance.' + ) .example(`node $0 list-ips`) .example(`node $0 get My-Uptime-Check`) .example(`node $0 delete My-Uptime-Check`) .wrap(120) .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/monitoring/uptime-checks/`) + .epilogue( + `For more information, see https://cloud.google.com/monitoring/uptime-checks/` + ) .help() - .strict() - .argv; + .strict().argv;