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
2 changes: 2 additions & 0 deletions CHANGELOG.md

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if you always use the "create merge commit" merge method, but the second commit is basically empty/has the wrong commit message and could be rebased out.

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ This release marks our first release under the Prometheus umbrella.
- chore: Add copyright license headers and test
- Make cluster and worker-thread metric aggregation order deterministic
- Export `MetricObject`, `MetricObjectWithValues`, `MetricValue` and `MetricValueWithName` from the TypeScript definitions
- perf: Improve the memory usage of histograms by delaying allocation of bucket data
- fix: Avoid updating exemplar values during subsequent metric changes (Fixes [#616](https://github.com/prometheus/client_js/issues/616))

### Added

Expand Down
56 changes: 25 additions & 31 deletions lib/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Histogram extends Metric {
this.type = 'histogram';
this.defaultLabels = {};
this.defaultExemplarLabelSet = {};
this.enableExemplars = false;
this.enableExemplars = config.enableExemplars;

for (const label of this.labelNames) {
if (label === 'le') {
Expand All @@ -51,26 +51,17 @@ class Histogram extends Metric {
return acc;
}, {});

if (config.enableExemplars) {
this.enableExemplars = true;
this.bucketExemplars = this.upperBounds.reduce((acc, upperBound) => {
acc[upperBound] = null;
return acc;
}, {});
Object.freeze(this.bucketExemplars);
this.observe = this.observeWithExemplar;
} else {
this.observe = this.observeWithoutExemplar;
}
this.observe = this.enableExemplars
? this.observeWithExemplar
: this.observeWithoutExemplar;

Object.freeze(this.bucketValues);
Object.freeze(this.upperBounds);

if (this.labelNames.length === 0) {
this.store = new LabelMap(this.labelNames);
this.store.merge(
{},
createBaseValues({}, this.bucketValues, this.bucketExemplars),
createBaseValues({}, this.bucketValues, this.enableExemplars),
);
}
}
Expand All @@ -90,14 +81,13 @@ class Histogram extends Metric {
value,
exemplarLabels = this.defaultExemplarLabelSet,
} = {}) {
observe(this, labels === 0 ? 0 : labels || {}, value);
this.updateExemplar(labels, value, exemplarLabels);
const valueFromMap = observe(this, labels === 0 ? 0 : labels || {}, value);
this.updateExemplar(valueFromMap, value, exemplarLabels);
}

updateExemplar(labels, value, exemplarLabels) {
updateExemplar({ bucketExemplars }, value, exemplarLabels) {
if (isEmpty(exemplarLabels)) return;
const bound = findBound(this.upperBounds, value);
const { bucketExemplars } = this.store.entry(labels);
let exemplar = bucketExemplars[bound];
if (!isObject(exemplar)) {
exemplar = new Exemplar();
Expand Down Expand Up @@ -147,7 +137,7 @@ class Histogram extends Metric {
this.store.validate(labels);
this.store.merge(
labels,
createBaseValues(labels, this.bucketValues, this.bucketExemplars),
createBaseValues(labels, this.bucketValues, this.enableExemplars),
);
}

Expand All @@ -173,7 +163,9 @@ class Histogram extends Metric {
const labels = getLabels(this.labelNames, args);
this.store.validate(labels);
return {
observe: value => observe(this, labels, value),
observe: value => {
observe(this, labels, value);
},
startTimer: () => startTimer(this, labels),
};
}
Expand Down Expand Up @@ -233,6 +225,7 @@ function findBound(upperBounds, value) {
* @param {Histogram} histogram
* @param {object} labels - Object with labels where key is the label key and value is label value. Can only be one level deep
* @param {number} value - Value to observe in the histogram
* @returns {object} bucket data
*/
function observe(histogram, labels, value) {
const labelValuePair = convertLabelsAndValues(labels, value);
Expand All @@ -251,7 +244,7 @@ function observe(histogram, labels, value) {
createBaseValues(
labelValuePair.labels,
histogram.bucketValues,
histogram.bucketExemplars,
histogram.enableExemplars,
),
);
}
Expand All @@ -261,20 +254,22 @@ function observe(histogram, labels, value) {
entry.sum += labelValuePair.value;
entry.count += 1;

if (Object.hasOwn(entry.bucketValues, b)) {
if (typeof entry.bucketValues[b] === 'number') {
entry.bucketValues[b] += 1;
}

return entry;
}

function createBaseValues(labels, bucketValues, bucketExemplars) {
function createBaseValues(labels, bucketValues, enableExemplars) {
const result = {
labels,
bucketValues: { ...bucketValues },
bucketValues: Object.create(bucketValues),
sum: 0,
count: 0,
};
if (bucketExemplars) {
result.bucketExemplars = { ...bucketExemplars };
if (enableExemplars) {
result.bucketExemplars = {};
}
return result;
}
Expand All @@ -295,16 +290,15 @@ function extractBucketValuesForExport(histogram) {
const name = `${histogram.name}_bucket`;
return bucketData => {
let acc = 0;
const { labels, bucketValues, bucketExemplars } = bucketData;
const buckets = histogram.upperBounds.map(upperBound => {
acc += bucketData.bucketValues[upperBound];
acc += bucketValues[upperBound];
return setValuePair(
{ le: upperBound },
acc,
name,
bucketData.bucketExemplars
? bucketData.bucketExemplars[upperBound]
: null,
bucketData.labels,
bucketExemplars?.[upperBound] ?? null,
labels,
);
});
return { buckets, data: bucketData };
Expand Down
Loading