diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 2666d4f186f..0c1a23dd77c 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -47,6 +47,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2 * feat(opentelemetry-configuration): parse more parameters from config file [#5955](https://github.com/open-telemetry/opentelemetry-js/pull/5955) @maryliag * feat(exporter-prometheus): support withoutTargetInfo option [#5962](https://github.com/open-telemetry/opentelemetry-js/pull/5962) @cjihrig * feat(opentelemetry-configuration): parse trace provider from config file [#5992](https://github.com/open-telemetry/opentelemetry-js/pull/5992) @maryliag +* feat(exporter-prometheus): support withoutScopeInfo option [#5993](https://github.com/open-telemetry/opentelemetry-js/pull/5993) @cjihrig ### :bug: Bug Fixes diff --git a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts index c7cd9e77287..56f057d61cb 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts @@ -35,6 +35,7 @@ export class PrometheusExporter extends MetricReader { prefix: '', appendTimestamp: false, withResourceConstantLabels: undefined, + withoutScopeInfo: false, withoutTargetInfo: false, }; @@ -87,6 +88,9 @@ export class PrometheusExporter extends MetricReader { const _withResourceConstantLabels = config.withResourceConstantLabels || PrometheusExporter.DEFAULT_OPTIONS.withResourceConstantLabels; + const _withoutScopeInfo = + config.withoutScopeInfo || + PrometheusExporter.DEFAULT_OPTIONS.withoutScopeInfo; const _withoutTargetInfo = config.withoutTargetInfo || PrometheusExporter.DEFAULT_OPTIONS.withoutTargetInfo; @@ -96,7 +100,8 @@ export class PrometheusExporter extends MetricReader { this._prefix, this._appendTimestamp, _withResourceConstantLabels, - _withoutTargetInfo + _withoutTargetInfo, + _withoutScopeInfo ); this._baseUrl = `http://${this._host}:${this._port}/`; diff --git a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts index d967e65cdfc..a9a18db351d 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts @@ -23,8 +23,18 @@ import { DataPoint, Histogram, } from '@opentelemetry/sdk-metrics'; -import { hrTimeToMilliseconds } from '@opentelemetry/core'; +import { + InstrumentationScope, + hrTimeToMilliseconds, +} from '@opentelemetry/core'; import { Resource } from '@opentelemetry/resources'; +import { + ATTR_OTEL_SCOPE_NAME, + ATTR_OTEL_SCOPE_VERSION, +} from '@opentelemetry/semantic-conventions'; + +// This is currently listed as experimental. +const ATTR_OTEL_SCOPE_SCHEMA_URL = 'otel.scope.schema_url'; type PrometheusDataTypeLiteral = | 'counter' @@ -173,19 +183,22 @@ export class PrometheusSerializer { private _appendTimestamp: boolean; private _additionalAttributes: Attributes | undefined; private _withResourceConstantLabels: RegExp | undefined; + private _withoutScopeInfo: boolean | undefined; private _withoutTargetInfo: boolean | undefined; constructor( prefix?: string, appendTimestamp = false, withResourceConstantLabels?: RegExp, - withoutTargetInfo?: boolean + withoutTargetInfo?: boolean, + withoutScopeInfo?: boolean ) { if (prefix) { this._prefix = prefix + '_'; } this._appendTimestamp = appendTimestamp; this._withResourceConstantLabels = withResourceConstantLabels; + this._withoutScopeInfo = !!withoutScopeInfo; this._withoutTargetInfo = !!withoutTargetInfo; } @@ -227,12 +240,15 @@ export class PrometheusSerializer { private _serializeScopeMetrics(scopeMetrics: ScopeMetrics) { let str = ''; for (const metric of scopeMetrics.metrics) { - str += this._serializeMetricData(metric) + '\n'; + str += this._serializeMetricData(metric, scopeMetrics.scope) + '\n'; } return str; } - private _serializeMetricData(metricData: MetricData) { + private _serializeMetricData( + metricData: MetricData, + scope: InstrumentationScope + ) { let name = sanitizePrometheusMetricName( escapeString(metricData.descriptor.name) ); @@ -250,19 +266,47 @@ export class PrometheusSerializer { ? `\n# UNIT ${name} ${escapeString(metricData.descriptor.unit)}` : ''; const type = `# TYPE ${name} ${toPrometheusType(metricData)}`; + let additionalAttributes: Attributes | undefined; + + if (this._withoutScopeInfo) { + additionalAttributes = this._additionalAttributes; + } else { + additionalAttributes = Object.assign( + { + [ATTR_OTEL_SCOPE_NAME]: scope.name, + [ATTR_OTEL_SCOPE_SCHEMA_URL]: scope.schemaUrl ?? '', + [ATTR_OTEL_SCOPE_VERSION]: scope.version ?? '', + }, + this._additionalAttributes + ); + } let results = ''; switch (dataPointType) { case DataPointType.SUM: case DataPointType.GAUGE: { results = metricData.dataPoints - .map(it => this._serializeSingularDataPoint(name, metricData, it)) + .map(it => + this._serializeSingularDataPoint( + name, + metricData, + it, + additionalAttributes + ) + ) .join(''); break; } case DataPointType.HISTOGRAM: { results = metricData.dataPoints - .map(it => this._serializeHistogramDataPoint(name, metricData, it)) + .map(it => + this._serializeHistogramDataPoint( + name, + metricData, + it, + additionalAttributes + ) + ) .join(''); break; } @@ -279,7 +323,8 @@ export class PrometheusSerializer { private _serializeSingularDataPoint( name: string, data: MetricData, - dataPoint: DataPoint + dataPoint: DataPoint, + additionalAttributes: Attributes | undefined ): string { let results = ''; @@ -291,7 +336,7 @@ export class PrometheusSerializer { attributes, value, this._appendTimestamp ? timestamp : undefined, - this._additionalAttributes + additionalAttributes ); return results; } @@ -299,7 +344,8 @@ export class PrometheusSerializer { private _serializeHistogramDataPoint( name: string, data: MetricData, - dataPoint: DataPoint + dataPoint: DataPoint, + additionalAttributes: Attributes | undefined ): string { let results = ''; @@ -316,7 +362,7 @@ export class PrometheusSerializer { attributes, value, this._appendTimestamp ? timestamp : undefined, - this._additionalAttributes + additionalAttributes ); } @@ -343,7 +389,7 @@ export class PrometheusSerializer { attributes, cumulativeSum, this._appendTimestamp ? timestamp : undefined, - Object.assign({}, this._additionalAttributes ?? {}, { + Object.assign({}, additionalAttributes, { le: upperBound === undefined || upperBound === Infinity ? '+Inf' diff --git a/experimental/packages/opentelemetry-exporter-prometheus/src/export/types.ts b/experimental/packages/opentelemetry-exporter-prometheus/src/export/types.ts index 82420aa09e1..88e4cc1ec72 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/src/export/types.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/src/export/types.ts @@ -75,6 +75,12 @@ export interface ExporterConfig { */ withResourceConstantLabels?: RegExp; + /** + * If true, scope labels are not included in scraped metrics. + * @default false (scope labels are included) + */ + withoutScopeInfo?: boolean; + /** * If true, the target_info metric is not included in scraped metrics. * @default false (target_info metric is included) diff --git a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts index 421e0403481..9262b3d185e 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts @@ -234,7 +234,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP counter_total a test description', '# TYPE counter_total counter', - 'counter_total{key1="attributeValue1"} 10', + 'counter_total{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1"} 10', '', ]); }); @@ -264,7 +264,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP metric_observable_gauge a test description', '# TYPE metric_observable_gauge gauge', - 'metric_observable_gauge{pid="123",core="1"} 0.999', + 'metric_observable_gauge{pid="123",core="1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1"} 0.999', '', ]); }); @@ -284,8 +284,8 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP counter_total a test description', '# TYPE counter_total counter', - 'counter_total{counterKey1="attributeValue1"} 10', - 'counter_total{counterKey1="attributeValue2"} 20', + 'counter_total{counterKey1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1"} 10', + 'counter_total{counterKey1="attributeValue2",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1"} 20', '', ]); }); @@ -327,7 +327,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP counter_total description missing', '# TYPE counter_total counter', - 'counter_total{key1="attributeValue1"} 10', + 'counter_total{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1"} 10', '', ]); }); @@ -344,7 +344,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP counter_bad_name_total description missing', '# TYPE counter_bad_name_total counter', - 'counter_bad_name_total{key1="attributeValue1"} 10', + 'counter_bad_name_total{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1"} 10', '', ]); }); @@ -362,7 +362,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP counter a test description', '# TYPE counter gauge', - 'counter{key1="attributeValue1"} 20', + 'counter{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1"} 20', '', ]); }); @@ -391,7 +391,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP metric_observable_counter_total a test description', '# TYPE metric_observable_counter_total counter', - 'metric_observable_counter_total{key1="attributeValue1"} 20', + 'metric_observable_counter_total{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1"} 20', '', ]); }); @@ -422,7 +422,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP metric_observable_up_down_counter a test description', '# TYPE metric_observable_up_down_counter gauge', - 'metric_observable_up_down_counter{key1="attributeValue1"} 20', + 'metric_observable_up_down_counter{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1"} 20', '', ]); }); @@ -441,24 +441,24 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP test_histogram a test description', '# TYPE test_histogram histogram', - 'test_histogram_count{key1="attributeValue1"} 1', - 'test_histogram_sum{key1="attributeValue1"} 20', - 'test_histogram_bucket{key1="attributeValue1",le="0"} 0', - 'test_histogram_bucket{key1="attributeValue1",le="5"} 0', - 'test_histogram_bucket{key1="attributeValue1",le="10"} 0', - 'test_histogram_bucket{key1="attributeValue1",le="25"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="50"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="75"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="100"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="250"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="500"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="750"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="1000"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="2500"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="5000"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="7500"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="10000"} 1', - 'test_histogram_bucket{key1="attributeValue1",le="+Inf"} 1', + 'test_histogram_count{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1"} 1', + 'test_histogram_sum{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1"} 20', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="0"} 0', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="5"} 0', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="10"} 0', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="25"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="50"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="75"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="100"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="250"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="500"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="750"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="1000"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="2500"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="5000"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="7500"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="10000"} 1', + 'test_histogram_bucket{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="1",le="+Inf"} 1', '', ]); }); @@ -496,7 +496,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP test_prefix_counter_total description missing', '# TYPE test_prefix_counter_total counter', - 'test_prefix_counter_total{key1="attributeValue1"} 10', + 'test_prefix_counter_total{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version=""} 10', '', ]); }); @@ -514,7 +514,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP counter_total description missing', '# TYPE counter_total counter', - 'counter_total{key1="attributeValue1"} 10', + 'counter_total{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version=""} 10', '', ]); }); @@ -532,7 +532,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP counter_total description missing', '# TYPE counter_total counter', - 'counter_total{key1="attributeValue1"} 10', + 'counter_total{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version=""} 10', '', ]); }); @@ -549,7 +549,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP counter_total description missing', '# TYPE counter_total counter', - `counter_total{key1="attributeValue1"} 10 ${mockedHrTimeMs}`, + `counter_total{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version=""} 10 ${mockedHrTimeMs}`, '', ]); }); @@ -566,7 +566,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP counter_total description missing', '# TYPE counter_total counter', - `counter_total{key1="attributeValue1",service_name="${serviceName}",telemetry_sdk_language="${sdkLanguage}",telemetry_sdk_name="${sdkName}",telemetry_sdk_version="${sdkVersion}"} 10`, + `counter_total{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="",service_name="${serviceName}",telemetry_sdk_language="${sdkLanguage}",telemetry_sdk_name="${sdkName}",telemetry_sdk_version="${sdkVersion}"} 10`, '', ]); }); @@ -583,7 +583,7 @@ describe('PrometheusExporter', () => { ...serializedDefaultResourceLines, '# HELP counter_total description missing', '# TYPE counter_total counter', - `counter_total{key1="attributeValue1",telemetry_sdk_language="${sdkLanguage}",telemetry_sdk_name="${sdkName}"} 10`, + `counter_total{key1="attributeValue1",otel_scope_name="test-prometheus",otel_scope_schema_url="",otel_scope_version="",telemetry_sdk_language="${sdkLanguage}",telemetry_sdk_name="${sdkName}"} 10`, '', ]); }); @@ -595,6 +595,21 @@ describe('PrometheusExporter', () => { assert.deepStrictEqual(body.includes('target_info'), false); }); + + it('should omit scope labels if withoutScopeInfo is true', async () => { + exporter = new PrometheusExporter({ withoutScopeInfo: true }); + setup(exporter); + const body = await request('http://localhost:9464/metrics'); + const lines = body.split('\n'); + + assert.deepStrictEqual(lines, [ + ...serializedDefaultResourceLines, + '# HELP counter_total description missing', + '# TYPE counter_total counter', + `counter_total{key1="attributeValue1"} 10`, + '', + ]); + }); }); }); diff --git a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts index 2d025cd3155..27ca307e38b 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts @@ -114,7 +114,8 @@ describe('PrometheusSerializer', () => { const result = serializer['_serializeSingularDataPoint']( metric.descriptor.name, metric, - pointData[0] + pointData[0], + serializer['_additionalAttributes'] ); return result; } @@ -173,7 +174,8 @@ describe('PrometheusSerializer', () => { const result = serializer['_serializeHistogramDataPoint']( metric.descriptor.name, metric, - pointData[0] + pointData[0], + serializer['_additionalAttributes'] ); return result; } @@ -264,8 +266,8 @@ describe('PrometheusSerializer', () => { result, '# HELP test_total foobar\n' + '# TYPE test_total counter\n' + - 'test_total{val="1"} 1\n' + - 'test_total{val="2"} 1\n' + 'test_total{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1\n' + + 'test_total{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1\n' ); }); @@ -276,8 +278,8 @@ describe('PrometheusSerializer', () => { result, '# HELP test_total foobar\n' + '# TYPE test_total counter\n' + - `test_total{val="1"} 1 ${mockedHrTimeMs}\n` + - `test_total{val="2"} 1 ${mockedHrTimeMs}\n` + `test_total{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1 ${mockedHrTimeMs}\n` + + `test_total{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1 ${mockedHrTimeMs}\n` ); }); @@ -288,8 +290,8 @@ describe('PrometheusSerializer', () => { result, '# HELP test_total foobar\n' + '# TYPE test_total counter\n' + - `test_total{val="1",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` + - `test_total{val="2",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` + `test_total{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` + + `test_total{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` ); }); }); @@ -334,8 +336,8 @@ describe('PrometheusSerializer', () => { result, '# HELP test_total foobar\n' + '# TYPE test_total gauge\n' + - 'test_total{val="1"} 1\n' + - 'test_total{val="2"} 1\n' + 'test_total{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1\n' + + 'test_total{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1\n' ); }); @@ -346,8 +348,8 @@ describe('PrometheusSerializer', () => { result, '# HELP test_total foobar\n' + '# TYPE test_total gauge\n' + - `test_total{val="1"} 1 ${mockedHrTimeMs}\n` + - `test_total{val="2"} 1 ${mockedHrTimeMs}\n` + `test_total{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1 ${mockedHrTimeMs}\n` + + `test_total{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1 ${mockedHrTimeMs}\n` ); }); @@ -359,8 +361,8 @@ describe('PrometheusSerializer', () => { result, '# HELP test_total foobar\n' + '# TYPE test_total gauge\n' + - `test_total{val="1",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` + - `test_total{val="2",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` + `test_total{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` + + `test_total{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` ); }); }); @@ -407,8 +409,8 @@ describe('PrometheusSerializer', () => { result, '# HELP test_total foobar\n' + '# TYPE test_total gauge\n' + - 'test_total{val="1"} 1\n' + - 'test_total{val="2"} 1\n' + 'test_total{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1\n' + + 'test_total{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1\n' ); }); @@ -419,8 +421,8 @@ describe('PrometheusSerializer', () => { result, '# HELP test_total foobar\n' + '# TYPE test_total gauge\n' + - `test_total{val="1"} 1 ${mockedHrTimeMs}\n` + - `test_total{val="2"} 1 ${mockedHrTimeMs}\n` + `test_total{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1 ${mockedHrTimeMs}\n` + + `test_total{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1 ${mockedHrTimeMs}\n` ); }); @@ -431,8 +433,8 @@ describe('PrometheusSerializer', () => { result, '# HELP test_total foobar\n' + '# TYPE test_total gauge\n' + - `test_total{val="1",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` + - `test_total{val="2",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` + `test_total{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` + + `test_total{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes}} 1 ${mockedHrTimeMs}\n` ); }); }); @@ -484,18 +486,18 @@ describe('PrometheusSerializer', () => { result, '# HELP test foobar\n' + '# TYPE test histogram\n' + - 'test_count{val="1"} 3\n' + - 'test_sum{val="1"} 175\n' + - 'test_bucket{val="1",le="1"} 0\n' + - 'test_bucket{val="1",le="10"} 1\n' + - 'test_bucket{val="1",le="100"} 2\n' + - 'test_bucket{val="1",le="+Inf"} 3\n' + - 'test_count{val="2"} 1\n' + - 'test_sum{val="2"} 5\n' + - 'test_bucket{val="2",le="1"} 0\n' + - 'test_bucket{val="2",le="10"} 1\n' + - 'test_bucket{val="2",le="100"} 1\n' + - 'test_bucket{val="2",le="+Inf"} 1\n' + 'test_count{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 3\n' + + 'test_sum{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 175\n' + + 'test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="1"} 0\n' + + 'test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="10"} 1\n' + + 'test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="100"} 2\n' + + 'test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="+Inf"} 3\n' + + 'test_count{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1\n' + + 'test_sum{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 5\n' + + 'test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="1"} 0\n' + + 'test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="10"} 1\n' + + 'test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="100"} 1\n' + + 'test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="+Inf"} 1\n' ); }); @@ -506,18 +508,18 @@ describe('PrometheusSerializer', () => { result, '# HELP test foobar\n' + '# TYPE test histogram\n' + - `test_count{val="1",${resourceAttributes}} 3\n` + - `test_sum{val="1",${resourceAttributes}} 175\n` + - `test_bucket{val="1",${resourceAttributes},le="1"} 0\n` + - `test_bucket{val="1",${resourceAttributes},le="10"} 1\n` + - `test_bucket{val="1",${resourceAttributes},le="100"} 2\n` + - `test_bucket{val="1",${resourceAttributes},le="+Inf"} 3\n` + - `test_count{val="2",${resourceAttributes}} 1\n` + - `test_sum{val="2",${resourceAttributes}} 5\n` + - `test_bucket{val="2",${resourceAttributes},le="1"} 0\n` + - `test_bucket{val="2",${resourceAttributes},le="10"} 1\n` + - `test_bucket{val="2",${resourceAttributes},le="100"} 1\n` + - `test_bucket{val="2",${resourceAttributes},le="+Inf"} 1\n` + `test_count{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes}} 3\n` + + `test_sum{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes}} 175\n` + + `test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes},le="1"} 0\n` + + `test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes},le="10"} 1\n` + + `test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes},le="100"} 2\n` + + `test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes},le="+Inf"} 3\n` + + `test_count{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes}} 1\n` + + `test_sum{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes}} 5\n` + + `test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes},le="1"} 0\n` + + `test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes},le="10"} 1\n` + + `test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes},le="100"} 1\n` + + `test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",${resourceAttributes},le="+Inf"} 1\n` ); }); @@ -562,16 +564,16 @@ describe('PrometheusSerializer', () => { result, '# HELP test foobar\n' + '# TYPE test histogram\n' + - 'test_count{val="1"} 3\n' + - 'test_bucket{val="1",le="1"} 0\n' + - 'test_bucket{val="1",le="10"} 1\n' + - 'test_bucket{val="1",le="100"} 2\n' + - 'test_bucket{val="1",le="+Inf"} 3\n' + - 'test_count{val="2"} 1\n' + - 'test_bucket{val="2",le="1"} 0\n' + - 'test_bucket{val="2",le="10"} 1\n' + - 'test_bucket{val="2",le="100"} 1\n' + - 'test_bucket{val="2",le="+Inf"} 1\n' + 'test_count{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 3\n' + + 'test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="1"} 0\n' + + 'test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="10"} 1\n' + + 'test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="100"} 2\n' + + 'test_bucket{val="1",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="+Inf"} 3\n' + + 'test_count{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1\n' + + 'test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="1"} 0\n' + + 'test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="10"} 1\n' + + 'test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="100"} 1\n' + + 'test_bucket{val="2",otel_scope_name="test",otel_scope_schema_url="",otel_scope_version="",le="+Inf"} 1\n' ); }); }); @@ -619,7 +621,8 @@ describe('PrometheusSerializer', () => { const result = serializer['_serializeSingularDataPoint']( metric.descriptor.name, metric, - pointData[0] + pointData[0], + serializer['_additionalAttributes'] ); return result; } @@ -639,7 +642,7 @@ describe('PrometheusSerializer', () => { '# HELP test_total description missing\n' + `# UNIT test_total ${unitOfMetric}\n` + '# TYPE test_total counter\n' + - 'test_total 1\n' + 'test_total{otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1\n' ); }); @@ -654,7 +657,7 @@ describe('PrometheusSerializer', () => { serializedDefaultResource + '# HELP test_total description missing\n' + '# TYPE test_total counter\n' + - 'test_total 1\n' + 'test_total{otel_scope_name="test",otel_scope_schema_url="",otel_scope_version=""} 1\n' ); }); @@ -710,7 +713,8 @@ describe('PrometheusSerializer', () => { const result = serializer['_serializeSingularDataPoint']( metric.descriptor.name, metric, - pointData[0] + pointData[0], + serializer['_additionalAttributes'] ); return result; } @@ -850,5 +854,28 @@ describe('PrometheusSerializer', () => { assert.strictEqual(result.includes('target_info'), false); }); + + it('omits scope labels if withoutScopeInfo is true', async () => { + const serializer = new PrometheusSerializer( + undefined, + true, + undefined, + false, + true + ); + const result = serializer['_serializeResource']( + resourceFromAttributes({ + env: 'prod', + hostname: 'myhost', + datacenter: 'sdc', + region: 'europe', + owner: 'frontend', + }) + ); + + assert.strictEqual(result.includes('otel_scope_name'), false); + assert.strictEqual(result.includes('otel_scope_schema_url'), false); + assert.strictEqual(result.includes('otel_scope_version'), false); + }); }); });