Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
- Organized default metrics
- perf: Histogram rendering builds its export list straight from the store iterator instead of an intermediate array. Faster at high series counts on Node 24 and 26, can be slightly slower on Node 22
- fix: Correct content type exported for cluster and worker mode.
- perf: Remove truthy conditionals from default metric collectors
- perf: Remove truthy conditionals from the codebase
- fix: Preserve zero-valued Counter exemplars
- perf: Remove object and array fallback truthiness from core metric paths

### Added

Expand Down
5 changes: 2 additions & 3 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ async function primaryListener(worker, event) {
}
request.responseHandlers.delete(worker.id);

if (event.error) {
if (event.error !== undefined) {
response.reject(new Error(event.error));
} else {
response.resolve({
Expand Down Expand Up @@ -409,8 +409,7 @@ function scanListeners(messageType, emitter, fn) {
const functionString = fn.toString();

for (const listener of emitter.listeners(messageType)) {
// eslint-disable-next-line eqeqeq
if (functionString == listener) {
if (functionString === `${listener}`) {
if (['test', 'development'].includes(process.env.NODE_ENV)) {
debug('removing duplicate listener', messageType);
emitter.off(messageType, listener);
Expand Down
2 changes: 1 addition & 1 deletion lib/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Counter extends Metric {
}

async get() {
if (this.collect) {
if (this.collect !== undefined) {
const v = this.collect();
if (v instanceof Promise) await v;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/gauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Gauge extends Metric {
}

async get() {
if (this.collect) {
if (this.collect !== undefined) {
const v = this.collect();
if (v instanceof Promise) await v;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Histogram extends Metric {
}

async getForPromString() {
if (this.collect) {
if (this.collect !== undefined) {
const v = this.collect();
if (v instanceof Promise) await v;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class Metric {
defaults,
config,
);
if (!this.registers) {
// in case config.registers is `undefined`
this.registers = [Registry.globalRegistry];
}

// in case config.registers is `undefined`
this.registers ??= [Registry.globalRegistry];

if (!this.help) {
throw new Error('Missing mandatory help parameter');
}
Expand Down
5 changes: 1 addition & 4 deletions lib/pushgateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ async function useGateway(method, job, groupings) {
this.registry
.metrics()
.then(metrics => {
if (
options.headers &&
options.headers['Content-Encoding'] === 'gzip'
) {
if (options.headers?.['Content-Encoding'] === 'gzip') {
metrics = gzipSync(metrics);
}
req.write(metrics);
Expand Down
12 changes: 6 additions & 6 deletions lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ class Registry {
val.value,
)}`;

const { exemplar } = val;
if (exemplar && isOpenMetrics) {
const formattedExemplars = formatLabels(exemplar.labelSet);
fullMetricLine += ` # {${formattedExemplars.join(
',',
)}} ${getValueAsString(exemplar.value)} ${exemplar.timestamp}`;
if (isOpenMetrics) {
const { exemplar } = val;
if (exemplar) {
const formattedExemplars = formatLabels(exemplar.labelSet).join(',');
fullMetricLine += ` # {${formattedExemplars}} ${getValueAsString(exemplar.value)} ${exemplar.timestamp}`;
}
}
values.push(fullMetricLine);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/timeWindowQuantiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const { TDigest } = require('tdigest');

class TimeWindowQuantiles {
constructor(maxAgeSeconds, ageBuckets) {
this.maxAgeSeconds = maxAgeSeconds || 0;
this.ageBuckets = ageBuckets || 0;
this.maxAgeSeconds = maxAgeSeconds ?? 0;
this.ageBuckets = ageBuckets ?? 0;

this.shouldRotate = maxAgeSeconds && ageBuckets;

Expand Down
2 changes: 1 addition & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function hashObject(labels, labelNames) {
// is unique for each possible labels object and consistent across
// calls with equivalent labels objects.

if (labelNames) {
if (labelNames !== undefined) {
return fastHashObject(labelNames, labels);
}

Expand Down
4 changes: 2 additions & 2 deletions test/clusterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ describe.each([
} finally {
jest.dontMock('cluster');
gauge.remove();
if (connectedDescriptor) {
if (connectedDescriptor !== undefined) {
Object.defineProperty(process, 'connected', connectedDescriptor);
} else {
delete process.connected;
Expand Down Expand Up @@ -418,7 +418,7 @@ describe('worker message handling', () => {
jest.resetModules();
jest.dontMock('cluster');
process.removeListener('message', listener);
if (connectedDescriptor) {
if (connectedDescriptor !== undefined) {
Object.defineProperty(process, 'connected', connectedDescriptor);
} else {
delete process.connected;
Expand Down
4 changes: 2 additions & 2 deletions test/defaultMetricsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe.each([
beforeAll(() => {
cpuUsage = process.cpuUsage;

if (cpuUsage) {
if (cpuUsage !== undefined) {
Object.defineProperty(process, 'cpuUsage', {
value() {
return { user: 1000, system: 10 };
Expand All @@ -43,7 +43,7 @@ describe.each([
});

afterAll(() => {
if (cpuUsage) {
if (cpuUsage !== undefined) {
Object.defineProperty(process, 'cpuUsage', {
value: cpuUsage,
});
Expand Down
9 changes: 5 additions & 4 deletions test/exemplarsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,18 @@ describe('Exemplars', () => {
});
});

function getValueByLabel(label, values, key) {
function getValueByLabel(label, values, key = 'le') {
return values.reduce((acc, val) => {
if (val.labels && val.labels[key || 'le'] === label) {
if (val.labels?.[key] === label) {
acc = val;
}
return acc;
}, {});
}
function getValuesByLabel(label, values, key) {

function getValuesByLabel(label, values, key = 'le') {
return values.reduce((acc, val) => {
if (val.labels && val.labels[key || 'le'] === label) {
if (val.labels?.[key] === label) {
acc.push(val);
}
return acc;
Expand Down
8 changes: 4 additions & 4 deletions test/histogramTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,17 +503,17 @@ describe.each([
return acc;
}, {});
}
function getValueByLabel(label, values, key) {
function getValueByLabel(label, values, key = 'le') {
return values.reduce((acc, val) => {
if (val.labels && val.labels[key || 'le'] === label) {
if (val.labels?.[key] === label) {
acc = val;
}
return acc;
}, {});
}
function getValuesByLabel(label, values, key) {
function getValuesByLabel(label, values, key = 'le') {
return values.reduce((acc, val) => {
if (val.labels && val.labels[key || 'le'] === label) {
if (val.labels?.[key] === label) {
acc.push(val);
}
return acc;
Expand Down
2 changes: 1 addition & 1 deletion test/metrics/gcTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe.each([
// node version is too old
}

if (perf_hooks) {
if (perf_hooks !== undefined) {
expect(metrics).toHaveLength(1);

expect(metrics[0].help).toEqual(
Expand Down
Loading