Skip to content
Closed
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 experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
* feat(sdk-logs)!: add required `forceFlush()` to `LogRecordExporter` interface [#6356](https://github.com/open-telemetry/opentelemetry-js/pull/6356) @pichlermarc
* (user-facing): `LogRecordExporter` interface now requires a `forceFlush()` method to be implemented. Custom exporters will need to implement this method to continue working with the Logs SDK.
* feat(api-logs, sdk-logs)!: add Logger#enabled() [#6371](https://github.com/open-telemetry/opentelemetry-js/pull/6371) @david-luna
* fix(opentelemetry-exporter-prometheus)!: apply ratio suffix to gauges when necessary [#6613](https://github.com/open-telemetry/opentelemetry-js/pull/6613) @cjihrig

### :rocket: Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,19 @@ function enforcePrometheusNamingConvention(
name: string,
data: MetricData
): string {
if (
data.dataPointType === DataPointType.GAUGE &&
data.descriptor.unit === '1' &&
!name.endsWith('_ratio')
) {
name += '_ratio';
}

// Prometheus requires that metrics of the Counter kind have "_total" suffix
if (
!name.endsWith('_total') &&
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As a drive-by change, I switched the order of these checks to save the most expensive one for last.

data.dataPointType === DataPointType.SUM &&
data.isMonotonic
data.isMonotonic &&
!name.endsWith('_total')
Comment thread
dyladan marked this conversation as resolved.
) {
name = name + '_total';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,46 @@ describe('PrometheusSerializer', () => {

assert.strictEqual(result, 'test_total 1\n');
});

it('gauges should rename metric of type counter when name misses _total suffix', async () => {
const serializer = new PrometheusSerializer();
const reader = new TestMetricReader();
const meterProvider = new MeterProvider({
views: [
{
aggregation: {
type: AggregationType.LAST_VALUE,
},
instrumentName: '*',
},
],
readers: [reader],
});
const meter = meterProvider.getMeter('test');
const gauge = meter.createUpDownCounter('test_gauge', {
description: 'foobar',
unit: '1',
});

gauge.add(1, { val: '1' });
const { resourceMetrics, errors } = await reader.collect();
assert.strictEqual(errors.length, 0);
assert.strictEqual(resourceMetrics.scopeMetrics.length, 1);
assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1);
const scopeMetrics = resourceMetrics.scopeMetrics[0];
const resourceAttributes = resourceMetrics.resource.attributes;
serializer['_additionalAttributes'] = serializer[
'_filterResourceConstantLabels'
](resourceAttributes, serializer['_withResourceConstantLabels']);
const result = serializer['_serializeScopeMetrics'](scopeMetrics);
assert.strictEqual(
result,
'# HELP test_gauge_ratio foobar\n' +
'# UNIT test_gauge_ratio 1\n' +
'# TYPE test_gauge_ratio gauge\n' +
'test_gauge_ratio{val="1",otel_scope_name="test"} 1\n'
);
});
});

describe('serialize non-normalized values', () => {
Expand Down