Skip to content

Commit fb6b5a0

Browse files
authored
Merge pull request #2868 from GoogleCloudPlatform/nodejs-monitoring-migration-2
Adds quickstart samples referenced in DevSite
2 parents 47773ed + 94844ad commit fb6b5a0

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

monitoring/snippets/quickstart.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2017 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(projectId) {
18+
// [START monitoring_quickstart]
19+
// Imports the Google Cloud client library
20+
const monitoring = require('@google-cloud/monitoring');
21+
22+
async function quickstart() {
23+
// Creates a client
24+
const client = new monitoring.MetricServiceClient();
25+
26+
// TODO(developer): Uncomment and set the following variables
27+
// const projectId = "PROJECT_ID"
28+
29+
// Prepares an individual data point
30+
const dataPoint = {
31+
interval: {
32+
endTime: {
33+
seconds: Date.now() / 1000,
34+
},
35+
},
36+
value: {
37+
// The amount of sales
38+
doubleValue: 123.45,
39+
},
40+
};
41+
42+
// Prepares the time series request
43+
const request = {
44+
name: client.projectPath(projectId),
45+
timeSeries: [
46+
{
47+
// Ties the data point to a custom metric
48+
metric: {
49+
type: 'custom.googleapis.com/stores/daily_sales',
50+
labels: {
51+
store_id: 'Pittsburgh',
52+
},
53+
},
54+
resource: {
55+
type: 'global',
56+
labels: {
57+
project_id: projectId,
58+
},
59+
},
60+
points: [dataPoint],
61+
},
62+
],
63+
};
64+
65+
// Writes time series data
66+
const [result] = await client.createTimeSeries(request);
67+
console.log('Done writing time series data.', result);
68+
}
69+
quickstart();
70+
// [END monitoring_quickstart]
71+
}
72+
73+
process.on('unhandledRejection', err => {
74+
console.error(err.message);
75+
process.exitCode = 1;
76+
});
77+
main(...process.argv.slice(2));
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2017 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it, before} = require('mocha');
19+
const cp = require('child_process');
20+
const monitoring = require('@google-cloud/monitoring');
21+
22+
const client = new monitoring.AlertPolicyServiceClient();
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
// Tests in this suite can trigger the upstream error,
26+
// "points were written more frequently than the maximum
27+
// sampling period configured for the metric":
28+
const delay = async test => {
29+
const retries = test.currentRetry();
30+
if (retries === 0) return; // no retry on the first failure.
31+
// see: https://cloud.google.com/storage/docs/exponential-backoff:
32+
const ms = Math.pow(2, retries) * 250 + Math.random() * 1000;
33+
return new Promise(done => {
34+
console.info(`retrying "${test.title}" in ${ms}ms`);
35+
setTimeout(done, ms);
36+
});
37+
};
38+
describe('quickstart', () => {
39+
let projectId;
40+
before(async () => {
41+
projectId = await client.getProjectId();
42+
});
43+
it('should run the quickstart', async function () {
44+
this.retries(8);
45+
await delay(this.test); // delay the start of the test, if this is a retry.
46+
const result = execSync(`node quickstart.js ${projectId}`);
47+
assert.match(result, /Done writing time series data/);
48+
});
49+
});

0 commit comments

Comments
 (0)