Skip to content

Commit

Permalink
feat: Add missing prometheus exports for ValueRecorder, SumObserver &…
Browse files Browse the repository at this point in the history
… UpDownSumObserver (#1428)

Co-authored-by: Valentin Marchaud <[email protected]>
Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
3 people authored Aug 17, 2020
1 parent cb58e7a commit 2916ecf
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/opentelemetry-exporter-prometheus/src/prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,12 @@ export class PrometheusExporter implements MetricExporter {
return new Counter(metricObject);
case MetricKind.UP_DOWN_COUNTER:
return new Gauge(metricObject);
// case MetricKind.VALUE_RECORDER:
// case MetricKind.SUM_OBSERVER:
// case MetricKind.UP_DOWN_SUM_OBSERVER:
case MetricKind.VALUE_RECORDER:
return new Gauge(metricObject);
case MetricKind.SUM_OBSERVER:
return new Counter(metricObject);
case MetricKind.UP_DOWN_SUM_OBSERVER:
return new Gauge(metricObject);
case MetricKind.VALUE_OBSERVER:
return new Gauge(metricObject);
default:
Expand Down
116 changes: 116 additions & 0 deletions packages/opentelemetry-exporter-prometheus/test/prometheus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,122 @@ describe('PrometheusExporter', () => {
});
});
});

it('should export a SumObserver as a counter', done => {
function getValue() {
return 20;
}

meter.createSumObserver(
'sum_observer',
{
description: 'a test description',
},
(observerResult: ObserverResult) => {
observerResult.observe(getValue(), {
key1: 'labelValue1',
});
}
);

meter.collect().then(() => {
exporter.export(meter.getBatcher().checkPointSet(), () => {
http
.get('http://localhost:9464/metrics', res => {
res.on('data', chunk => {
const body = chunk.toString();
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP sum_observer a test description',
'# TYPE sum_observer counter',
`sum_observer{key1="labelValue1"} 20 ${mockedTimeMS}`,
'',
]);
});

done();
})
.on('error', errorHandler(done));
});
});
});

it('should export a UpDownSumObserver as a gauge', done => {
function getValue() {
return 20;
}

meter.createUpDownSumObserver(
'updown_observer',
{
description: 'a test description',
},
(observerResult: ObserverResult) => {
observerResult.observe(getValue(), {
key1: 'labelValue1',
});
}
);

meter.collect().then(() => {
exporter.export(meter.getBatcher().checkPointSet(), () => {
http
.get('http://localhost:9464/metrics', res => {
res.on('data', chunk => {
const body = chunk.toString();
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP updown_observer a test description',
'# TYPE updown_observer gauge',
'updown_observer{key1="labelValue1"} 20',
'',
]);
});

done();
})
.on('error', errorHandler(done));
});
});
});

it('should export a ValueRecorder as a gauge', done => {
const valueRecorder = meter.createValueRecorder('value_recorder', {
description: 'a test description',
});

valueRecorder.bind({ key1: 'labelValue1' }).record(20);

meter.collect().then(() => {
exporter.export(meter.getBatcher().checkPointSet(), () => {
http
.get('http://localhost:9464/metrics', res => {
res.on('data', chunk => {
const body = chunk.toString();
const lines = body.split('\n');

assert.strictEqual(
lines[0],
'# HELP value_recorder a test description'
);
assert.strictEqual(lines[1], '# TYPE value_recorder gauge');

const line3 = lines[2].split(' ');
assert.strictEqual(
line3[0],
'value_recorder{key1="labelValue1"}'
);
assert.equal(line3[1], 20);

done();
});
})
.on('error', errorHandler(done));
});
});
});
});

describe('configuration', () => {
Expand Down

0 comments on commit 2916ecf

Please sign in to comment.