Skip to content

Commit

Permalink
Merge pull request #94 from metrics-js/invalid-error-log
Browse files Browse the repository at this point in the history
Fixes invalid error logging
  • Loading branch information
leftieFriele authored Feb 1, 2024
2 parents 5c6134a + 4840ca5 commit 3bbc95c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,43 +249,43 @@ module.exports = class PrometheusMetricsConsumer extends Writable {
this.histogram(metric);
} catch (err) {
this.logger.error(
err,
`failed to generate prometheus histogram for metric "${JSON.stringify(
metric,
)}"`,
err,
);
}
} else if (this.typeFor(metric) === metricTypes.GAUGE) {
try {
this.gauge(metric);
} catch (err) {
this.logger.error(
err,
`failed to generate prometheus gauge for metric "${JSON.stringify(
metric,
)}"`,
err,
);
}
} else if (this.typeFor(metric) === metricTypes.SUMMARY) {
try {
this.summary(metric);
} catch (err) {
this.logger.error(
err,
`failed to generate prometheus summary metric for "${JSON.stringify(
metric,
)}"`,
err,
);
}
} else {
try {
this.counter(metric);
} catch (err) {
this.logger.error(
err,
`failed to generate prometheus counter for metric "${JSON.stringify(
metric,
)}"`,
err,
);
}
}
Expand Down
54 changes: 54 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,57 @@ test('should have correct bucket count', (t) => {

source.pipe(consumer);
});

test('should log errors according to abslog contract', (t) => {
let errObject;
let errMsg = '';
const log = () => {};
const err = (obj, msg) => {
errObject = obj;
errMsg = msg;
};

const mockLogger = {
trace: log,
debug: log,
info: log,
warn: log,
error: err,
fatal: log,
};
const consumer = new PrometheusMetricsConsumer({
logger: mockLogger,
client: promClient,
});

const source = src([
{},
'',
{ name: 'test' },
new Metric({
name: 'test3',
description: '.',
labels: [{ name: 'label3', value: 'one' }],
value: 1,
type: 2,
}),
new Metric({
name: 'test3',
description: '.',
labels: [
{ name: 'label3', value: 'one' },
{ name: 'label4', value: 'two' },
],
value: 1,
type: 2,
}),
]);

consumer.on('finish', () => {
t.match(errObject, /Error/);
t.match(errMsg, 'failed to generate prometheus counter for metric');
t.end();
});

source.pipe(consumer);
});

0 comments on commit 3bbc95c

Please sign in to comment.