Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"php": "^7.2||^8.0",
"guzzlehttp/psr7": "^2.1.1",
"jean85/pretty-package-versions": "^1.5||^2.0",
"sentry/sentry": "^4.19",
"sentry/sentry": "^4.19.1",
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
"symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",
"symfony/console": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",
Expand Down
7 changes: 6 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ parameters:
count: 1
path: src/DependencyInjection/SentryExtension.php

-
message: "#^Cannot access offset 'before_send_metric' on mixed\\.$#"
count: 1
path: src/DependencyInjection/SentryExtension.php

-
message: "#^Parameter \\#1 \\$array of function array_filter expects array, mixed given\\.$#"
count: 1
Expand All @@ -102,7 +107,7 @@ parameters:

-
message: "#^Parameter \\#1 \\$id of class Symfony\\\\Component\\\\DependencyInjection\\\\Reference constructor expects string, mixed given\\.$#"
count: 11
count: 12
path: src/DependencyInjection/SentryExtension.php

-
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->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')
->end()
->booleanNode('enable_logs')->end()
->booleanNode('enable_metrics')->defaultTrue()->end()
->booleanNode('attach_stacktrace')->end()
->booleanNode('attach_metric_code_locations')->end()
->integerNode('context_lines')->min(0)->end()
Expand Down Expand Up @@ -117,6 +118,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('before_send_check_in')->end()
->scalarNode('before_send_metrics')->end()
->scalarNode('before_send_log')->end()
->scalarNode('before_send_metric')->end()
->variableNode('trace_propagation_targets')->end()
->arrayNode('tags')
->useAttributeAsKey('name')
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ private function registerConfiguration(ContainerBuilder $container, array $confi
$options['before_breadcrumb'] = new Reference($options['before_breadcrumb']);
}

if (isset($options['before_send_metric'])) {
$options['before_send_metric'] = new Reference($options['before_send_metric']);
}

if (isset($options['class_serializers'])) {
$options['class_serializers'] = array_map(static function (string $value): Reference {
return new Reference($value);
Expand Down
1 change: 1 addition & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testProcessConfigurationWithDefaultConfiguration(): void
'options' => [
'integrations' => [],
'prefixes' => array_merge(['%kernel.project_dir%'], array_filter(explode(\PATH_SEPARATOR, get_include_path() ?: ''))),
'enable_metrics' => true,
'environment' => '%kernel.environment%',
'release' => '%env(default::SENTRY_RELEASE)%',
'ignore_exceptions' => [],
Expand Down
1 change: 1 addition & 0 deletions tests/DependencyInjection/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public function testClientIsCreatedFromOptions(): void
'traces_sampler' => new Reference('App\\Sentry\\Tracing\\TracesSampler'),
'profiles_sample_rate' => 1,
'enable_logs' => true,
'enable_metrics' => true,
'attach_stacktrace' => true,
'attach_metric_code_locations' => true,
'context_lines' => 0,
Expand Down
18 changes: 18 additions & 0 deletions tests/End2End/App/KernelWithMetricsCallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End\App;

use Symfony\Component\Config\Loader\LoaderInterface;

class KernelWithMetricsCallback extends Kernel
{
public function registerContainerConfiguration(LoaderInterface $loader): void
{
parent::registerContainerConfiguration($loader);

$loader->load(__DIR__ . '/metrics.yml');
$loader->load(__DIR__ . '/metrics_with_callback.yml');
}
}
18 changes: 18 additions & 0 deletions tests/End2End/App/KernelWithMetricsDisabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End\App;

use Symfony\Component\Config\Loader\LoaderInterface;

class KernelWithMetricsDisabled extends Kernel
{
public function registerContainerConfiguration(LoaderInterface $loader): void
{
parent::registerContainerConfiguration($loader);

$loader->load(__DIR__ . '/metrics.yml');
$loader->load(__DIR__ . '/metrics_disabled.yml');
}
}
3 changes: 3 additions & 0 deletions tests/End2End/App/metrics_disabled.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sentry:
options:
enable_metrics: false
12 changes: 12 additions & 0 deletions tests/End2End/App/metrics_with_callback.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sentry:
options:
before_send_metric: 'sentry.callback.before_send_metric'

services:
Sentry\SentryBundle\Tests\End2End\Fixtures\SentryCallbacks:
class: 'Sentry\SentryBundle\Tests\End2End\Fixtures\SentryCallbacks'

sentry.callback.before_send_metric:
class: 'Sentry\SentryBundle\Tests\End2End\Fixtures\SentryCallbacks'
factory: ['@Sentry\SentryBundle\Tests\End2End\Fixtures\SentryCallbacks', 'getBeforeSendMetric']

21 changes: 21 additions & 0 deletions tests/End2End/Fixtures/SentryCallbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End\Fixtures;

use Sentry\Metrics\Types\Metric;

class SentryCallbacks
{
public static function getBeforeSendMetric(): callable
{
return static function (Metric $metric): ?Metric {
if ('test-counter' === $metric->getName()) {
return null;
}

return $metric;
};
}
}
2 changes: 1 addition & 1 deletion tests/End2End/MetricsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testMetricsInCommand(): void

$count = $metrics[0];
$this->assertSame('test-counter', $count->getName());
$this->assertSame(10.0, $count->getValue());
$this->assertSame(10, $count->getValue());

$gauge = $metrics[1];
$this->assertSame('test-gauge', $gauge->getName());
Expand Down
34 changes: 34 additions & 0 deletions tests/End2End/MetricsDisabledEnd2EndTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End;

use Sentry\SentryBundle\Tests\End2End\App\KernelWithMetricsDisabled;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
* @runTestsInSeparateProcesses
*/
class MetricsDisabledEnd2EndTest extends WebTestCase
{
protected function setUp(): void
{
StubTransport::$events = [];
}

protected static function getKernelClass(): string
{
return KernelWithMetricsDisabled::class;
}

public function testMetricsAreFlushedAfterRequest(): void
{
$client = static::createClient();

$client->request('GET', '/metrics');
$this->assertSame(200, $client->getResponse()->getStatusCode());

$this->assertEmpty(StubTransport::$events);
}
}
2 changes: 1 addition & 1 deletion tests/End2End/MetricsEnd2EndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testMetricsAreFlushedAfterRequest(): void

$count = $metrics[0];
$this->assertSame('test-counter', $count->getName());
$this->assertSame(10.0, $count->getValue());
$this->assertSame(10, $count->getValue());

$gauge = $metrics[1];
$this->assertSame('test-gauge', $gauge->getName());
Expand Down
46 changes: 46 additions & 0 deletions tests/End2End/MetricsWithCallbackEnd2EndTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End;

use Sentry\SentryBundle\Tests\End2End\App\KernelWithMetricsCallback;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
* @runTestsInSeparateProcesses
*/
class MetricsWithCallbackEnd2EndTest extends WebTestCase
{
protected function setUp(): void
{
StubTransport::$events = [];
}

protected static function getKernelClass(): string
{
return KernelWithMetricsCallback::class;
}

public function testMetricsAreFlushedAfterRequest(): void
{
$client = static::createClient();

$client->request('GET', '/metrics');
$this->assertSame(200, $client->getResponse()->getStatusCode());

$this->assertCount(1, StubTransport::$events);

$metrics = StubTransport::$events[0]->getMetrics();
$this->assertCount(2, $metrics);

// Counter metric removed by `before_send_metric`
$gauge = $metrics[0];
$this->assertSame('test-gauge', $gauge->getName());
$this->assertSame(20.51, $gauge->getValue());

$distribution = $metrics[1];
$this->assertSame('test-distribution', $distribution->getName());
$this->assertSame(100.81, $distribution->getValue());
}
}
Loading