diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 06491883f5c..a45f89af667 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -18,6 +18,7 @@ All notable changes to experimental packages in this project will be documented * fix(instrumentation-grpc): always set grpc semcov status code attribute with numeric value [#3076](/~https://github.com/open-telemetry/opentelemetry-js/pull/3076) @blumamir * fix(instrumentation): only call `onRequire` for full matches on core modules with sub-paths [#3451](/~https://github.com/open-telemetry/opentelemetry-js/pull/3451) @mhassan1 * fix(instrumentation): add back support for absolute paths via `require-in-the-middle` [#3457](/~https://github.com/open-telemetry/opentelemetry-js/pull/3457) @mhassan1 +* fix(prometheus-sanitization): replace repeated `_` with a single `_` [3470](/~https://github.com/open-telemetry/opentelemetry-js/pull/3470) @samimusallam ### :books: (Refine Doc) diff --git a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts index 8df7e7a56be..54152a4920f 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts @@ -56,6 +56,7 @@ function escapeAttributeValue(str: MetricAttributeValue = '') { } const invalidCharacterRegex = /[^a-z0-9_]/gi; +const multipleUnderscoreRegex = /_{2,}/g; /** * Ensures metric names are valid Prometheus metric names by removing @@ -76,7 +77,10 @@ const invalidCharacterRegex = /[^a-z0-9_]/gi; * @param name name to be sanitized */ function sanitizePrometheusMetricName(name: string): string { - return name.replace(invalidCharacterRegex, '_'); // replace all invalid characters with '_' + // replace all invalid characters with '_' + return name + .replace(invalidCharacterRegex, '_') + .replace(multipleUnderscoreRegex, '_'); } /** diff --git a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts index c7050140fe3..0b0365b5e7d 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts @@ -374,7 +374,7 @@ describe('PrometheusExporter', () => { }); it('should sanitize names', async () => { - const counter = meter.createCounter('counter.bad-name'); + const counter = meter.createCounter('counter..bad-name'); counter.add(10, { key1: 'attributeValue1' });