Skip to content

Commit

Permalink
Add new Monitoring snippets. (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry authored Jan 25, 2017
1 parent d2f566a commit 74de567
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
51 changes: 51 additions & 0 deletions packages/google-cloud-monitoring/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <metricId> [projectId] Get a metric descriptor.
delete <metricId> [projectId] Deletes a custom metric descriptor.
write [projectId] Writes example time series data to
'custom.googleapis.com/stores/daily_sales'.
read <filter> [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 <resourceType> [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
Expand Down
6 changes: 4 additions & 2 deletions packages/google-cloud-monitoring/samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
71 changes: 71 additions & 0 deletions packages/google-cloud-monitoring/samples/quickstart.js
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 74de567

Please sign in to comment.