From 6b900bedf59c096da19acacaffaeaebb42cdac28 Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Mon, 6 Apr 2020 12:32:49 -0700 Subject: [PATCH] test: sometimes there are no metrics from 10 minutes ago (#411) --- monitoring/snippets/metrics.js | 8 +++----- monitoring/snippets/test/metrics.test.js | 6 +++++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/monitoring/snippets/metrics.js b/monitoring/snippets/metrics.js index 0df8929d55..39fedd3360 100644 --- a/monitoring/snippets/metrics.js +++ b/monitoring/snippets/metrics.js @@ -289,7 +289,6 @@ async function readTimeSeriesAggregate(projectId) { // [START monitoring_read_timeseries_align] // Imports the Google Cloud client library const monitoring = require('@google-cloud/monitoring'); - const util = require('util'); // Creates a client const client = new monitoring.MetricServiceClient(); @@ -325,11 +324,10 @@ async function readTimeSeriesAggregate(projectId) { console.log('CPU utilization:'); timeSeries.forEach(data => { console.log(data.metric.labels.instance_name); - for (const p of data.points) { - console.log(util.inspect(p)); - } console.log(` Now: ${data.points[0].value.doubleValue}`); - console.log(` 10 min ago: ${data.points[1].value.doubleValue}`); + if (data.points.length > 1) { + console.log(` 10 min ago: ${data.points[1].value.doubleValue}`); + } console.log('====='); }); // [END monitoring_read_timeseries_align] diff --git a/monitoring/snippets/test/metrics.test.js b/monitoring/snippets/test/metrics.test.js index 8f36c14b92..e184921feb 100644 --- a/monitoring/snippets/test/metrics.test.js +++ b/monitoring/snippets/test/metrics.test.js @@ -173,7 +173,11 @@ describe('metrics', async () => { timeSeries.forEach(data => { assert.include(output, data.metric.labels.instance_name); assert.include(output, ' Now: 0.'); - assert.include(output, ' 10 min ago: 0.'); + // Don't assert against a value from 10 minutes ago, if none is + // being reported by stackdriver: + if (data.points.length > 1) { + assert.include(output, ' 10 min ago: 0.'); + } }); });