From cbd437e73c3a09f1305fca05211b1b81defe7553 Mon Sep 17 00:00:00 2001 From: Weyert de Boer Date: Mon, 19 Apr 2021 17:53:37 +0100 Subject: [PATCH 1/7] feat: add diag warning when metric name is invalid If you are using the metric kind `counter` Prometheus expects the metric name to have the suffix `_total` as it will throw the following error: ``` failed_requests counter metrics should have "_total" suffix ``` This commit logs a warning message when the diag log level is DEBUG to assist implementors --- .../src/PrometheusSerializer.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts b/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts index 45b1abdad48..b6c371c2b11 100644 --- a/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts +++ b/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts @@ -21,6 +21,7 @@ import { import { PrometheusCheckpoint } from './types'; import { Labels } from '@opentelemetry/api-metrics'; import { hrTimeToMilliseconds } from '@opentelemetry/core'; +import { diag } from '@opentelemetry/api'; type PrometheusDataTypeLiteral = | 'counter' @@ -177,6 +178,16 @@ export class PrometheusSerializer { serializeRecord(name: string, record: MetricRecord): string { let results = ''; + + if ( + record.descriptor.metricKind === MetricKind.COUNTER && + !name.endsWith('_total') + ) { + diag.debug( + `The metric ${name} of kind Counter is missing the mandatory _total as suffix` + ); + } + switch (record.aggregator.kind) { case AggregatorKind.SUM: case AggregatorKind.LAST_VALUE: { From 39e302c4ae03bb056591f39b3d423a0b97beb559 Mon Sep 17 00:00:00 2001 From: Weyert de Boer Date: Mon, 19 Apr 2021 18:29:02 +0100 Subject: [PATCH 2/7] test: add unit test to check the warn log message is shown --- .../src/PrometheusSerializer.ts | 4 +- .../test/PrometheusSerializer.test.ts | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts b/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts index b6c371c2b11..09999a34f34 100644 --- a/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts +++ b/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts @@ -183,9 +183,7 @@ export class PrometheusSerializer { record.descriptor.metricKind === MetricKind.COUNTER && !name.endsWith('_total') ) { - diag.debug( - `The metric ${name} of kind Counter is missing the mandatory _total as suffix` - ); + diag.debug(`Counter ${name} is missing the mandatory _total suffix`); } switch (record.aggregator.kind) { diff --git a/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts b/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts index 4495154f921..1add9fbfd9b 100644 --- a/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts +++ b/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts @@ -23,6 +23,7 @@ import { UpDownCounterMetric, ValueObserverMetric, } from '@opentelemetry/metrics'; +import { diag, DiagLogLevel } from '@opentelemetry/api'; import * as assert from 'assert'; import { Labels } from '@opentelemetry/api-metrics'; import { PrometheusSerializer } from '../src/PrometheusSerializer'; @@ -370,6 +371,70 @@ describe('PrometheusSerializer', () => { }); }); + describe('validate against metric conventions', () => { + mockAggregator(SumAggregator); + + it('should warn for counter metrics with wrong name', async () => { + let calledArgs: any[] = []; + const dummyLogger = { + verbose: () => {}, + debug: (...args: any[]) => { + calledArgs = args; + }, + info: () => {}, + warn: () => {}, + error: () => {}, + }; + diag.setLogger(dummyLogger, DiagLogLevel.ALL); + const serializer = new PrometheusSerializer(); + + const meter = new MeterProvider({ + processor: new ExactProcessor(SumAggregator), + }).getMeter('test'); + const counter = meter.createCounter('test') as CounterMetric; + counter.bind({}).add(1); + + const records = await counter.getMetricRecord(); + const record = records[0]; + + const result = serializer.serializeRecord(record.descriptor.name, record); + assert.strictEqual(result, `test 1 ${mockedHrTimeMs}\n`); + assert.ok( + calledArgs.includes( + 'Counter test is missing the mandatory _total suffix' + ) + ); + }); + + it('should not warn for counter metrics with correct name', async () => { + let calledArgs: any[] = []; + const dummyLogger = { + verbose: () => {}, + debug: (...args: any[]) => { + calledArgs = args; + }, + info: () => {}, + warn: () => {}, + error: () => {}, + }; + diag.setLogger(dummyLogger, DiagLogLevel.ALL); + const serializer = new PrometheusSerializer(); + + const meter = new MeterProvider({ + processor: new ExactProcessor(SumAggregator), + }).getMeter('test'); + const counter = meter.createCounter('test_total') as CounterMetric; + counter.bind({}).add(1); + + const records = await counter.getMetricRecord(); + const record = records[0]; + + const result = serializer.serializeRecord(record.descriptor.name, record); + assert.strictEqual(result, `test_total 1 ${mockedHrTimeMs}\n`); + assert.ok(calledArgs.length === 0); + }); + }); + describe('serialize non-normalized values', () => { describe('with SumAggregator', () => { mockAggregator(SumAggregator); From 0a122a351d2e49a67f5e4d00124863e5ea37aaea Mon Sep 17 00:00:00 2001 From: Weyert de Boer Date: Tue, 20 Apr 2021 15:57:22 +0100 Subject: [PATCH 3/7] feat: automatically append `_total` to counter metric when its missing from name --- .../src/PrometheusSerializer.ts | 11 +++- .../test/PrometheusExporter.test.ts | 28 ++++----- .../test/PrometheusSerializer.test.ts | 63 +++++++------------ 3 files changed, 47 insertions(+), 55 deletions(-) diff --git a/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts b/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts index 09999a34f34..11e59bbe9b1 100644 --- a/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts +++ b/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts @@ -21,7 +21,6 @@ import { import { PrometheusCheckpoint } from './types'; import { Labels } from '@opentelemetry/api-metrics'; import { hrTimeToMilliseconds } from '@opentelemetry/core'; -import { diag } from '@opentelemetry/api'; type PrometheusDataTypeLiteral = | 'counter' @@ -161,6 +160,14 @@ export class PrometheusSerializer { if (this._prefix) { name = `${this._prefix}${name}`; } + + if ( + !name.endsWith('_total') && + checkpoint.descriptor.metricKind === MetricKind.COUNTER + ) { + name = name + '_total'; + } + const help = `# HELP ${name} ${escapeString( checkpoint.descriptor.description || 'description missing' )}`; @@ -183,7 +190,7 @@ export class PrometheusSerializer { record.descriptor.metricKind === MetricKind.COUNTER && !name.endsWith('_total') ) { - diag.debug(`Counter ${name} is missing the mandatory _total suffix`); + name = name + '_total'; } switch (record.aggregator.kind) { diff --git a/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts b/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts index 6954fd29548..5abdbf7984d 100644 --- a/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts +++ b/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts @@ -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', }); @@ -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}`, '', ]); @@ -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}`, '', ]); @@ -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}`, '', ]); @@ -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', '', ]); diff --git a/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts b/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts index 1add9fbfd9b..03c7457b706 100644 --- a/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts +++ b/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts @@ -54,7 +54,7 @@ describe('PrometheusSerializer', () => { const meter = new MeterProvider({ processor: new ExactProcessor(SumAggregator), }).getMeter('test'); - const counter = meter.createCounter('test') as CounterMetric; + const counter = meter.createCounter('test_total') as CounterMetric; counter.bind(labels).add(1); const records = await counter.getMetricRecord(); @@ -66,7 +66,7 @@ describe('PrometheusSerializer', () => { ); assert.strictEqual( result, - `test{foo1="bar1",foo2="bar2"} 1 ${mockedHrTimeMs}\n` + `test_total{foo1="bar1",foo2="bar2"} 1 ${mockedHrTimeMs}\n` ); }); @@ -76,7 +76,7 @@ describe('PrometheusSerializer', () => { const meter = new MeterProvider({ processor: new ExactProcessor(SumAggregator), }).getMeter('test'); - const counter = meter.createCounter('test') as CounterMetric; + const counter = meter.createCounter('test_total') as CounterMetric; counter.bind(labels).add(1); const records = await counter.getMetricRecord(); @@ -86,7 +86,7 @@ describe('PrometheusSerializer', () => { record.descriptor.name, record ); - assert.strictEqual(result, 'test{foo1="bar1",foo2="bar2"} 1\n'); + assert.strictEqual(result, 'test_total{foo1="bar1",foo2="bar2"} 1\n'); }); }); @@ -156,6 +156,7 @@ describe('PrometheusSerializer', () => { const recorder = meter.createValueRecorder('test', { description: 'foobar', }) as ValueRecorderMetric; + recorder.bind(labels).record(5); const records = await recorder.getMetricRecord(); @@ -245,7 +246,7 @@ describe('PrometheusSerializer', () => { processor: new ExactProcessor(SumAggregator), }).getMeter('test'); const processor = new PrometheusLabelsBatcher(); - const counter = meter.createCounter('test', { + const counter = meter.createCounter('test_total', { description: 'foobar', }) as CounterMetric; counter.bind({ val: '1' }).add(1); @@ -258,10 +259,10 @@ describe('PrometheusSerializer', () => { const result = serializer.serialize(checkPointSet); assert.strictEqual( result, - '# HELP test foobar\n' + - '# TYPE test counter\n' + - `test{val="1"} 1 ${mockedHrTimeMs}\n` + - `test{val="2"} 1 ${mockedHrTimeMs}\n` + '# HELP test_total foobar\n' + + '# TYPE test_total counter\n' + + `test_total{val="1"} 1 ${mockedHrTimeMs}\n` + + `test_total{val="2"} 1 ${mockedHrTimeMs}\n` ); }); @@ -272,7 +273,7 @@ describe('PrometheusSerializer', () => { processor: new ExactProcessor(SumAggregator), }).getMeter('test'); const processor = new PrometheusLabelsBatcher(); - const counter = meter.createCounter('test', { + const counter = meter.createCounter('test_total', { description: 'foobar', }) as CounterMetric; counter.bind({ val: '1' }).add(1); @@ -285,10 +286,10 @@ describe('PrometheusSerializer', () => { const result = serializer.serialize(checkPointSet); assert.strictEqual( result, - '# HELP test foobar\n' + - '# TYPE test counter\n' + - 'test{val="1"} 1\n' + - 'test{val="2"} 1\n' + '# HELP test_total foobar\n' + + '# TYPE test_total counter\n' + + 'test_total{val="1"} 1\n' + + 'test_total{val="2"} 1\n' ); }); }); @@ -374,36 +375,20 @@ describe('PrometheusSerializer', () => { describe('validate against metric conventions', () => { mockAggregator(SumAggregator); - it('should warn for counter metrics with wrong name', async () => { - let calledArgs: any[] = []; - const dummyLogger = { - verbose: () => {}, - debug: (...args: any[]) => { - calledArgs = args; - }, - info: () => {}, - warn: () => {}, - error: () => {}, - }; - diag.setLogger(dummyLogger, DiagLogLevel.ALL); + it('should rename metric of type counter when name misses _counter suffix', async () => { const serializer = new PrometheusSerializer(); const meter = new MeterProvider({ processor: new ExactProcessor(SumAggregator), }).getMeter('test'); - const counter = meter.createCounter('test') as CounterMetric; + const counter = meter.createCounter('test_total') as CounterMetric; counter.bind({}).add(1); const records = await counter.getMetricRecord(); const record = records[0]; const result = serializer.serializeRecord(record.descriptor.name, record); - assert.strictEqual(result, `test 1 ${mockedHrTimeMs}\n`); - assert.ok( - calledArgs.includes( - 'Counter test is missing the mandatory _total suffix' - ) - ); + assert.strictEqual(result, `test_total 1 ${mockedHrTimeMs}\n`); }); it('should not warn for counter metrics with correct name', async () => { @@ -445,7 +430,7 @@ describe('PrometheusSerializer', () => { const meter = new MeterProvider({ processor: new ExactProcessor(SumAggregator), }).getMeter('test'); - const counter = meter.createCounter('test') as CounterMetric; + const counter = meter.createCounter('test_total') as CounterMetric; counter.bind({}).add(1); const records = await counter.getMetricRecord(); @@ -455,7 +440,7 @@ describe('PrometheusSerializer', () => { record.descriptor.name, record ); - assert.strictEqual(result, `test 1 ${mockedHrTimeMs}\n`); + assert.strictEqual(result, `test_total 1 ${mockedHrTimeMs}\n`); }); it('should serialize non-string label values', async () => { @@ -464,7 +449,7 @@ describe('PrometheusSerializer', () => { const meter = new MeterProvider({ processor: new ExactProcessor(SumAggregator), }).getMeter('test'); - const counter = meter.createCounter('test') as CounterMetric; + const counter = meter.createCounter('test_total') as CounterMetric; counter .bind(({ object: {}, @@ -482,7 +467,7 @@ describe('PrometheusSerializer', () => { ); assert.strictEqual( result, - `test{object="[object Object]",NaN="NaN",null="null",undefined="undefined"} 1 ${mockedHrTimeMs}\n` + `test_total{object="[object Object]",NaN="NaN",null="null",undefined="undefined"} 1 ${mockedHrTimeMs}\n` ); }); @@ -522,7 +507,7 @@ describe('PrometheusSerializer', () => { const meter = new MeterProvider({ processor: new ExactProcessor(SumAggregator), }).getMeter('test'); - const counter = meter.createCounter('test') as CounterMetric; + const counter = meter.createCounter('test_total') as CounterMetric; counter .bind(({ backslash: '\u005c', // \ => \\ (\u005c\u005c) @@ -542,7 +527,7 @@ describe('PrometheusSerializer', () => { ); assert.strictEqual( result, - 'test{' + + 'test_total{' + 'backslash="\u005c\u005c",' + 'doubleQuote="\u005c\u0022",' + 'lineFeed="\u005c\u006e",' + From eb46772fdb17f3ae4a0609074b60b192fb77a493 Mon Sep 17 00:00:00 2001 From: Weyert de Boer Date: Tue, 20 Apr 2021 15:59:36 +0100 Subject: [PATCH 4/7] test: improve _total suffix test case --- .../test/PrometheusSerializer.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts b/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts index 03c7457b706..49126c133a5 100644 --- a/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts +++ b/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts @@ -375,13 +375,13 @@ describe('PrometheusSerializer', () => { describe('validate against metric conventions', () => { mockAggregator(SumAggregator); - it('should rename metric of type counter when name misses _counter suffix', async () => { + it('should rename metric of type counter when name misses _total suffix', async () => { const serializer = new PrometheusSerializer(); const meter = new MeterProvider({ processor: new ExactProcessor(SumAggregator), }).getMeter('test'); - const counter = meter.createCounter('test_total') as CounterMetric; + const counter = meter.createCounter('test') as CounterMetric; counter.bind({}).add(1); const records = await counter.getMetricRecord(); From cf2e43378a9e30f539210f1ef5c00a7763d85ea5 Mon Sep 17 00:00:00 2001 From: Weyert de Boer Date: Wed, 21 Apr 2021 16:13:38 +0100 Subject: [PATCH 5/7] refactor: move the logic for checking metric name to its own function --- .../src/PrometheusSerializer.ts | 44 ++++++++++++++----- .../test/PrometheusSerializer.test.ts | 4 +- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts b/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts index a4c69173b43..786d5fa07f5 100644 --- a/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts +++ b/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts @@ -63,6 +63,28 @@ 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 { + // If the metric kind is Counter if so ensure that the name has the suffix + // _total or add it when missing + if (!name.endsWith('_total') && kind === MetricKind.COUNTER) { + name = name + '_total'; + } + + return name; +} + function valueString(value: number) { if (Number.isNaN(value)) { return 'Nan'; @@ -163,12 +185,11 @@ export class PrometheusSerializer { name = `${this._prefix}${name}`; } - if ( - !name.endsWith('_total') && - checkpoint.descriptor.metricKind === MetricKind.COUNTER - ) { - name = name + '_total'; - } + // Prometheus requires that metrics of the Counter kind have _total suffix + name = enforcePrometheusNamingConvention( + name, + checkpoint.descriptor.metricKind + ); const help = `# HELP ${name} ${escapeString( checkpoint.descriptor.description || 'description missing' @@ -188,12 +209,11 @@ export class PrometheusSerializer { serializeRecord(name: string, record: MetricRecord): string { let results = ''; - if ( - record.descriptor.metricKind === MetricKind.COUNTER && - !name.endsWith('_total') - ) { - name = name + '_total'; - } + // Prometheus requires that metrics of the Counter kind have _total suffix + name = enforcePrometheusNamingConvention( + name, + record.descriptor.metricKind + ); switch (record.aggregator.kind) { case AggregatorKind.SUM: diff --git a/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts b/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts index 9c95729f855..07d240ff42d 100644 --- a/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts +++ b/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts @@ -543,7 +543,7 @@ describe('PrometheusSerializer', () => { const meter = new MeterProvider({ processor: new ExactProcessor(SumAggregator), - }).getMeter('test'); + }).getMeter('test_total'); const counter = meter.createCounter('test') as CounterMetric; // if you try to use a label name like account-id prometheus will complain // with an error like: @@ -562,7 +562,7 @@ describe('PrometheusSerializer', () => { ); assert.strictEqual( result, - `test{account_id="123456"} 1 ${mockedHrTimeMs}\n` + `test_total{account_id="123456"} 1 ${mockedHrTimeMs}\n` ); }); }); From d2122b0e2b495a8fb14575cdf410ba4d791ffc48 Mon Sep 17 00:00:00 2001 From: Weyert de Boer Date: Thu, 22 Apr 2021 11:08:07 +0100 Subject: [PATCH 6/7] style: reordered comments --- .../src/PrometheusSerializer.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts b/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts index 786d5fa07f5..a05e25abe5a 100644 --- a/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts +++ b/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts @@ -76,8 +76,7 @@ function enforcePrometheusNamingConvention( name: string, kind: MetricKind ): string { - // If the metric kind is Counter if so ensure that the name has the suffix - // _total or add it when missing + // Prometheus requires that metrics of the Counter kind have "_total" suffix if (!name.endsWith('_total') && kind === MetricKind.COUNTER) { name = name + '_total'; } @@ -185,7 +184,6 @@ export class PrometheusSerializer { name = `${this._prefix}${name}`; } - // Prometheus requires that metrics of the Counter kind have _total suffix name = enforcePrometheusNamingConvention( name, checkpoint.descriptor.metricKind @@ -209,7 +207,6 @@ export class PrometheusSerializer { serializeRecord(name: string, record: MetricRecord): string { let results = ''; - // Prometheus requires that metrics of the Counter kind have _total suffix name = enforcePrometheusNamingConvention( name, record.descriptor.metricKind From 7885ef2fed5b171dbc8855c21fb6f486be14be5a Mon Sep 17 00:00:00 2001 From: Weyert de Boer Date: Thu, 22 Apr 2021 11:15:16 +0100 Subject: [PATCH 7/7] test: fix the naming of counter metrics in the tests --- .../test/PrometheusExporter.test.ts | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts b/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts index 5abdbf7984d..29527736f13 100644 --- a/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts +++ b/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts @@ -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; @@ -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}`, '', ]); @@ -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; @@ -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}`, '', ]); @@ -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); @@ -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}`, '', ]); @@ -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}`, '', ]); @@ -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}`, '', ]);