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

feat: Add missing prometheus exports for ValueRecorder, SumObserver & UpDownSumObserver #1428

Merged
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
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