Skip to content

Commit

Permalink
Merge branch 'main' into fix-grpc-instrumentation-attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Jul 30, 2022
2 parents 3dd925e + 492a3e8 commit aad7024
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
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(
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 () => {
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

0 comments on commit aad7024

Please sign in to comment.