1717use Sentry \Tracing \SpanId ;
1818use Sentry \Tracing \TraceId ;
1919use Sentry \Unit ;
20- use Sentry \Util \RingBuffer ;
20+ use Sentry \Util \TelemetryStorage ;
2121
2222/**
2323 * @internal
@@ -29,22 +29,17 @@ final class MetricsAggregator
2929 */
3030 public const METRICS_BUFFER_SIZE = 1000 ;
3131
32- /**
33- * @var RingBuffer<Metric>
34- */
35- private $ metrics ;
36-
37- public function __construct ()
38- {
39- $ this ->metrics = new RingBuffer (self ::METRICS_BUFFER_SIZE );
40- }
41-
4232 private const METRIC_TYPES = [
4333 CounterMetric::TYPE => CounterMetric::class,
4434 DistributionMetric::TYPE => DistributionMetric::class,
4535 GaugeMetric::TYPE => GaugeMetric::class,
4636 ];
4737
38+ /**
39+ * @var TelemetryStorage<Metric>|null
40+ */
41+ private $ metrics ;
42+
4843 /**
4944 * @param int|float $value
5045 * @param array<string, int|float|string|bool> $attributes
@@ -58,6 +53,7 @@ public function add(
5853 ): void {
5954 $ hub = SentrySdk::getCurrentHub ();
6055 $ client = $ hub ->getClient ();
56+ $ metricFlushThreshold = null ;
6157
6258 if (!\is_int ($ value ) && !\is_float ($ value )) {
6359 if ($ client !== null ) {
@@ -67,20 +63,24 @@ public function add(
6763 return ;
6864 }
6965
70- if ($ client instanceof Client ) {
66+ if ($ client !== null ) {
7167 $ options = $ client ->getOptions ();
68+ $ metricFlushThreshold = $ options ->getMetricFlushThreshold ();
7269
7370 if ($ options ->getEnableMetrics () === false ) {
7471 return ;
7572 }
7673
7774 $ defaultAttributes = [
78- 'sentry.sdk.name ' => $ client ->getSdkIdentifier (),
79- 'sentry.sdk.version ' => $ client ->getSdkVersion (),
8075 'sentry.environment ' => $ options ->getEnvironment () ?? Event::DEFAULT_ENVIRONMENT ,
8176 'server.address ' => $ options ->getServerName (),
8277 ];
8378
79+ if ($ client instanceof Client) {
80+ $ defaultAttributes ['sentry.sdk.name ' ] = $ client ->getSdkIdentifier ();
81+ $ defaultAttributes ['sentry.sdk.version ' ] = $ client ->getSdkVersion ();
82+ }
83+
8484 if ($ options ->shouldSendDefaultPii ()) {
8585 $ hub ->configureScope (static function (Scope $ scope ) use (&$ defaultAttributes ) {
8686 $ user = $ scope ->getUser ();
@@ -122,12 +122,17 @@ public function add(
122122 }
123123 }
124124
125- $ this ->metrics ->push ($ metric );
125+ $ metrics = $ this ->getStorage ($ metricFlushThreshold );
126+ $ metrics ->push ($ metric );
127+
128+ if ($ metricFlushThreshold !== null && \count ($ metrics ) >= $ metricFlushThreshold ) {
129+ $ this ->flush ($ hub );
130+ }
126131 }
127132
128133 public function flush (?HubInterface $ hub = null ): ?EventId
129134 {
130- if ($ this ->metrics ->isEmpty ()) {
135+ if ($ this ->metrics === null || $ this -> metrics ->isEmpty ()) {
131136 return null ;
132137 }
133138
@@ -151,4 +156,21 @@ private function getTraceContext(HubInterface $hub): array
151156 /** @var array{trace_id: string, span_id: string} $traceContext */
152157 return $ traceContext ;
153158 }
159+
160+ /**
161+ * @return TelemetryStorage<Metric>
162+ */
163+ private function getStorage (?int $ metricFlushThreshold = null ): TelemetryStorage
164+ {
165+ if ($ this ->metrics === null ) {
166+ /** @var TelemetryStorage<Metric> $metrics */
167+ $ metrics = $ metricFlushThreshold !== null
168+ ? TelemetryStorage::unbounded ()
169+ : TelemetryStorage::bounded (self ::METRICS_BUFFER_SIZE );
170+
171+ $ this ->metrics = $ metrics ;
172+ }
173+
174+ return $ this ->metrics ;
175+ }
154176}
0 commit comments