Skip to content

Commit d2d7fe4

Browse files
committed
ref(metrics): add metric options
1 parent 44bdbb1 commit d2d7fe4

12 files changed

+164
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"php": "^7.2||^8.0",
1616
"guzzlehttp/psr7": "^2.1.1",
1717
"jean85/pretty-package-versions": "^1.5||^2.0",
18-
"sentry/sentry": "^4.19",
18+
"sentry/sentry": "^4.19.1",
1919
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
2020
"symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",
2121
"symfony/console": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",

src/DependencyInjection/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function getConfigTreeBuilder(): TreeBuilder
8989
->info('The sampling factor to apply to profiles. A value of 0 will deny sending any profiles, and a value of 1 will send all profiles. Profiles are sampled in relation to traces_sample_rate')
9090
->end()
9191
->booleanNode('enable_logs')->end()
92+
->booleanNode('enable_metrics')->defaultTrue()->end()
9293
->booleanNode('attach_stacktrace')->end()
9394
->booleanNode('attach_metric_code_locations')->end()
9495
->integerNode('context_lines')->min(0)->end()
@@ -117,6 +118,7 @@ public function getConfigTreeBuilder(): TreeBuilder
117118
->scalarNode('before_send_check_in')->end()
118119
->scalarNode('before_send_metrics')->end()
119120
->scalarNode('before_send_log')->end()
121+
->scalarNode('before_send_metric')->end()
120122
->variableNode('trace_propagation_targets')->end()
121123
->arrayNode('tags')
122124
->useAttributeAsKey('name')

src/DependencyInjection/SentryExtension.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ private function registerConfiguration(ContainerBuilder $container, array $confi
137137
$options['before_breadcrumb'] = new Reference($options['before_breadcrumb']);
138138
}
139139

140+
if (isset($options['before_send_metric'])) {
141+
$options['before_send_metric'] = new Reference($options['before_send_metric']);
142+
}
143+
140144
if (isset($options['class_serializers'])) {
141145
$options['class_serializers'] = array_map(static function (string $value): Reference {
142146
return new Reference($value);
@@ -316,7 +320,7 @@ private function registerHttpClientTracingConfiguration(ContainerBuilder $contai
316320
}
317321

318322
/**
319-
* @param string[] $integrations
323+
* @param string[] $integrations
320324
* @param array<string, mixed> $config
321325
*
322326
* @return array<Reference|Definition>
@@ -348,12 +352,12 @@ private function configureRequestIntegration(array $integrations, bool $useDefau
348352

349353
/**
350354
* @param class-string<IntegrationInterface> $integrationClass
351-
* @param array<Reference|Definition> $integrations
355+
* @param array<Reference|Definition> $integrations
352356
*/
353357
private function isIntegrationEnabled(string $integrationClass, array $integrations): bool
354358
{
355359
foreach ($integrations as $integration) {
356-
if ($integration instanceof Reference && $integrationClass === (string) $integration) {
360+
if ($integration instanceof Reference && $integrationClass === (string)$integration) {
357361
return true;
358362
}
359363

tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function testProcessConfigurationWithDefaultConfiguration(): void
2727
'options' => [
2828
'integrations' => [],
2929
'prefixes' => array_merge(['%kernel.project_dir%'], array_filter(explode(\PATH_SEPARATOR, get_include_path() ?: ''))),
30+
'enable_metrics' => true,
3031
'environment' => '%kernel.environment%',
3132
'release' => '%env(default::SENTRY_RELEASE)%',
3233
'ignore_exceptions' => [],

tests/DependencyInjection/SentryExtensionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ public function testClientIsCreatedFromOptions(): void
217217
'traces_sampler' => new Reference('App\\Sentry\\Tracing\\TracesSampler'),
218218
'profiles_sample_rate' => 1,
219219
'enable_logs' => true,
220+
'enable_metrics' => true,
220221
'attach_stacktrace' => true,
221222
'attach_metric_code_locations' => true,
222223
'context_lines' => 0,
@@ -498,7 +499,7 @@ private function createContainerFromFixture(string $fixtureFile): ContainerBuild
498499

499500
/**
500501
* @param array<int, mixed> $methodCall
501-
* @param mixed[] $arguments
502+
* @param mixed[] $arguments
502503
*/
503504
private function assertDefinitionMethodCallAt(array $methodCall, string $method, array $arguments): void
504505
{
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End\App;
6+
7+
use Symfony\Component\Config\Loader\LoaderInterface;
8+
9+
class KernelWithMetricsCallback extends Kernel
10+
{
11+
public function registerContainerConfiguration(LoaderInterface $loader): void
12+
{
13+
parent::registerContainerConfiguration($loader);
14+
15+
$loader->load(__DIR__ . '/metrics.yml');
16+
$loader->load(__DIR__ . '/metrics_with_callback.yml');
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End\App;
6+
7+
use Symfony\Component\Config\Loader\LoaderInterface;
8+
9+
class KernelWithMetricsDisabled extends Kernel
10+
{
11+
public function registerContainerConfiguration(LoaderInterface $loader): void
12+
{
13+
parent::registerContainerConfiguration($loader);
14+
15+
$loader->load(__DIR__ . '/metrics.yml');
16+
$loader->load(__DIR__ . '/metrics_disabled.yml');
17+
}
18+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sentry:
2+
options:
3+
enable_metrics: false
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sentry:
2+
options:
3+
before_send_metric: 'sentry.callback.before_send_metric'
4+
5+
services:
6+
Sentry\SentryBundle\Tests\End2End\Fixtures\SentryCallbacks:
7+
class: 'Sentry\SentryBundle\Tests\End2End\Fixtures\SentryCallbacks'
8+
9+
sentry.callback.before_send_metric:
10+
class: 'Sentry\SentryBundle\Tests\End2End\Fixtures\SentryCallbacks'
11+
factory: ['@Sentry\SentryBundle\Tests\End2End\Fixtures\SentryCallbacks', 'getBeforeSendMetric']
12+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Sentry\SentryBundle\Tests\End2End\Fixtures;
4+
5+
use Sentry\Metrics\Types\Metric;
6+
7+
class SentryCallbacks
8+
{
9+
10+
public static function getBeforeSendMetric(): callable
11+
{
12+
return static function (Metric $metric): ?Metric {
13+
if ($metric->getName() === 'test-counter') {
14+
return null;
15+
}
16+
return $metric;
17+
};
18+
}
19+
20+
}

0 commit comments

Comments
 (0)