Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Changed

- remove unnecessary loop from `osMemoryHeapLinux`
- Improve performance of `hashObject` by using pre-sorted array of label names

### Added

Expand Down
4 changes: 2 additions & 2 deletions lib/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Counter extends Metric {
incWithoutExemplar(labels, value) {
let hash = '';
if (isObject(labels)) {
hash = hashObject(labels);
hash = hashObject(labels, this.sortedLabelNames);
validateLabel(this.labelNames, labels);
} else {
value = labels;
Expand Down Expand Up @@ -130,7 +130,7 @@ class Counter extends Metric {
remove(...args) {
const labels = getLabels(this.labelNames, args) || {};
validateLabel(this.labelNames, labels);
return removeLabels.call(this, this.hashMap, labels);
return removeLabels.call(this, this.hashMap, labels, this.sortedLabelNames);
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/gauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class Gauge extends Metric {
}

_getValue(labels) {
const hash = hashObject(labels || {});
const hash = hashObject(labels || {}, this.sortedLabelNames);
return this.hashMap[hash] ? this.hashMap[hash].value : 0;
}

Expand All @@ -139,7 +139,7 @@ class Gauge extends Metric {
remove(...args) {
const labels = getLabels(this.labelNames, args);
validateLabel(this.labelNames, labels);
removeLabels.call(this, this.hashMap, labels);
removeLabels.call(this, this.hashMap, labels, this.sortedLabelNames);
}
}

Expand All @@ -158,7 +158,7 @@ function setDelta(gauge, labels, delta) {
}

validateLabel(gauge.labelNames, labels);
const hash = hashObject(labels);
const hash = hashObject(labels, gauge.sortedLabelNames);
setValueDelta(gauge.hashMap, delta, labels, hash);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Histogram extends Metric {
}

updateExemplar(labels, value, exemplarLabels) {
const hash = hashObject(labels);
const hash = hashObject(labels, this.sortedLabelNames);
const bound = findBound(this.upperBounds, value);
const { bucketExemplars } = this.hashMap[hash];
let exemplar = bucketExemplars[bound];
Expand Down Expand Up @@ -132,7 +132,7 @@ class Histogram extends Metric {
* @returns {void}
*/
zero(labels) {
const hash = hashObject(labels);
const hash = hashObject(labels, this.sortedLabelNames);
this.hashMap[hash] = createBaseValues(
labels,
this.bucketValues,
Expand Down Expand Up @@ -167,7 +167,7 @@ class Histogram extends Metric {
remove(...args) {
const labels = getLabels(this.labelNames, args);
validateLabel(this.labelNames, labels);
removeLabels.call(this, this.hashMap, labels);
removeLabels.call(this, this.hashMap, labels, this.sortedLabelNames);
}
}

Expand Down Expand Up @@ -214,7 +214,7 @@ function observe(labels) {
);
}

const hash = hashObject(labelValuePair.labels);
const hash = hashObject(labelValuePair.labels, this.sortedLabelNames);
let valueFromMap = this.hashMap[hash];
if (!valueFromMap) {
valueFromMap = createBaseValues(
Expand Down
7 changes: 7 additions & 0 deletions lib/metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ class Metric {
if (!validateLabelName(this.labelNames)) {
throw new Error('Invalid label name');
}

if (this.collect && typeof this.collect !== 'function') {
throw new Error('Optional "collect" parameter must be a function');
}

if (this.labelNames) {
this.sortedLabelNames = [...this.labelNames].sort();
} else {
this.sortedLabelNames = [];
}

this.reset();

for (const register of this.registers) {
Expand Down
4 changes: 2 additions & 2 deletions lib/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Summary extends Metric {
remove(...args) {
const labels = getLabels(this.labelNames, args);
validateLabel(this.labelNames, labels);
removeLabels.call(this, this.hashMap, labels);
removeLabels.call(this, this.hashMap, labels, this.sortedLabelNames);
}
}

Expand Down Expand Up @@ -170,7 +170,7 @@ function observe(labels) {
);
}

const hash = hashObject(labelValuePair.labels);
const hash = hashObject(labelValuePair.labels, this.sortedLabelNames);
let summaryOfLabel = this.hashMap[hash];
if (!summaryOfLabel) {
summaryOfLabel = {
Expand Down
50 changes: 33 additions & 17 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ exports.getValueAsString = function getValueString(value) {
}
};

exports.removeLabels = function removeLabels(hashMap, labels) {
const hash = hashObject(labels);
exports.removeLabels = function removeLabels(
hashMap,
labels,
sortedLabelNames,
) {
const hash = hashObject(labels, sortedLabelNames);
delete hashMap[hash];
};

Expand Down Expand Up @@ -59,32 +63,44 @@ exports.getLabels = function (labelNames, args) {
return acc;
};

function hashObject(labels) {
// We don't actually need a hash here. We just need a string that
// is unique for each possible labels object and consistent across
// calls with equivalent labels objects.
let keys = Object.keys(labels);
function fastHashObject(keys, labels) {
if (keys.length === 0) {
return '';
}
// else
if (keys.length > 1) {
keys = keys.sort(); // need consistency across calls
}

let hash = '';
let i = 0;
const size = keys.length;
for (; i < size - 1; i++) {
hash += `${keys[i]}:${labels[keys[i]]},`;

for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = labels[key];
if (value === undefined) continue;

hash += `${key}:${value},`;
}
hash += `${keys[i]}:${labels[keys[i]]}`;

return hash;
}

function hashObject(labels, labelNames) {
// We don't actually need a hash here. We just need a string that
// is unique for each possible labels object and consistent across
// calls with equivalent labels objects.

if (labelNames) {
return fastHashObject(labelNames, labels);
}

const keys = Object.keys(labels);
if (keys.length > 1) {
keys.sort(); // need consistency across calls
}

return fastHashObject(keys, labels);
}
exports.hashObject = hashObject;

exports.isObject = function isObject(obj) {
return obj === Object(obj);
return obj !== null && typeof obj === 'object';
};

exports.nowTimestamp = function nowTimestamp() {
Expand Down