Skip to content

Commit b48096f

Browse files
committed
add details about regex and improve name
1 parent 33395d6 commit b48096f

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

packages/core/src/metrics/constants.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,24 @@ export const GAUGE_METRIC_TYPE = 'g';
33
export const SET_METRIC_TYPE = 's';
44
export const DISTRIBUTION_METRIC_TYPE = 'd';
55

6-
export const NAME_AND_TAG_KEY_REGEX = /[^a-zA-Z0-9_/.-]+"/g;
7-
export const TAG_VALUE_REGEX = /[^\w\d_:/@.{}[\]$-]+/g;
6+
/**
7+
* Normalization regex for metric names and metric tag names.
8+
*
9+
* This enforces that names and tag keys only contain alphanumeric characters,
10+
* underscores, forward slashes, periods, and dashes.
11+
*
12+
* See: https://develop.sentry.dev/sdk/metrics/#normalization
13+
*/
14+
export const NAME_AND_TAG_KEY_NORMALIZATION_REGEX = /[^a-zA-Z0-9_/.-]+/g;
15+
16+
/**
17+
* Normalization regex for metric tag balues.
18+
*
19+
* This enforces that values only contain words, digits, or the following
20+
* special characters: _:/@.{}[\]$-
21+
*
22+
* See: https://develop.sentry.dev/sdk/metrics/#normalization
23+
*/
24+
export const TAG_VALUE_NORMALIZATION_REGEX = /[^\w\d_:/@.{}[\]$-]+/g;
825

926
export const DEFAULT_FLUSH_INTERVAL = 5000;

packages/core/src/metrics/simpleaggregator.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { Client, ClientOptions, MeasurementUnit, Primitive } from '@sentry/types';
22
import { timestampInSeconds } from '@sentry/utils';
3-
import { DEFAULT_FLUSH_INTERVAL, NAME_AND_TAG_KEY_REGEX, TAG_VALUE_REGEX } from './constants';
3+
import {
4+
DEFAULT_FLUSH_INTERVAL,
5+
NAME_AND_TAG_KEY_NORMALIZATION_REGEX,
6+
TAG_VALUE_NORMALIZATION_REGEX,
7+
} from './constants';
48
import type { Metric } from './instance';
59
import { METRIC_MAP } from './instance';
610
import type { MetricType, MetricsAggregator } from './types';
@@ -45,7 +49,7 @@ export class SimpleMetricsAggregator implements MetricsAggregator {
4549
maybeFloatTimestamp = timestampInSeconds(),
4650
): void {
4751
const timestamp = Math.floor(maybeFloatTimestamp);
48-
const name = unsanitizedName.replace(NAME_AND_TAG_KEY_REGEX, '_');
52+
const name = unsanitizedName.replace(NAME_AND_TAG_KEY_NORMALIZATION_REGEX, '_');
4953
const tags = sanitizeTags(unsanitizedTags);
5054

5155
const bucketKey = getBucketKey(metricType, name, unit, tags);
@@ -112,8 +116,8 @@ function sanitizeTags(unsanitizedTags: { [key: string]: Primitive }): { [key: st
112116
const tags: { [key: string]: string } = {};
113117
for (const key in unsanitizedTags) {
114118
if (Object.prototype.hasOwnProperty.call(unsanitizedTags, key)) {
115-
const sanitizedKey = key.replace(NAME_AND_TAG_KEY_REGEX, '_');
116-
tags[sanitizedKey] = String(unsanitizedTags[key]).replace(TAG_VALUE_REGEX, '_');
119+
const sanitizedKey = key.replace(NAME_AND_TAG_KEY_NORMALIZATION_REGEX, '_');
120+
tags[sanitizedKey] = String(unsanitizedTags[key]).replace(TAG_VALUE_NORMALIZATION_REGEX, '_');
117121
}
118122
}
119123
return tags;

0 commit comments

Comments
 (0)