1010use Sentry \Integration \ErrorListenerIntegration ;
1111use Sentry \Integration \IntegrationInterface ;
1212use Sentry \Logs \Log ;
13+ use Sentry \Metrics \Types \AbstractType ;
1314use Sentry \Transport \TransportInterface ;
1415use Symfony \Component \OptionsResolver \Options as SymfonyOptions ;
1516use Symfony \Component \OptionsResolver \OptionsResolver ;
@@ -177,6 +178,31 @@ public function getEnableLogs(): bool
177178 return $ this ->options ['enable_logs ' ] ?? false ;
178179 }
179180
181+ /**
182+ * Sets if metrics should be enabled or not.
183+ */
184+ public function setEnableMetrics (bool $ enableTracing ): self
185+ {
186+ $ options = array_merge ($ this ->options , ['enable_metrics ' => $ enableTracing ]);
187+
188+ $ this ->options = $ this ->resolver ->resolve ($ options );
189+
190+ return $ this ;
191+ }
192+
193+ /**
194+ * Returns whether metrics are enabled or not.
195+ */
196+ public function getEnableMetrics (): bool
197+ {
198+ /**
199+ * @var bool $enableMetrics
200+ */
201+ $ enableMetrics = $ this ->options ['enable_metrics ' ] ?? true ;
202+
203+ return $ enableMetrics ;
204+ }
205+
180206 /**
181207 * Sets the sampling factor to apply to transactions. A value of 0 will deny
182208 * sending any transactions, and a value of 1 will send 100% of transactions.
@@ -676,6 +702,35 @@ public function getBeforeSendMetricsCallback(): callable
676702 return $ this ->options ['before_send_metrics ' ];
677703 }
678704
705+ /**
706+ * Gets a callback that will be invoked before a metric is added.
707+ * Returning `null` means that the metric will be discarded.
708+ */
709+ public function getBeforeSendMetricCallback (): callable
710+ {
711+ /**
712+ * @var callable $callback
713+ */
714+ $ callback = $ this ->options ['before_send_metric ' ];
715+
716+ return $ callback ;
717+ }
718+
719+ /**
720+ * Sets a new callback that is invoked before metrics are sent.
721+ * Returning `null` means that the metric will be discarded.
722+ *
723+ * @return $this
724+ */
725+ public function setBeforeSendMetricCallback (callable $ callback ): self
726+ {
727+ $ options = array_merge ($ this ->options , ['before_send_metric ' => $ callback ]);
728+
729+ $ this ->options = $ this ->resolver ->resolve ($ options );
730+
731+ return $ this ;
732+ }
733+
679734 /**
680735 * Sets a callable to be called to decide whether metrics should
681736 * be send or not.
@@ -1220,6 +1275,7 @@ private function configureOptions(OptionsResolver $resolver): void
12201275 'sample_rate ' => 1 ,
12211276 'enable_tracing ' => null ,
12221277 'enable_logs ' => false ,
1278+ 'enable_metrics ' => true ,
12231279 'traces_sample_rate ' => null ,
12241280 'traces_sampler ' => null ,
12251281 'profiles_sample_rate ' => null ,
@@ -1256,10 +1312,14 @@ private function configureOptions(OptionsResolver $resolver): void
12561312 },
12571313 /**
12581314 * @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
1315+ * Use `before_send_metric` instead.
12591316 */
12601317 'before_send_metrics ' => static function (Event $ metrics ): ?Event {
12611318 return null ;
12621319 },
1320+ 'before_send_metric ' => static function (AbstractType $ metric ): AbstractType {
1321+ return $ metric ;
1322+ },
12631323 'trace_propagation_targets ' => null ,
12641324 'strict_trace_propagation ' => false ,
12651325 'tags ' => [],
@@ -1290,6 +1350,7 @@ private function configureOptions(OptionsResolver $resolver): void
12901350 $ resolver ->setAllowedTypes ('sample_rate ' , ['int ' , 'float ' ]);
12911351 $ resolver ->setAllowedTypes ('enable_tracing ' , ['null ' , 'bool ' ]);
12921352 $ resolver ->setAllowedTypes ('enable_logs ' , 'bool ' );
1353+ $ resolver ->setAllowedTypes ('enable_metrics ' , 'bool ' );
12931354 $ resolver ->setAllowedTypes ('traces_sample_rate ' , ['null ' , 'int ' , 'float ' ]);
12941355 $ resolver ->setAllowedTypes ('traces_sampler ' , ['null ' , 'callable ' ]);
12951356 $ resolver ->setAllowedTypes ('profiles_sample_rate ' , ['null ' , 'int ' , 'float ' ]);
@@ -1309,6 +1370,7 @@ private function configureOptions(OptionsResolver $resolver): void
13091370 $ resolver ->setAllowedTypes ('before_send ' , ['callable ' ]);
13101371 $ resolver ->setAllowedTypes ('before_send_transaction ' , ['callable ' ]);
13111372 $ resolver ->setAllowedTypes ('before_send_log ' , 'callable ' );
1373+ $ resolver ->setAllowedTypes ('before_send_metric ' , ['callable ' ]);
13121374 $ resolver ->setAllowedTypes ('ignore_exceptions ' , 'string[] ' );
13131375 $ resolver ->setAllowedTypes ('ignore_transactions ' , 'string[] ' );
13141376 $ resolver ->setAllowedTypes ('trace_propagation_targets ' , ['null ' , 'string[] ' ]);
0 commit comments