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: export unit block when unit is set in metric descriptor #3041

Merged
merged 9 commits into from
Jul 29, 2022
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feature(prometheus-serialiser): export the unit block when unit is set in metric descriptor [#3066](https://github.com/open-telemetry/opentelemetry-js/pull/3041) @weyert

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ export class PrometheusSerializer {
const help = `# HELP ${name} ${escapeString(
metricData.descriptor.description || 'description missing'
)}`;
const unit = metricData.descriptor.unit ? `\n# UNIT ${name} ${escapeString(
dyladan marked this conversation as resolved.
Show resolved Hide resolved
metricData.descriptor.unit,
)}` : '';
const type = `# TYPE ${name} ${toPrometheusType(
metricData
)}`;
Expand All @@ -235,7 +238,7 @@ export class PrometheusSerializer {
}
}

return `${help}\n${type}\n${results}`.trim();
return `${help}${unit}\n${type}\n${results}`.trim();
}

private _serializeSingularDataPoint(name: string, type: InstrumentType, dataPoint: DataPoint<number>): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,17 @@ describe('PrometheusSerializer', () => {
});

describe('validate against metric conventions', () => {
async function getCounterResult(name: string, serializer: PrometheusSerializer) {

async function getCounterResult(name: string, serializer: PrometheusSerializer, options: Partial<{ unit: string, exportAll: boolean }> = {}) {
const reader = new TestMetricReader();
const meterProvider = new MeterProvider({
views: [new View({ aggregation: new SumAggregation(), instrumentName: '*' })]
});
meterProvider.addMetricReader(reader);
const meter = meterProvider.getMeter('test');

const counter = meter.createCounter(name);
const { unit, exportAll = false } = options;
const counter = meter.createCounter(name, { unit: unit });
counter.add(1);

const { resourceMetrics, errors } = await reader.collect();
Expand All @@ -447,10 +449,41 @@ describe('PrometheusSerializer', () => {
const pointData = metric.dataPoints as DataPoint<number>[];
assert.strictEqual(pointData.length, 1);

const result = serializer['_serializeSingularDataPoint'](metric.descriptor.name, metric.descriptor.type, pointData[0]);
return result;
if (exportAll) {
const result = serializer.serialize(resourceMetrics);
return result;
} else {
const result = serializer['_serializeSingularDataPoint'](metric.descriptor.name, metric.descriptor.type, pointData[0]);
return result;
}
}

it('should export unit block when unit of metric is given', async () => {
weyert marked this conversation as resolved.
Show resolved Hide resolved
const serializer = new PrometheusSerializer();

const unitOfMetric = 'seconds';
const result = await getCounterResult('test', serializer, { unit: unitOfMetric, exportAll: true });
assert.strictEqual(
result,
'# HELP test_total description missing\n' +
`# UNIT test_total ${unitOfMetric}\n` +
'# TYPE test_total counter\n' +
`test_total 1 ${mockedHrTimeMs}\n`
);
});

it('should not export unit block when unit of metric is missing', async () => {
const serializer = new PrometheusSerializer();

const result = await getCounterResult('test', serializer, { exportAll: true });
assert.strictEqual(
result,
'# HELP test_total description missing\n' +
'# TYPE test_total counter\n' +
`test_total 1 ${mockedHrTimeMs}\n`
);
});

it('should rename metric of type counter when name misses _total suffix', async () => {
const serializer = new PrometheusSerializer();

Expand Down