From 74de5674b96a1396acb7593f0a1f58af17f5bc2e Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Wed, 25 Jan 2017 13:56:39 -0800 Subject: [PATCH] Add new Monitoring snippets. (#301) --- .../google-cloud-monitoring/samples/README.md | 51 +++++++++++++ .../samples/package.json | 6 +- .../samples/quickstart.js | 71 +++++++++++++++++++ 3 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 packages/google-cloud-monitoring/samples/quickstart.js diff --git a/packages/google-cloud-monitoring/samples/README.md b/packages/google-cloud-monitoring/samples/README.md index 445f9ede860..aeebd85a647 100644 --- a/packages/google-cloud-monitoring/samples/README.md +++ b/packages/google-cloud-monitoring/samples/README.md @@ -28,6 +28,57 @@ including Cassandra, Nginx, Apache Web Server, Elasticsearch and many others. ## Samples +### Metrics + +View the [documentation][metrics_docs] or the [source code][metrics_code]. + +__Usage:__ `node metrics.js --help` + +``` +Commands: + create [projectId] Creates an example 'custom.googleapis.com/stores/daily_sales' custom metric + descriptor. + list [projectId] Lists metric descriptors. + get [projectId] Get a metric descriptor. + delete [projectId] Deletes a custom metric descriptor. + write [projectId] Writes example time series data to + 'custom.googleapis.com/stores/daily_sales'. + read [projectId] Reads time series data that matches the given filter. + read-fields [projectId] Reads headers of time series data that matches + 'compute.googleapis.com/instance/cpu/utilization'. + read-aggregate [projectId] Aggregates time series data that matches + 'compute.googleapis.com/instance/cpu/utilization'. + read-reduce [projectId] Reduces time series data that matches + 'compute.googleapis.com/instance/cpu/utilization'. + list-resources [projectId] Lists monitored resource descriptors. + get-resource [projectId] Get a monitored resource descriptor. + +Options: + --help Show help [boolean] + --projectId, -p [string] + +Examples: + node metrics.js create + node metrics.js list + node metrics.js get logging.googleapis.com/log_entry_count + node metrics.js delete + custom.googleapis.com/stores/daily_sales + node metrics.js list-resources + node metrics.js get-resource cloudsql_database + node metrics.js write + node metrics.js read + 'metric.type="compute.googleapis.com/instance/cpu/utilizatio + n"' + node metrics.js read-fields + node metrics.js read-aggregate + node metrics.js read-reduce + +For more information, see https://cloud.google.com/monitoring/docs +``` + +[metrics_docs]: https://cloud.google.com/monitoring/docs +[metrics_code]: metrics.js + ### List resources `list_resources.js` is a command-line program to demonstrate connecting to the Google diff --git a/packages/google-cloud-monitoring/samples/package.json b/packages/google-cloud-monitoring/samples/package.json index 66ce1a78459..15de3068d06 100644 --- a/packages/google-cloud-monitoring/samples/package.json +++ b/packages/google-cloud-monitoring/samples/package.json @@ -8,7 +8,9 @@ "test": "cd ..; npm run st -- --verbose monitoring/system-test/*.test.js" }, "dependencies": { - "async":"2.1.4", - "googleapis": "16.0.0" + "@google-cloud/monitoring": "0.1.4", + "async": "2.1.4", + "googleapis": "16.0.0", + "yargs": "6.6.0" } } diff --git a/packages/google-cloud-monitoring/samples/quickstart.js b/packages/google-cloud-monitoring/samples/quickstart.js new file mode 100644 index 00000000000..c743cc18560 --- /dev/null +++ b/packages/google-cloud-monitoring/samples/quickstart.js @@ -0,0 +1,71 @@ +/** + * 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'; + +// [START monitoring_quickstart] +// Imports the Google Cloud client library +const Monitoring = require('@google-cloud/monitoring'); + +// Your Google Cloud Platform project ID +const projectId = 'YOUR_PROJECT_ID'; + +// Instantiates a client +const client = Monitoring.v3().metricServiceClient(); + +// Prepares an individual data point +const dataPoint = { + interval: { + endTime: { + seconds: Date.now() / 1000 + } + }, + value: { + // The amount of sales + doubleValue: 123.45 + } +}; + +// Prepares the time series request +const request = { + name: client.projectPath(projectId), + timeSeries: [ + { + // Ties the data point to a custom metric + metric: { + type: 'custom.googleapis.com/stores/daily_sales', + labels: { + store_id: 'Pittsburgh' + } + }, + resource: { + type: 'global', + labels: { + project_id: projectId + } + }, + points: [ + dataPoint + ] + } + ] +}; + +// Writes time series data +client.createTimeSeries(request) + .then((results) => { + console.log(`Done writing time series data.`); + }); +// [END monitoring_quickstart]