Skip to content

Commit e3a81d2

Browse files
fix(prometheus-sanitization): replace repeated _ with a single _ (#3470)
* fix(prometheus-sanitization): fix multiple underscores * fix(prometheus-sanitization): updated test to check for multiple underscores as well * fix(prometheus-sanitization): updated CHANGELOG.md Co-authored-by: Daniel Dyla <[email protected]>
1 parent e70cdeb commit e3a81d2

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

experimental/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ All notable changes to experimental packages in this project will be documented
1818
* fix(instrumentation-grpc): always set grpc semcov status code attribute with numeric value [#3076](https://github.com/open-telemetry/opentelemetry-js/pull/3076) @blumamir
1919
* 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
2020
* fix(instrumentation): add back support for absolute paths via `require-in-the-middle` [#3457](https://github.com/open-telemetry/opentelemetry-js/pull/3457) @mhassan1
21+
* fix(prometheus-sanitization): replace repeated `_` with a single `_` [3470](https://github.com/open-telemetry/opentelemetry-js/pull/3470) @samimusallam
2122

2223
### :books: (Refine Doc)
2324

experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function escapeAttributeValue(str: MetricAttributeValue = '') {
5656
}
5757

5858
const invalidCharacterRegex = /[^a-z0-9_]/gi;
59+
const multipleUnderscoreRegex = /_{2,}/g;
5960

6061
/**
6162
* Ensures metric names are valid Prometheus metric names by removing
@@ -76,7 +77,10 @@ const invalidCharacterRegex = /[^a-z0-9_]/gi;
7677
* @param name name to be sanitized
7778
*/
7879
function sanitizePrometheusMetricName(name: string): string {
79-
return name.replace(invalidCharacterRegex, '_'); // replace all invalid characters with '_'
80+
// replace all invalid characters with '_'
81+
return name
82+
.replace(invalidCharacterRegex, '_')
83+
.replace(multipleUnderscoreRegex, '_');
8084
}
8185

8286
/**

experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ describe('PrometheusExporter', () => {
374374
});
375375

376376
it('should sanitize names', async () => {
377-
const counter = meter.createCounter('counter.bad-name');
377+
const counter = meter.createCounter('counter..bad-name');
378378

379379
counter.add(10, { key1: 'attributeValue1' });
380380

0 commit comments

Comments
 (0)