Skip to content

Commit

Permalink
chore: split metrics samples (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and Ace Nassri committed Nov 10, 2022
1 parent d2af0cc commit 2fa61f0
Show file tree
Hide file tree
Showing 14 changed files with 975 additions and 569 deletions.
206 changes: 201 additions & 5 deletions monitoring/snippets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ and a variety of common application components including Cassandra, Nginx, Apach
* [Alerts.list Policies](#alerts.list-policies)
* [Alerts.replace Channels](#alerts.replace-channels)
* [Alerts.restore Policies](#alerts.restore-policies)
* [Metrics](#metrics)
* [Create Metric Descriptor](#create-metric-descriptor)
* [Delete Metric Descriptor](#delete-metric-descriptor)
* [Get Metric Descriptor](#get-metric-descriptor)
* [Get Monitored Resource Descriptor](#get-monitored-resource-descriptor)
* [List Metric Descriptors](#list-metric-descriptors)
* [List Monitored Resource Descriptors](#list-monitored-resource-descriptors)
* [Read Time Series Aggregate](#read-time-series-aggregate)
* [Read Time Series Data](#read-time-series-data)
* [Read Time Series Fields](#read-time-series-fields)
* [Read Time Series Reduce](#read-time-series-reduce)
* [Write Time Series Data](#write-time-series-data)
* [Quickstart](#quickstart)
* [Uptime](#uptime)

Expand Down Expand Up @@ -141,16 +151,202 @@ __Usage:__



### Metrics
### Create Metric Descriptor

View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.js).
Creates an example 'custom.googleapis.com/stores/daily_sales' custom metric descriptor.

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.js,samples/README.md)
View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.createDescriptor.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.createDescriptor.js,samples/README.md)

__Usage:__


`node metrics.createDescriptor.js your-project-id`


-----




### Delete Metric Descriptor

Deletes a custom metric descriptor.

View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.deleteDescriptor.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.deleteDescriptor.js,samples/README.md)

__Usage:__


`node metrics.deleteDescriptor.js your-project-id custom.googleapis.com/stores/daily_sales`


-----




### Get Metric Descriptor

Gets a custom metric descriptor

View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.getDescriptor.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.getDescriptor.js,samples/README.md)

__Usage:__


`node metrics.getDescriptor.js your-project-id custom.googleapis.com/your/id`


-----




### Get Monitored Resource Descriptor

View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.getMonitoredResourceDescriptor.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.getMonitoredResourceDescriptor.js,samples/README.md)

__Usage:__


`node metrics.getMonitoredResourceDescriptor.js your-project-id some-resource-type`


-----




### List Metric Descriptors

View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.listDescriptors.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.listDescriptors.js,samples/README.md)

__Usage:__


`node metrics.listDescriptors.js your-project-id`


-----




### List Monitored Resource Descriptors

View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.listMonitoredResourceDescriptors.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.listMonitoredResourceDescriptors.js,samples/README.md)

__Usage:__


`node metrics.listMonitoredResourceDescriptors.js your-project-id`


-----




### Read Time Series Aggregate

Aggregates time series data that matches 'compute.googleapis.com/instance/cpu/utilization'.

View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.readTimeSeriesAggregate.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.readTimeSeriesAggregate.js,samples/README.md)

__Usage:__


`node metrics.readTimeSeriesAggregate.js your-project-id`


-----




### Read Time Series Data

Reads time series data that matches the given filter.

View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.readTimeSeriesData.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.readTimeSeriesData.js,samples/README.md)

__Usage:__


`node metrics.readTimeSeriesData.js your-project-id 'metric.type="compute.googleapis.com/instance/cpu/utilization"'`


-----




### Read Time Series Fields

Reads headers of time series data that matches 'compute.googleapis.com/instance/cpu/utilization'.

View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.readTimeSeriesFields.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.readTimeSeriesFields.js,samples/README.md)

__Usage:__


`node metrics.readTimeSeriesFields.js your-project-id`


-----




### Read Time Series Reduce

Reduces time series data that matches 'compute.googleapis.com/instance/cpu/utilization'.

View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.readTimeSeriesReduce.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.readTimeSeriesReduce.js,samples/README.md)

__Usage:__


`node metrics.readTimeSeriesReduce.js your-project-id`


-----




### Write Time Series Data

Writes example time series data to 'custom.googleapis.com/stores/daily_sales'.

View the [source code](https://github.com/googleapis/nodejs-monitoring/blob/master/samples/metrics.writeTimeSeriesData.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-monitoring&page=editor&open_in_editor=samples/metrics.writeTimeSeriesData.js,samples/README.md)

__Usage:__


`node samples/metrics.js`
`node metrics.writeTimeSeriesData.js your-project-id`


-----
Expand Down
76 changes: 76 additions & 0 deletions monitoring/snippets/metrics.createDescriptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2017 Google LLC
//
// 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';

// sample-metadata:
// title: Create Metric Descriptor
// description: Creates an example 'custom.googleapis.com/stores/daily_sales' custom metric descriptor.
// usage: node metrics.createDescriptor.js your-project-id

function main(projectId) {
// [START monitoring_create_metric]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');

// Creates a client
const client = new monitoring.MetricServiceClient();

/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
async function createMetricDescriptor() {
const request = {
name: client.projectPath(projectId),
metricDescriptor: {
description: 'Daily sales records from all branch stores.',
displayName: 'Daily Sales',
type: 'custom.googleapis.com/stores/daily_sales',
metricKind: 'GAUGE',
valueType: 'DOUBLE',
unit: '{USD}',
labels: [
{
key: 'store_id',
valueType: 'STRING',
description: 'The ID of the store.',
},
],
},
};

// Creates a custom metric descriptor
const [descriptor] = await client.createMetricDescriptor(request);
console.log('Created custom Metric:\n');
console.log(`Name: ${descriptor.displayName}`);
console.log(`Description: ${descriptor.description}`);
console.log(`Type: ${descriptor.type}`);
console.log(`Kind: ${descriptor.metricKind}`);
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}`);
});
}
createMetricDescriptor();
// [END monitoring_create_metric]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
53 changes: 53 additions & 0 deletions monitoring/snippets/metrics.deleteDescriptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2017 Google LLC
//
// 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';

// sample-metadata:
// title: Delete Metric Descriptor
// description: Deletes a custom metric descriptor.
// usage: node metrics.deleteDescriptor.js your-project-id custom.googleapis.com/stores/daily_sales

function main(projectId, metricId) {
// [START monitoring_delete_metric]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');

// Creates a client
const client = new monitoring.MetricServiceClient();

async function deleteMetricDescriptor() {
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const metricId = 'custom.googleapis.com/stores/daily_sales';

const request = {
name: client.projectMetricDescriptorPath(projectId, metricId),
};

// Deletes a metric descriptor
const [result] = await client.deleteMetricDescriptor(request);
console.log(`Deleted ${metricId}`, result);
}
deleteMetricDescriptor();
// [END monitoring_delete_metric]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
Loading

0 comments on commit 2fa61f0

Please sign in to comment.