1- import type { ClientOptions , MeasurementUnit , Primitive } from '@sentry/types' ;
1+ import type { Client , ClientOptions , MeasurementUnit , Primitive } from '@sentry/types' ;
22import { timestampInSeconds } from '@sentry/utils' ;
3- import type { BaseClient } from '../baseclient' ;
4- import { NAME_AND_TAG_KEY_REGEX , TAG_VALUE_REGEX } from './constants' ;
5- import { createMetricEnvelope } from './envelope' ;
3+ import { DEFAULT_FLUSH_INTERVAL , NAME_AND_TAG_KEY_REGEX , TAG_VALUE_REGEX } from './constants' ;
64import type { Metric } from './instance' ;
75import { METRIC_MAP } from './instance' ;
86import type { MetricType , MetricsAggregator } from './types' ;
@@ -22,14 +20,17 @@ type SimpleMetricBucket = Map<
2220
2321/**
2422 * A simple metrics aggregator that aggregates metrics in memory and flushes them periodically.
23+ * Default flush interval is 5 seconds.
2524 *
2625 * @experimental This API is experimental and might change in the future.
2726 */
2827export class SimpleMetricsAggregator implements MetricsAggregator {
2928 private _buckets : SimpleMetricBucket ;
29+ private readonly _interval : ReturnType < typeof setInterval > ;
3030
31- public constructor ( private readonly _client : BaseClient < ClientOptions > ) {
31+ public constructor ( private readonly _client : Client < ClientOptions > ) {
3232 this . _buckets = new Map ( ) ;
33+ this . _interval = setInterval ( ( ) => this . flush ( ) , DEFAULT_FLUSH_INTERVAL ) ;
3334 }
3435
3536 /**
@@ -71,18 +72,18 @@ export class SimpleMetricsAggregator implements MetricsAggregator {
7172 return ;
7273 }
7374
74- // Allow of this logic should be in the client, but we want to
75- const metrics = serializeBuckets ( this . _buckets ) ;
76- const sdkMetadata = this . _client . getSdkMetadata && this . _client . getSdkMetadata ( ) ;
77- const metricsEnvelope = createMetricEnvelope (
78- metrics ,
79- sdkMetadata ,
80- this . _client . getOptions ( ) . tunnel ,
81- this . _client . getDsn ( ) ,
82- ) ;
75+ if ( this . _client . captureSerializedMetrics ) {
76+ this . _client . captureSerializedMetrics ( serializeBuckets ( this . _buckets ) ) ;
77+ }
78+ this . _buckets . clear ( ) ;
79+ }
8380
84- // TODO(abhi): Remove this hack - only here until we decide on final API for metrics on client
85- void this . _client [ '_sendEnvelope' ] ( metricsEnvelope ) ;
81+ /**
82+ * @inheritDoc
83+ */
84+ public close ( ) : void {
85+ clearInterval ( this . _interval ) ;
86+ this . flush ( ) ;
8687 this . _buckets . clear ( ) ;
8788 }
8889}
0 commit comments