Skip to content

Commit

Permalink
Merge 9130227 into a0385c4
Browse files Browse the repository at this point in the history
  • Loading branch information
weyert authored Apr 22, 2021
2 parents a0385c4 + 9130227 commit 99abebf
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ function sanitizePrometheusMetricName(name: string): string {
return name.replace(invalidCharacterRegex, '_'); // replace all invalid characters with '_'
}

/**
* @private
*
* Helper method which assists in enforcing the naming conventions for metric
* names in Prometheus
* @param name the name of the metric
* @param kind the kind of metric
* @returns string
*/
function enforcePrometheusNamingConvention(
name: string,
kind: MetricKind
): string {
// Prometheus requires that metrics of the Counter kind have "_total" suffix
if (!name.endsWith('_total') && kind === MetricKind.COUNTER) {
name = name + '_total';
}

return name;
}

function valueString(value: number) {
if (Number.isNaN(value)) {
return 'Nan';
Expand Down Expand Up @@ -162,6 +183,12 @@ export class PrometheusSerializer {
if (this._prefix) {
name = `${this._prefix}${name}`;
}

name = enforcePrometheusNamingConvention(
name,
checkpoint.descriptor.metricKind
);

const help = `# HELP ${name} ${escapeString(
checkpoint.descriptor.description || 'description missing'
)}`;
Expand All @@ -179,6 +206,12 @@ export class PrometheusSerializer {

serializeRecord(name: string, record: MetricRecord): string {
let results = '';

name = enforcePrometheusNamingConvention(
name,
record.descriptor.metricKind
);

switch (record.aggregator.kind) {
case AggregatorKind.SUM:
case AggregatorKind.LAST_VALUE: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('PrometheusExporter', () => {
});

it('should export a count aggregation', done => {
const counter = meter.createCounter('counter', {
const counter = meter.createCounter('counter_total', {
description: 'a test description',
});

Expand All @@ -251,13 +251,13 @@ describe('PrometheusExporter', () => {

assert.strictEqual(
lines[0],
'# HELP counter a test description'
'# HELP counter_total a test description'
);

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

Expand Down Expand Up @@ -313,7 +313,7 @@ describe('PrometheusExporter', () => {
});

it('should export multiple labels', done => {
const counter = meter.createCounter('counter', {
const counter = meter.createCounter('counter_total', {
description: 'a test description',
}) as CounterMetric;

Expand All @@ -328,10 +328,10 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP counter a test description',
'# TYPE counter counter',
`counter{counterKey1="labelValue1"} 10 ${mockedHrTimeMs}`,
`counter{counterKey1="labelValue2"} 20 ${mockedHrTimeMs}`,
'# HELP counter_total a test description',
'# TYPE counter_total counter',
`counter_total{counterKey1="labelValue1"} 10 ${mockedHrTimeMs}`,
`counter_total{counterKey1="labelValue2"} 20 ${mockedHrTimeMs}`,
'',
]);

Expand All @@ -344,7 +344,7 @@ describe('PrometheusExporter', () => {
});

it('should export multiple labels on manual shutdown', done => {
const counter = meter.createCounter('counter', {
const counter = meter.createCounter('counter_total', {
description: 'a test description',
}) as CounterMetric;

Expand All @@ -359,11 +359,11 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP counter a test description',
'# TYPE counter counter',
`counter{counterKey1="labelValue1"} 10 ${mockedHrTimeMs}`,
`counter{counterKey1="labelValue2"} 20 ${mockedHrTimeMs}`,
`counter{counterKey1="labelValue3"} 30 ${mockedHrTimeMs}`,
'# HELP counter_total a test description',
'# TYPE counter_total counter',
`counter_total{counterKey1="labelValue1"} 10 ${mockedHrTimeMs}`,
`counter_total{counterKey1="labelValue2"} 20 ${mockedHrTimeMs}`,
`counter_total{counterKey1="labelValue3"} 30 ${mockedHrTimeMs}`,
'',
]);

Expand Down Expand Up @@ -392,7 +392,7 @@ describe('PrometheusExporter', () => {
});

it('should add a description if missing', done => {
const counter = meter.createCounter('counter');
const counter = meter.createCounter('counter_total');

const boundCounter = counter.bind({ key1: 'labelValue1' });
boundCounter.add(10);
Expand All @@ -405,9 +405,9 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP counter description missing',
'# TYPE counter counter',
`counter{key1="labelValue1"} 10 ${mockedHrTimeMs}`,
'# HELP counter_total description missing',
'# TYPE counter_total counter',
`counter_total{key1="labelValue1"} 10 ${mockedHrTimeMs}`,
'',
]);

Expand All @@ -432,9 +432,9 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP counter_bad_name description missing',
'# TYPE counter_bad_name counter',
`counter_bad_name{key1="labelValue1"} 10 ${mockedHrTimeMs}`,
'# HELP counter_bad_name_total description missing',
'# TYPE counter_bad_name_total counter',
`counter_bad_name_total{key1="labelValue1"} 10 ${mockedHrTimeMs}`,
'',
]);

Expand Down Expand Up @@ -620,9 +620,9 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP test_prefix_counter description missing',
'# TYPE test_prefix_counter counter',
`test_prefix_counter{key1="labelValue1"} 10 ${mockedHrTimeMs}`,
'# HELP test_prefix_counter_total description missing',
'# TYPE test_prefix_counter_total counter',
`test_prefix_counter_total{key1="labelValue1"} 10 ${mockedHrTimeMs}`,
'',
]);

Expand Down Expand Up @@ -650,9 +650,9 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP counter description missing',
'# TYPE counter counter',
`counter{key1="labelValue1"} 10 ${mockedHrTimeMs}`,
'# HELP counter_total description missing',
'# TYPE counter_total counter',
`counter_total{key1="labelValue1"} 10 ${mockedHrTimeMs}`,
'',
]);

Expand Down Expand Up @@ -680,9 +680,9 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP counter description missing',
'# TYPE counter counter',
`counter{key1="labelValue1"} 10 ${mockedHrTimeMs}`,
'# HELP counter_total description missing',
'# TYPE counter_total counter',
`counter_total{key1="labelValue1"} 10 ${mockedHrTimeMs}`,
'',
]);

Expand Down Expand Up @@ -710,9 +710,9 @@ describe('PrometheusExporter', () => {
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
'# HELP counter description missing',
'# TYPE counter counter',
'counter{key1="labelValue1"} 10',
'# HELP counter_total description missing',
'# TYPE counter_total counter',
'counter_total{key1="labelValue1"} 10',
'',
]);

Expand Down
Loading

0 comments on commit 99abebf

Please sign in to comment.