1- import type { Client , ClientOptions , MeasurementUnit , MetricsAggregator , Primitive } from '@sentry/types' ;
1+ import type {
2+ Client ,
3+ ClientOptions ,
4+ MeasurementUnit ,
5+ MetricBucketItem ,
6+ MetricsAggregator ,
7+ Primitive
8+ } from '@sentry/types' ;
29import { timestampInSeconds } from '@sentry/utils' ;
310import {
4- DEFAULT_FLUSH_INTERVAL ,
11+ DEFAULT_BROWSER_FLUSH_INTERVAL ,
512 NAME_AND_TAG_KEY_NORMALIZATION_REGEX ,
6- TAG_VALUE_NORMALIZATION_REGEX ,
713} from './constants' ;
814import { METRIC_MAP } from './instance' ;
9- import type { MetricType , SimpleMetricBucket } from './types' ;
10- import { getBucketKey } from './utils' ;
15+ import type { MetricType , MetricBucket } from './types' ;
16+ import { getBucketKey , sanitizeTags } from './utils' ;
1117
1218/**
1319 * A simple metrics aggregator that aggregates metrics in memory and flushes them periodically.
1420 * Default flush interval is 5 seconds.
1521 *
1622 * @experimental This API is experimental and might change in the future.
1723 */
18- export class SimpleMetricsAggregator implements MetricsAggregator {
19- private _buckets : SimpleMetricBucket ;
24+ export class BrowserMetricsAggregator implements MetricsAggregator {
25+ private _buckets : MetricBucket ;
2026 private readonly _interval : ReturnType < typeof setInterval > ;
2127
2228 public constructor ( private readonly _client : Client < ClientOptions > ) {
2329 this . _buckets = new Map ( ) ;
24- this . _interval = setInterval ( ( ) => this . flush ( ) , DEFAULT_FLUSH_INTERVAL ) ;
30+ this . _interval = setInterval ( ( ) => this . flush ( ) , DEFAULT_BROWSER_FLUSH_INTERVAL ) ;
2531 }
2632
2733 /**
@@ -31,27 +37,33 @@ export class SimpleMetricsAggregator implements MetricsAggregator {
3137 metricType : MetricType ,
3238 unsanitizedName : string ,
3339 value : number | string ,
34- unit : MeasurementUnit = 'none' ,
35- unsanitizedTags : Record < string , Primitive > = { } ,
36- maybeFloatTimestamp = timestampInSeconds ( ) ,
40+ unit : MeasurementUnit | undefined = 'none' ,
41+ unsanitizedTags : Record < string , Primitive > | undefined = { } ,
42+ maybeFloatTimestamp : number | undefined = timestampInSeconds ( ) ,
3743 ) : void {
3844 const timestamp = Math . floor ( maybeFloatTimestamp ) ;
3945 const name = unsanitizedName . replace ( NAME_AND_TAG_KEY_NORMALIZATION_REGEX , '_' ) ;
4046 const tags = sanitizeTags ( unsanitizedTags ) ;
4147
4248 const bucketKey = getBucketKey ( metricType , name , unit , tags ) ;
43- const bucketItem = this . _buckets . get ( bucketKey ) ;
49+ const bucketItem : MetricBucketItem | undefined = this . _buckets . get ( bucketKey ) ;
4450 if ( bucketItem ) {
45- const [ bucketMetric , bucketTimestamp ] = bucketItem ;
46- bucketMetric . add ( value ) ;
51+ bucketItem . metric . add ( value ) ;
4752 // TODO(abhi): Do we need this check?
48- if ( bucketTimestamp < timestamp ) {
49- bucketItem [ 1 ] = timestamp ;
53+ if ( bucketItem . timestamp < timestamp ) {
54+ bucketItem . timestamp = timestamp ;
5055 }
5156 } else {
52- // @ts -expect-error we don't need to narrow down the type of value here, saves bundle size.
53- const newMetric = new METRIC_MAP [ metricType ] ( value ) ;
54- this . _buckets . set ( bucketKey , [ newMetric , timestamp , metricType , name , unit , tags ] ) ;
57+ this . _buckets . set ( bucketKey , {
58+ // @ts -expect-error we don't need to narrow down the type of value here, saves bundle size.
59+ metric : new METRIC_MAP [ metricType ] ( value ) ,
60+ timestamp,
61+ metricType,
62+ name,
63+ unit,
64+ tags,
65+ } ) ;
66+
5567 }
5668 }
5769
@@ -78,14 +90,3 @@ export class SimpleMetricsAggregator implements MetricsAggregator {
7890 this . flush ( ) ;
7991 }
8092}
81-
82- function sanitizeTags ( unsanitizedTags : Record < string , Primitive > ) : Record < string , string > {
83- const tags : Record < string , string > = { } ;
84- for ( const key in unsanitizedTags ) {
85- if ( Object . prototype . hasOwnProperty . call ( unsanitizedTags , key ) ) {
86- const sanitizedKey = key . replace ( NAME_AND_TAG_KEY_NORMALIZATION_REGEX , '_' ) ;
87- tags [ sanitizedKey ] = String ( unsanitizedTags [ key ] ) . replace ( TAG_VALUE_NORMALIZATION_REGEX , '_' ) ;
88- }
89- }
90- return tags ;
91- }
0 commit comments