Skip to content

Commit a70d9bf

Browse files
committed
Finish new Monitoring samples.
1 parent 21f0cab commit a70d9bf

File tree

6 files changed

+173
-35
lines changed

6 files changed

+173
-35
lines changed

monitoring/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,20 @@ Options:
5858
--projectId, -p [string]
5959
6060
Examples:
61+
node metrics.js create
6162
node metrics.js list
6263
node metrics.js get logging.googleapis.com/log_entry_count
64+
node metrics.js delete
65+
custom.googleapis.com/stores/daily_sales
6366
node metrics.js list-resources
6467
node metrics.js get-resource cloudsql_database
68+
node metrics.js write
69+
node metrics.js read
70+
'metric.type="compute.googleapis.com/instance/cpu/utilizatio
71+
n"'
72+
node metrics.js read-fields
73+
node metrics.js read-aggregate
74+
node metrics.js read-reduce
6575
6676
For more information, see https://cloud.google.com/monitoring/docs
6777
```

monitoring/metrics.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ function readTimeSeriesFields (projectId) {
295295

296296
console.log('Found data points for the following instances:');
297297
timeSeries.forEach((data) => {
298-
console.log(`${data.metric.labels.instance_name}`);
298+
console.log(data.metric.labels.instance_name);
299299
});
300300
});
301301
// [END monitoring_read_timeseries_fields]
@@ -340,7 +340,7 @@ function readTimeSeriesAggregate (projectId) {
340340

341341
console.log('CPU utilization:');
342342
timeSeries.forEach((data) => {
343-
console.log(`${data.metric.labels.instance_name}`);
343+
console.log(data.metric.labels.instance_name);
344344
console.log(` Now: ${data.points[0].value.doubleValue}`);
345345
console.log(` 10 min ago: ${data.points[1].value.doubleValue}`);
346346
});
@@ -530,10 +530,17 @@ const cli = require(`yargs`)
530530
type: 'string'
531531
}
532532
})
533+
.example(`node $0 create`)
533534
.example(`node $0 list`)
534535
.example(`node $0 get logging.googleapis.com/log_entry_count`)
536+
.example(`node $0 delete custom.googleapis.com/stores/daily_sales`)
535537
.example(`node $0 list-resources`)
536538
.example(`node $0 get-resource cloudsql_database`)
539+
.example(`node $0 write`)
540+
.example(`node $0 read 'metric.type="compute.googleapis.com/instance/cpu/utilization"'`)
541+
.example(`node $0 read-fields`)
542+
.example(`node $0 read-aggregate`)
543+
.example(`node $0 read-reduce`)
537544
.wrap(120)
538545
.recommendCommands()
539546
.epilogue(`For more information, see https://cloud.google.com/monitoring/docs`);

monitoring/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": "Apache Version 2.0",
66
"author": "Google Inc.",
77
"scripts": {
8-
"test": "cd ..; npm run st -- --verbose monitoring/system-test/metrics.test.js"
8+
"test": "cd ..; npm run st -- --verbose monitoring/system-test/*.test.js"
99
},
1010
"dependencies": {
1111
"@google-cloud/monitoring": "0.1.4",

monitoring/quickstart.js

+33-21
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,47 @@ const projectId = 'YOUR_PROJECT_ID';
2525
// Instantiates a client
2626
const client = Monitoring.v3().metricServiceClient();
2727

28-
const request = {
29-
name: client.projectPath(projectId),
30-
filter: 'metric.type="compute.googleapis.com/instance/cpu/utilization"',
28+
// Prepares an individual data point
29+
const dataPoint = {
3130
interval: {
32-
startTime: {
33-
// Limit results to the last 10 minutes
34-
seconds: (Date.now() / 1000) - (60 * 10)
35-
},
3631
endTime: {
3732
seconds: Date.now() / 1000
3833
}
34+
},
35+
value: {
36+
// The amount of sales
37+
doubleValue: 123.45
3938
}
4039
};
4140

41+
// Prepares the time series request
42+
const request = {
43+
name: client.projectPath(projectId),
44+
timeSeries: [
45+
{
46+
// Ties the data point to a custom metric
47+
metric: {
48+
type: 'custom.googleapis.com/stores/daily_sales',
49+
labels: {
50+
store_id: 'Pittsburgh'
51+
}
52+
},
53+
resource: {
54+
type: 'global',
55+
labels: {
56+
project_id: projectId
57+
}
58+
},
59+
points: [
60+
dataPoint
61+
]
62+
}
63+
]
64+
};
65+
4266
// Writes time series data
43-
client.listTimeSeries(request)
67+
client.createTimeSeries(request)
4468
.then((results) => {
45-
const timeSeries = results[0];
46-
47-
console.log('Found CPU utilization data for the following instances:');
48-
timeSeries.forEach((computeInstance) => {
49-
const instanceName = computeInstance.metric.labels.instance_name;
50-
console.log(` ${instanceName}:`);
51-
52-
computeInstance.points.forEach((point) => {
53-
const startTime = new Date(point.interval.startTime.seconds * 1000);
54-
const value = point.value.doubleValue;
55-
console.log(` ${startTime.toISOString()} - ${value}`);
56-
});
57-
});
69+
console.log(`Done writing time series data.`);
5870
});
5971
// [END monitoring_quickstart]

monitoring/system-test/metrics.test.js

+113
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
require(`../../system-test/_setup`);
1919

20+
const client = require(`@google-cloud/monitoring`).v3().metricServiceClient();
2021
const path = require(`path`);
2122

2223
const cmd = `node metrics.js`;
2324
const cwd = path.join(__dirname, `..`);
2425
const customMetricId = `custom.googleapis.com/stores/daily_sales`;
2526
const computeMetricId = `compute.googleapis.com/instance/cpu/utilization`;
27+
const filter = `metric.type="${computeMetricId}"`;
2628
const projectId = process.env.GCLOUD_PROJECT;
2729
const resourceId = `cloudsql_database`;
2830

@@ -50,6 +52,11 @@ test.serial(`should get a metric descriptor`, async (t) => {
5052
}).start();
5153
});
5254

55+
test.serial(`should write time series data`, async (t) => {
56+
const output = await runAsync(`${cmd} write`, cwd);
57+
t.true(output.includes(`Done writing time series data.`));
58+
});
59+
5360
test.serial(`should delete a metric descriptor`, async (t) => {
5461
const output = await runAsync(`${cmd} delete ${customMetricId}`, cwd);
5562
t.true(output.includes(`Deleted ${customMetricId}`));
@@ -64,3 +71,109 @@ test(`should get a monitored resource descriptor`, async (t) => {
6471
const output = await runAsync(`${cmd} get-resource ${resourceId}`, cwd);
6572
t.true(output.includes(`Type: ${resourceId}`));
6673
});
74+
75+
test(`should read time series data`, async (t) => {
76+
const [timeSeries] = await client.listTimeSeries({
77+
name: client.projectPath(projectId),
78+
filter: filter,
79+
interval: {
80+
startTime: {
81+
// Limit results to the last 20 minutes
82+
seconds: (Date.now() / 1000) - (60 * 20)
83+
},
84+
endTime: {
85+
seconds: Date.now() / 1000
86+
}
87+
}
88+
});
89+
const output = await runAsync(`${cmd} read '${filter}'`, cwd);
90+
timeSeries.forEach((data) => {
91+
t.true(output.includes(`${data.metric.labels.instance_name}:`));
92+
data.points.forEach((point) => {
93+
t.true(output.includes(JSON.stringify(point.value)));
94+
});
95+
});
96+
});
97+
98+
test(`should read time series data fields`, async (t) => {
99+
const [timeSeries] = await client.listTimeSeries({
100+
name: client.projectPath(projectId),
101+
filter: filter,
102+
interval: {
103+
startTime: {
104+
// Limit results to the last 20 minutes
105+
seconds: (Date.now() / 1000) - (60 * 20)
106+
},
107+
endTime: {
108+
seconds: Date.now() / 1000
109+
}
110+
},
111+
// Don't return time series data, instead just return information about
112+
// the metrics that match the filter
113+
view: `HEADERS`
114+
});
115+
const output = await runAsync(`${cmd} read-fields`, cwd);
116+
t.true(output.includes(`Found data points for the following instances:`));
117+
timeSeries.forEach((data) => {
118+
t.true(output.includes(data.metric.labels.instance_name));
119+
});
120+
});
121+
122+
test(`should read time series data aggregated`, async (t) => {
123+
const [timeSeries] = await client.listTimeSeries({
124+
name: client.projectPath(projectId),
125+
filter: filter,
126+
interval: {
127+
startTime: {
128+
// Limit results to the last 20 minutes
129+
seconds: (Date.now() / 1000) - (60 * 20)
130+
},
131+
endTime: {
132+
seconds: Date.now() / 1000
133+
}
134+
},
135+
// Aggregate results per matching instance
136+
aggregation: {
137+
alignmentPeriod: {
138+
seconds: 600
139+
},
140+
perSeriesAligner: `ALIGN_MEAN`
141+
}
142+
});
143+
const output = await runAsync(`${cmd} read-aggregate`, cwd);
144+
t.true(output.includes(`CPU utilization:`));
145+
timeSeries.forEach((data) => {
146+
t.true(output.includes(data.metric.labels.instance_name));
147+
t.true(output.includes(` Now: ${data.points[0].value.doubleValue}`));
148+
t.true(output.includes(` 10 min ago: ${data.points[1].value.doubleValue}`));
149+
});
150+
});
151+
152+
test(`should read time series data reduced`, async (t) => {
153+
const [timeSeries] = await client.listTimeSeries({
154+
name: client.projectPath(projectId),
155+
filter: filter,
156+
interval: {
157+
startTime: {
158+
// Limit results to the last 20 minutes
159+
seconds: (Date.now() / 1000) - (60 * 20)
160+
},
161+
endTime: {
162+
seconds: Date.now() / 1000
163+
}
164+
},
165+
// Aggregate results per matching instance
166+
aggregation: {
167+
alignmentPeriod: {
168+
seconds: 600
169+
},
170+
crossSeriesReducer: `REDUCE_MEAN`,
171+
perSeriesAligner: `ALIGN_MEAN`
172+
}
173+
});
174+
const reductions = timeSeries[0].points;
175+
const output = await runAsync(`${cmd} read-reduce`, cwd);
176+
t.true(output.includes(`Average CPU utilization across all GCE instances:`));
177+
t.true(output.includes(` Last 10 min: ${reductions[0].value.doubleValue}`));
178+
t.true(output.includes(` 10-20 min ago: ${reductions[0].value.doubleValue}`));
179+
});

monitoring/system-test/quickstart.test.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,23 @@ test.afterEach.always(restoreConsole);
2626
test.cb(`should list time series`, (t) => {
2727
const clientMock = {
2828
projectPath: (projectId) => client.projectPath(projectId),
29-
listTimeSeries: (_request) => {
29+
createTimeSeries: (_request) => {
3030
_request.name = client.projectPath(process.env.GCLOUD_PROJECT);
31+
_request.timeSeries[0].resource.labels.project_id = process.env.GCLOUD_PROJECT;
3132

32-
return client.listTimeSeries(_request)
33-
.then(([timeSeries]) => {
34-
t.true(Array.isArray(timeSeries));
35-
33+
return client.createTimeSeries(_request)
34+
.then((result) => {
3635
setTimeout(() => {
3736
try {
38-
let numCalls = 1 + timeSeries.length;
39-
timeSeries.forEach((computeInstance) => {
40-
numCalls += computeInstance.points.length;
41-
});
42-
t.is(console.log.callCount, numCalls);
37+
t.is(console.log.callCount, 1);
38+
t.deepEqual(console.log.getCall(0).args, [`Done writing time series data.`]);
4339
t.end();
4440
} catch (err) {
4541
t.end(err);
4642
}
4743
}, 200);
4844

49-
return [timeSeries];
45+
return result;
5046
});
5147
}
5248
};

0 commit comments

Comments
 (0)