|
1 | 1 | import type { MeasurementUnit, MetricBucketItem, Primitive } from '@sentry/types';
|
2 | 2 | import { dropUndefinedKeys } from '@sentry/utils';
|
3 |
| -import { NAME_AND_TAG_KEY_NORMALIZATION_REGEX, TAG_VALUE_NORMALIZATION_REGEX } from './constants'; |
4 | 3 | import type { MetricType } from './types';
|
5 | 4 |
|
6 | 5 | /**
|
@@ -54,15 +53,72 @@ export function serializeMetricBuckets(metricBucketItems: MetricBucketItem[]): s
|
54 | 53 | return out;
|
55 | 54 | }
|
56 | 55 |
|
| 56 | +/** |
| 57 | + * Sanitizes units |
| 58 | + * |
| 59 | + * These Regex's are straight from the normalisation docs: |
| 60 | + * https://develop.sentry.dev/sdk/metrics/#normalization |
| 61 | + */ |
| 62 | +export function sanitizeUnit(unit: string): string { |
| 63 | + return unit.replace(/[^\w]+/gi, '_'); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Sanitizes metric keys |
| 68 | + * |
| 69 | + * These Regex's are straight from the normalisation docs: |
| 70 | + * https://develop.sentry.dev/sdk/metrics/#normalization |
| 71 | + */ |
| 72 | +export function sanitizeMetricKey(key: string): string { |
| 73 | + return key.replace(/[^\w\-.]+/gi, '_'); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Sanitizes metric keys |
| 78 | + * |
| 79 | + * These Regex's are straight from the normalisation docs: |
| 80 | + * https://develop.sentry.dev/sdk/metrics/#normalization |
| 81 | + */ |
| 82 | +function sanitizeTagKey(key: string): string { |
| 83 | + return key.replace(/[^\w\-./]+/gi, ''); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * These Regex's are straight from the normalisation docs: |
| 88 | + * https://develop.sentry.dev/sdk/metrics/#normalization |
| 89 | + */ |
| 90 | +const tagValueReplacements: [string, string][] = [ |
| 91 | + ['\n', '\\n'], |
| 92 | + ['\r', '\\r'], |
| 93 | + ['\t', '\\t'], |
| 94 | + ['\\', '\\\\'], |
| 95 | + ['|', '\\u{7c}'], |
| 96 | + [',', '\\u{2c}'], |
| 97 | +]; |
| 98 | + |
| 99 | +function getCharOrReplacement(input: string): string { |
| 100 | + for (const [search, replacement] of tagValueReplacements) { |
| 101 | + if (input === search) { |
| 102 | + return replacement; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return input; |
| 107 | +} |
| 108 | + |
| 109 | +function sanitizeTagValue(value: string): string { |
| 110 | + return [...value].reduce((acc, char) => acc + getCharOrReplacement(char), ''); |
| 111 | +} |
| 112 | + |
57 | 113 | /**
|
58 | 114 | * Sanitizes tags.
|
59 | 115 | */
|
60 | 116 | export function sanitizeTags(unsanitizedTags: Record<string, Primitive>): Record<string, string> {
|
61 | 117 | const tags: Record<string, string> = {};
|
62 | 118 | for (const key in unsanitizedTags) {
|
63 | 119 | if (Object.prototype.hasOwnProperty.call(unsanitizedTags, key)) {
|
64 |
| - const sanitizedKey = key.replace(NAME_AND_TAG_KEY_NORMALIZATION_REGEX, '_'); |
65 |
| - tags[sanitizedKey] = String(unsanitizedTags[key]).replace(TAG_VALUE_NORMALIZATION_REGEX, ''); |
| 120 | + const sanitizedKey = sanitizeTagKey(key); |
| 121 | + tags[sanitizedKey] = sanitizeTagValue(String(unsanitizedTags[key])); |
66 | 122 | }
|
67 | 123 | }
|
68 | 124 | return tags;
|
|
0 commit comments