Skip to content

Commit

Permalink
fix(prometheus-sanitization): replace repeated _ with a single _ (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
samimusallam and dyladan authored Dec 8, 2022
1 parent e70cdeb commit e3a81d2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:/open-telemetry/opentelemetry-js/pull/3076) @blumamir
* fix(instrumentation): only call `onRequire` for full matches on core modules with sub-paths [#3451](https:/open-telemetry/opentelemetry-js/pull/3451) @mhassan1
* fix(instrumentation): add back support for absolute paths via `require-in-the-middle` [#3457](https:/open-telemetry/opentelemetry-js/pull/3457) @mhassan1
* fix(prometheus-sanitization): replace repeated `_` with a single `_` [3470](https:/open-telemetry/opentelemetry-js/pull/3470) @samimusallam

### :books: (Refine Doc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, '_');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' });

Expand Down

0 comments on commit e3a81d2

Please sign in to comment.