Skip to content

Commit 0355f04

Browse files
committed
add enable_metrics and before_send_metric
1 parent 2889ed3 commit 0355f04

File tree

5 files changed

+126
-4
lines changed

5 files changed

+126
-4
lines changed

src/Metrics/Metrics.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function increment(
4848
/**
4949
* @param array<string, string> $tags
5050
*
51-
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
51+
* @deprecated Use TraceMetrics::distribution() instead. Metrics API is a no-op and will be removed in 5.x.
5252
*/
5353
public function distribution(
5454
string $key,

src/Metrics/MetricsAggregator.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct()
4242
];
4343

4444
/**
45-
* @param int|float|string $value
45+
* @param int|float $value
4646
* @param array<string, int|float|string|bool> $attributes
4747
*/
4848
public function add(
@@ -58,6 +58,10 @@ public function add(
5858
if ($client instanceof Client) {
5959
$options = $client->getOptions();
6060

61+
if ($options->getEnableMetrics() === false) {
62+
return;
63+
}
64+
6165
$defaultAttributes = [
6266
'sentry.sdk.name' => $client->getSdkIdentifier(),
6367
'sentry.sdk.version' => $client->getSdkVersion(),
@@ -89,9 +93,17 @@ public function add(
8993

9094
$metricTypeClass = self::METRIC_TYPES[$type];
9195
/** @var AbstractType $metric */
92-
/** @phpstan-ignore-next-line SetType accepts int|float|string, others only int|float */
96+
/** @phpstan-ignore-next-line */
9397
$metric = new $metricTypeClass($name, $value, $traceId, $spanId, $attributes, microtime(true), $unit);
9498

99+
if ($client !== null) {
100+
$beforeSendMetric = $client->getOptions()->getBeforeSendMetricCallback();
101+
$metric = $beforeSendMetric($metric);
102+
if ($metric === null) {
103+
return;
104+
}
105+
}
106+
95107
$this->metrics->push($metric);
96108
}
97109

src/Options.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Sentry\Integration\ErrorListenerIntegration;
1111
use Sentry\Integration\IntegrationInterface;
1212
use Sentry\Logs\Log;
13+
use Sentry\Metrics\Types\AbstractType;
1314
use Sentry\Transport\TransportInterface;
1415
use Symfony\Component\OptionsResolver\Options as SymfonyOptions;
1516
use 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[]']);

tests/Metrics/TraceMetricsTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Sentry\Client;
99
use Sentry\Metrics\MetricsAggregator;
10+
use Sentry\Metrics\Types\AbstractType;
1011
use Sentry\Metrics\Types\CounterType;
1112
use Sentry\Metrics\Types\DistributionType;
1213
use Sentry\Metrics\Types\GaugeType;
@@ -19,7 +20,7 @@ final class TraceMetricsTest extends TestCase
1920
{
2021
protected function setUp(): void
2122
{
22-
HubAdapter::getInstance()->bindClient(new Client(new Options(), new StubTransport()));
23+
HubAdapter::getInstance()->bindClient(new Client(new Options(), StubTransport::getInstance()));
2324
StubTransport::$events = [];
2425
}
2526

@@ -82,4 +83,37 @@ public function testMetricsBufferFull(): void
8283
$metrics = $event->getMetrics();
8384
$this->assertCount(MetricsAggregator::METRICS_BUFFER_SIZE, $metrics);
8485
}
86+
87+
public function testEnableMetrics(): void
88+
{
89+
HubAdapter::getInstance()->bindClient(new Client(new Options([
90+
'enable_metrics' => false,
91+
]), StubTransport::getInstance()));
92+
93+
trace_metrics()->count('test-count', 2, ['foo' => 'bar']);
94+
trace_metrics()->flush();
95+
96+
$this->assertEmpty(StubTransport::$events);
97+
}
98+
99+
public function testBeforeSendMetricAltersContent()
100+
{
101+
HubAdapter::getInstance()->bindClient(new Client(new Options([
102+
'before_send_metric' => static function (AbstractType $metric) {
103+
$metric->setValue(99999);
104+
105+
return $metric;
106+
},
107+
]), StubTransport::getInstance()));
108+
109+
trace_metrics()->count('test-count', 2, ['foo' => 'bar']);
110+
trace_metrics()->flush();
111+
112+
$this->assertCount(1, StubTransport::$events);
113+
$event = StubTransport::$events[0];
114+
115+
$this->assertCount(1, $event->getMetrics());
116+
$metric = $event->getMetrics()[0];
117+
$this->assertEquals(99999, $metric->getValue());
118+
}
85119
}

tests/StubTransport.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ class StubTransport implements TransportInterface
1919
*/
2020
public static $events = [];
2121

22+
/**
23+
* @var self
24+
*/
25+
private static $instance;
26+
27+
public static function getInstance(): self
28+
{
29+
if (self::$instance === null) {
30+
self::$instance = new self();
31+
}
32+
33+
return self::$instance;
34+
}
35+
2236
public function send(Event $event): Result
2337
{
2438
self::$events[] = $event;

0 commit comments

Comments
 (0)