11import type { Client , ClientOptions , MeasurementUnit , MetricsAggregator , Primitive } from '@sentry/types' ;
22import { timestampInSeconds } from '@sentry/utils' ;
3- import { DEFAULT_BROWSER_FLUSH_INTERVAL , NAME_AND_TAG_KEY_NORMALIZATION_REGEX , SET_METRIC_TYPE } from './constants' ;
3+ import { DEFAULT_BROWSER_FLUSH_INTERVAL , SET_METRIC_TYPE } from './constants' ;
44import { METRIC_MAP } from './instance' ;
55import { updateMetricSummaryOnActiveSpan } from './metric-summary' ;
66import type { MetricBucket , MetricType } from './types' ;
7- import { getBucketKey , sanitizeTags } from './utils' ;
7+ import { getBucketKey , sanitizeMetricKey , sanitizeTags , sanitizeUnit } from './utils' ;
88
99/**
1010 * A simple metrics aggregator that aggregates metrics in memory and flushes them periodically.
@@ -31,13 +31,14 @@ export class BrowserMetricsAggregator implements MetricsAggregator {
3131 metricType : MetricType ,
3232 unsanitizedName : string ,
3333 value : number | string ,
34- unit : MeasurementUnit | undefined = 'none' ,
34+ unsanitizedUnit : MeasurementUnit | undefined = 'none' ,
3535 unsanitizedTags : Record < string , Primitive > | undefined = { } ,
3636 maybeFloatTimestamp : number | undefined = timestampInSeconds ( ) ,
3737 ) : void {
3838 const timestamp = Math . floor ( maybeFloatTimestamp ) ;
39- const name = unsanitizedName . replace ( NAME_AND_TAG_KEY_NORMALIZATION_REGEX , '_' ) ;
39+ const name = sanitizeMetricKey ( unsanitizedName ) ;
4040 const tags = sanitizeTags ( unsanitizedTags ) ;
41+ const unit = sanitizeUnit ( unsanitizedUnit as string ) ;
4142
4243 const bucketKey = getBucketKey ( metricType , name , unit , tags ) ;
4344
@@ -77,11 +78,13 @@ export class BrowserMetricsAggregator implements MetricsAggregator {
7778 if ( this . _buckets . size === 0 ) {
7879 return ;
7980 }
81+
8082 if ( this . _client . captureAggregateMetrics ) {
8183 // TODO(@anonrig): Use Object.values() when we support ES6+
8284 const metricBuckets = Array . from ( this . _buckets ) . map ( ( [ , bucketItem ] ) => bucketItem ) ;
8385 this . _client . captureAggregateMetrics ( metricBuckets ) ;
8486 }
87+
8588 this . _buckets . clear ( ) ;
8689 }
8790
0 commit comments