Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

catch errors from createTimeSeries #1066

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
try {
await this.export();
} catch (err) {
if (typeof this.onMetricUploadError === 'function') {
this.onMetricUploadError(err);
}
this.reportMetricUploadError(err);
}
}, this.period);
}
Expand All @@ -134,7 +132,15 @@ export class StackdriverStatsExporter implements StatsEventListener {
}
}

this.createTimeSeries(metricsList);
this.createTimeSeries(metricsList).catch(err => {
this.reportMetricUploadError(err);
});
}

private reportMetricUploadError(err) {
if (typeof this.onMetricUploadError === 'function') {
this.onMetricUploadError(err);
}
}

/**
Expand Down
17 changes: 12 additions & 5 deletions packages/opencensus-exporter-stackdriver/test/nocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,24 @@ export function batchWrite<T extends {} = {}>(
return reply ? interceptor.reply(reply) : interceptor.reply(200);
}

export function timeSeries<T extends {} = {}>(
export function timeSeriesNock<T extends {} = {}>(
project: string,
validator?: (body: T) => boolean,
reply?: () => string,
withError?: boolean
validator?: (body: T) => boolean
) {
validator = validator || accept;
const interceptor = nock('https://monitoring.googleapis.com').post(
return nock('https://monitoring.googleapis.com').post(
'/v3/projects/' + project + '/timeSeries',
validator
);
}

export function timeSeries<T extends {} = {}>(
project: string,
validator?: (body: T) => boolean,
reply?: () => string,
withError?: boolean
) {
const interceptor = timeSeriesNock<T>(project, validator);
return reply ? interceptor.reply(reply) : interceptor.reply(200);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,31 @@ describe('Stackdriver Stats Exporter', () => {
const mockLogger = new MockLogger();
let exporterOptions: StackdriverExporterOptions;
let exporter: StackdriverStatsExporter;
const METRIC_NAME = 'metric-name';
const METRIC_DESCRIPTION = 'metric-description';
const UNIT = MeasureUnit.UNIT;
const METRIC_OPTIONS = {
description: METRIC_DESCRIPTION,
unit: UNIT,
labelKeys: [{ key: 'code', description: 'desc' }],
};

const metricRegistry = Metrics.getMetricRegistry();

const gauge = metricRegistry.addInt64Gauge(METRIC_NAME, METRIC_OPTIONS);

// tslint:disable-next-line:no-any
let metricUploadErrors: any[];

before(() => {
metricUploadErrors = [];
exporterOptions = {
period: 0,
projectId: PROJECT_ID,
logger: mockLogger,
onMetricUploadError: err => {
metricUploadErrors.push(err);
},
};
nocks.noDetectResource();
exporter = new StackdriverStatsExporter(exporterOptions);
Expand All @@ -90,18 +109,6 @@ describe('Stackdriver Stats Exporter', () => {
});

it('should export the data', async () => {
const METRIC_NAME = 'metric-name';
const METRIC_DESCRIPTION = 'metric-description';
const UNIT = MeasureUnit.UNIT;
const METRIC_OPTIONS = {
description: METRIC_DESCRIPTION,
unit: UNIT,
labelKeys: [{ key: 'code', description: 'desc' }],
};

const metricRegistry = Metrics.getMetricRegistry();

const gauge = metricRegistry.addInt64Gauge(METRIC_NAME, METRIC_OPTIONS);
gauge.getDefaultTimeSeries().add(100);

nocks.metricDescriptors(PROJECT_ID);
Expand Down Expand Up @@ -146,5 +153,20 @@ describe('Stackdriver Stats Exporter', () => {
assert.deepStrictEqual(timeSeries.points[0].value.int64Value, 100);
});
});

it('should export the data with an error', async () => {
gauge.getDefaultTimeSeries().add(100);

nocks.metricDescriptors(PROJECT_ID);
nocks.timeSeriesNock(PROJECT_ID).replyWithError('intentional failure');

await exporter.export();

await new Promise(resolve => setTimeout(resolve, DELAY)).then(() => {
assert.strictEqual(metricUploadErrors.length, 1);
const message = metricUploadErrors[0].message;
assert.ok(message.indexOf('intentional failure') >= 0, message);
});
});
});
});