Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make prometheus histogram export cumulative #1570

Merged
merged 6 commits into from
Oct 8, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,15 @@ export class PrometheusSerializer {
undefined
);
}

let cumulativeSum = 0;
for (const [idx, val] of value.buckets.counts.entries()) {
cumulativeSum += val;
const upperBound = value.buckets.boundaries[idx];
results += stringify(
name + '_bucket',
record.labels,
val,
cumulativeSum,
this._appendTimestamp ? timestamp : undefined,
{
le: upperBound === undefined ? '+Inf' : String(upperBound),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ describe('PrometheusSerializer', () => {
`test_sum{foo1="bar1",foo2="bar2"} 5 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="1"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="10"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="100"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="+Inf"} 0 ${mockedHrTimeMs}\n`
`test_bucket{foo1="bar1",foo2="bar2",le="100"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{foo1="bar1",foo2="bar2",le="+Inf"} 1 ${mockedHrTimeMs}\n`
);
});

Expand All @@ -196,8 +196,8 @@ describe('PrometheusSerializer', () => {
'test_sum{foo1="bar1",foo2="bar2"} 5\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="1"} 0\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="10"} 1\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="100"} 0\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="+Inf"} 0\n'
'test_bucket{foo1="bar1",foo2="bar2",le="100"} 1\n' +
'test_bucket{foo1="bar1",foo2="bar2",le="+Inf"} 1\n'
);
});
});
Expand Down Expand Up @@ -302,7 +302,7 @@ describe('PrometheusSerializer', () => {
describe('with HistogramAggregator', () => {
mockAggregator(HistogramAggregator);

it('serialize metric record with MinMaxLastSumCountAggregator aggregator', async () => {
it('serialize metric record with HistogramAggregator aggregator, cumulative', async () => {
const serializer = new PrometheusSerializer();

const batcher = new ExactBatcher(HistogramAggregator, [1, 10, 100]);
Expand All @@ -311,6 +311,9 @@ describe('PrometheusSerializer', () => {
description: 'foobar',
}) as ValueRecorderMetric;
recorder.bind({ val: '1' }).record(5);
recorder.bind({ val: '1' }).record(50);
recorder.bind({ val: '1' }).record(120);

recorder.bind({ val: '2' }).record(5);

const records = await recorder.getMetricRecord();
Expand All @@ -323,18 +326,18 @@ describe('PrometheusSerializer', () => {
result,
'# HELP test foobar\n' +
'# TYPE test histogram\n' +
`test_count{val="1"} 1 ${mockedHrTimeMs}\n` +
`test_sum{val="1"} 5 ${mockedHrTimeMs}\n` +
`test_count{val="1"} 3 ${mockedHrTimeMs}\n` +
`test_sum{val="1"} 175 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="1"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="10"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="100"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="+Inf"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="100"} 2 ${mockedHrTimeMs}\n` +
`test_bucket{val="1",le="+Inf"} 3 ${mockedHrTimeMs}\n` +
`test_count{val="2"} 1 ${mockedHrTimeMs}\n` +
`test_sum{val="2"} 5 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="1"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="10"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="100"} 0 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="+Inf"} 0 ${mockedHrTimeMs}\n`
`test_bucket{val="2",le="100"} 1 ${mockedHrTimeMs}\n` +
`test_bucket{val="2",le="+Inf"} 1 ${mockedHrTimeMs}\n`
);
});
});
Expand Down