From 33634c6143e13c197016bb43c897888f290ecdb4 Mon Sep 17 00:00:00 2001 From: Martin Linzmayer Date: Mon, 1 Sep 2025 16:47:46 +0200 Subject: [PATCH 1/2] fix(logs): use correct option name --- phpstan-baseline.neon | 2 +- src/DependencyInjection/Configuration.php | 2 +- src/DependencyInjection/SentryExtension.php | 10 +++++----- src/Resources/config/schema/sentry-1.0.xsd | 2 +- .../DependencyInjection/Fixtures/php/full.php | 2 +- .../DependencyInjection/Fixtures/xml/full.xml | 2 +- .../DependencyInjection/Fixtures/yml/full.yml | 2 +- .../SentryExtensionTest.php | 4 ++-- .../App/Callback/BeforeSendLogCallback.php | 20 +++++++++++++++++++ .../App/Controller/LoggingController.php | 9 +++++++++ tests/End2End/App/logging.yml | 8 ++++++++ tests/End2End/App/routing.yml | 4 ++++ tests/End2End/LoggingEnd2EndTest.php | 17 ++++++++++++++++ 13 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 tests/End2End/App/Callback/BeforeSendLogCallback.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2c01285c..da675be7 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -86,7 +86,7 @@ parameters: path: src/DependencyInjection/SentryExtension.php - - message: "#^Cannot access offset 'before_send_logs' on mixed\\.$#" + message: "#^Cannot access offset 'before_send_log' on mixed\\.$#" count: 1 path: src/DependencyInjection/SentryExtension.php diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index f2618281..7a3c9a39 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -113,7 +113,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->scalarNode('before_send_transaction')->end() ->scalarNode('before_send_check_in')->end() ->scalarNode('before_send_metrics')->end() - ->scalarNode('before_send_logs')->end() + ->scalarNode('before_send_log')->end() ->variableNode('trace_propagation_targets')->end() ->arrayNode('tags') ->useAttributeAsKey('name') diff --git a/src/DependencyInjection/SentryExtension.php b/src/DependencyInjection/SentryExtension.php index 55a268cc..36e4ac06 100644 --- a/src/DependencyInjection/SentryExtension.php +++ b/src/DependencyInjection/SentryExtension.php @@ -124,8 +124,8 @@ private function registerConfiguration(ContainerBuilder $container, array $confi $options['before_send_metrics'] = new Reference($options['before_send_metrics']); } - if (isset($options['before_send_logs'])) { - $options['before_send_logs'] = new Reference($options['before_send_logs']); + if (isset($options['before_send_log'])) { + $options['before_send_log'] = new Reference($options['before_send_log']); } if (isset($options['before_breadcrumb'])) { @@ -290,7 +290,7 @@ private function registerHttpClientTracingConfiguration(ContainerBuilder $contai } /** - * @param string[] $integrations + * @param string[] $integrations * @param array $config * * @return array @@ -322,12 +322,12 @@ private function configureRequestIntegration(array $integrations, bool $useDefau /** * @param class-string $integrationClass - * @param array $integrations + * @param array $integrations */ private function isIntegrationEnabled(string $integrationClass, array $integrations): bool { foreach ($integrations as $integration) { - if ($integration instanceof Reference && $integrationClass === (string) $integration) { + if ($integration instanceof Reference && $integrationClass === (string)$integration) { return true; } diff --git a/src/Resources/config/schema/sentry-1.0.xsd b/src/Resources/config/schema/sentry-1.0.xsd index 0bd49d7a..d8c81294 100644 --- a/src/Resources/config/schema/sentry-1.0.xsd +++ b/src/Resources/config/schema/sentry-1.0.xsd @@ -55,7 +55,7 @@ - + diff --git a/tests/DependencyInjection/Fixtures/php/full.php b/tests/DependencyInjection/Fixtures/php/full.php index 89862bb9..973b125c 100644 --- a/tests/DependencyInjection/Fixtures/php/full.php +++ b/tests/DependencyInjection/Fixtures/php/full.php @@ -33,7 +33,7 @@ 'before_send_transaction' => 'App\\Sentry\\BeforeSendTransactionCallback', 'before_send_check_in' => 'App\\Sentry\\BeforeSendCheckInCallback', 'before_send_metrics' => 'App\\Sentry\\BeforeSendMetricsCallback', - 'before_send_logs' => 'App\\Sentry\\BeforeSendLogsCallback', + 'before_send_log' => 'App\\Sentry\\BeforeSendLogsCallback', 'trace_propagation_targets' => ['website.invalid'], 'tags' => [ 'context' => 'development', diff --git a/tests/DependencyInjection/Fixtures/xml/full.xml b/tests/DependencyInjection/Fixtures/xml/full.xml index ba325880..ddf466a2 100644 --- a/tests/DependencyInjection/Fixtures/xml/full.xml +++ b/tests/DependencyInjection/Fixtures/xml/full.xml @@ -30,7 +30,7 @@ before-send-transaction="App\Sentry\BeforeSendTransactionCallback" before-send-check-in="App\Sentry\BeforeSendCheckInCallback" before-send-metrics="App\Sentry\BeforeSendMetricsCallback" - before-send-logs="App\Sentry\BeforeSendLogsCallback" + before-send-log="App\Sentry\BeforeSendLogsCallback" error-types="E_ALL" max-breadcrumbs="1" before-breadcrumb="App\Sentry\BeforeBreadcrumbCallback" diff --git a/tests/DependencyInjection/Fixtures/yml/full.yml b/tests/DependencyInjection/Fixtures/yml/full.yml index 6537f4d7..e9097964 100644 --- a/tests/DependencyInjection/Fixtures/yml/full.yml +++ b/tests/DependencyInjection/Fixtures/yml/full.yml @@ -30,7 +30,7 @@ sentry: before_send_transaction: App\Sentry\BeforeSendTransactionCallback before_send_check_in: App\Sentry\BeforeSendCheckInCallback before_send_metrics: App\Sentry\BeforeSendMetricsCallback - before_send_logs: App\Sentry\BeforeSendLogsCallback + before_send_log: App\Sentry\BeforeSendLogsCallback trace_propagation_targets: - 'website.invalid' tags: diff --git a/tests/DependencyInjection/SentryExtensionTest.php b/tests/DependencyInjection/SentryExtensionTest.php index 9717df22..9a8594c0 100644 --- a/tests/DependencyInjection/SentryExtensionTest.php +++ b/tests/DependencyInjection/SentryExtensionTest.php @@ -229,7 +229,7 @@ public function testClientIsCreatedFromOptions(): void 'before_send_transaction' => new Reference('App\\Sentry\\BeforeSendTransactionCallback'), 'before_send_check_in' => new Reference('App\\Sentry\\BeforeSendCheckInCallback'), 'before_send_metrics' => new Reference('App\\Sentry\\BeforeSendMetricsCallback'), - 'before_send_logs' => new Reference('App\\Sentry\\BeforeSendLogsCallback'), + 'before_send_log' => new Reference('App\\Sentry\\BeforeSendLogsCallback'), 'trace_propagation_targets' => ['website.invalid'], 'tags' => [ 'context' => 'development', @@ -491,7 +491,7 @@ private function createContainerFromFixture(string $fixtureFile): ContainerBuild /** * @param array $methodCall - * @param mixed[] $arguments + * @param mixed[] $arguments */ private function assertDefinitionMethodCallAt(array $methodCall, string $method, array $arguments): void { diff --git a/tests/End2End/App/Callback/BeforeSendLogCallback.php b/tests/End2End/App/Callback/BeforeSendLogCallback.php new file mode 100644 index 00000000..a5937bf1 --- /dev/null +++ b/tests/End2End/App/Callback/BeforeSendLogCallback.php @@ -0,0 +1,20 @@ +getBody() === "before_send_log") { + return null; + } + return $log; + }; + } + +} diff --git a/tests/End2End/App/Controller/LoggingController.php b/tests/End2End/App/Controller/LoggingController.php index 13580b2c..a2e98a84 100644 --- a/tests/End2End/App/Controller/LoggingController.php +++ b/tests/End2End/App/Controller/LoggingController.php @@ -43,4 +43,13 @@ public function loggingWithError() $this->logger->error('About to crash'); throw new \RuntimeException('Crash'); } + + public function beforeSendLog() + { + $this->logger->warning("warn 1"); + $this->logger->error("before_send_log"); + $this->logger->warning("warn 2"); + + return new Response(); + } } diff --git a/tests/End2End/App/logging.yml b/tests/End2End/App/logging.yml index 0e4d49b5..bbd0f68c 100644 --- a/tests/End2End/App/logging.yml +++ b/tests/End2End/App/logging.yml @@ -1,6 +1,7 @@ sentry: options: enable_logs: true + before_send_log: "sentry.callback.before_send_log" monolog: handlers: @@ -13,6 +14,13 @@ services: arguments: - !php/const Monolog\Logger::WARNING + Sentry\SentryBundle\Tests\End2End\App\Callback\BeforeSendLogCallback: + autowire: true + + sentry.callback.before_send_log: + class: 'Sentry\SentryBundle\Tests\End2End\App\Callback\BeforeSendLogCallback' + factory: ['@Sentry\SentryBundle\Tests\End2End\App\Callback\BeforeSendLogCallback', 'getCallback'] + Sentry\SentryBundle\Tests\End2End\App\Controller\LoggingController: autowire: true tags: diff --git a/tests/End2End/App/routing.yml b/tests/End2End/App/routing.yml index 2ad7b834..e4494d1e 100644 --- a/tests/End2End/App/routing.yml +++ b/tests/End2End/App/routing.yml @@ -50,6 +50,10 @@ logging_With_Error: path: /logging-with-error defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\LoggingController::loggingWithError' } +logging_before_send_logs: + path: /before-send-log + defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\LoggingController::beforeSendLog' } + buffer_flush: path: /buffer-flush defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\BufferFlushController::testBufferFlush' } diff --git a/tests/End2End/LoggingEnd2EndTest.php b/tests/End2End/LoggingEnd2EndTest.php index 4633b0bb..3ce2ca8d 100644 --- a/tests/End2End/LoggingEnd2EndTest.php +++ b/tests/End2End/LoggingEnd2EndTest.php @@ -57,6 +57,23 @@ public function testExceptionWithLogs(): void $this->assertCount(2, $logsEvent->getLogs()); } + public function testBeforeSendLogCallback() + { + $client = static::createClient(['debug' => false]); + + $client->request('GET', '/before-send-log'); + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $this->assertCount(1, StubTransport::$events); + $event = StubTransport::$events[0]; + $logs = $this->filterFrameworkLogs($event->getLogs()); + + // Make sure we just have two warn logs and no error log (since it's filtered by the callback) + $this->assertCount(2, $logs); + $this->assertEquals('warn 1', $logs[0]->getBody()); + $this->assertEquals('warn 2', $logs[1]->getBody()); + } + /** * Removes framework logs so that the tests can focus on our expected logs. * From e29924f816f3f285a0555fc649012c7d9b84b21c Mon Sep 17 00:00:00 2001 From: Martin Linzmayer Date: Mon, 1 Sep 2025 16:51:54 +0200 Subject: [PATCH 2/2] lints --- src/DependencyInjection/SentryExtension.php | 6 +++--- tests/DependencyInjection/SentryExtensionTest.php | 2 +- tests/End2End/App/Callback/BeforeSendLogCallback.php | 7 ++++--- tests/End2End/App/Controller/LoggingController.php | 6 +++--- tests/End2End/LoggingEnd2EndTest.php | 2 +- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/DependencyInjection/SentryExtension.php b/src/DependencyInjection/SentryExtension.php index 36e4ac06..a127069b 100644 --- a/src/DependencyInjection/SentryExtension.php +++ b/src/DependencyInjection/SentryExtension.php @@ -290,7 +290,7 @@ private function registerHttpClientTracingConfiguration(ContainerBuilder $contai } /** - * @param string[] $integrations + * @param string[] $integrations * @param array $config * * @return array @@ -322,12 +322,12 @@ private function configureRequestIntegration(array $integrations, bool $useDefau /** * @param class-string $integrationClass - * @param array $integrations + * @param array $integrations */ private function isIntegrationEnabled(string $integrationClass, array $integrations): bool { foreach ($integrations as $integration) { - if ($integration instanceof Reference && $integrationClass === (string)$integration) { + if ($integration instanceof Reference && $integrationClass === (string) $integration) { return true; } diff --git a/tests/DependencyInjection/SentryExtensionTest.php b/tests/DependencyInjection/SentryExtensionTest.php index 9a8594c0..ce7e4234 100644 --- a/tests/DependencyInjection/SentryExtensionTest.php +++ b/tests/DependencyInjection/SentryExtensionTest.php @@ -491,7 +491,7 @@ private function createContainerFromFixture(string $fixtureFile): ContainerBuild /** * @param array $methodCall - * @param mixed[] $arguments + * @param mixed[] $arguments */ private function assertDefinitionMethodCallAt(array $methodCall, string $method, array $arguments): void { diff --git a/tests/End2End/App/Callback/BeforeSendLogCallback.php b/tests/End2End/App/Callback/BeforeSendLogCallback.php index a5937bf1..e3fadc91 100644 --- a/tests/End2End/App/Callback/BeforeSendLogCallback.php +++ b/tests/End2End/App/Callback/BeforeSendLogCallback.php @@ -1,20 +1,21 @@ getBody() === "before_send_log") { + if ('before_send_log' === $log->getBody()) { return null; } + return $log; }; } - } diff --git a/tests/End2End/App/Controller/LoggingController.php b/tests/End2End/App/Controller/LoggingController.php index a2e98a84..c62fae6e 100644 --- a/tests/End2End/App/Controller/LoggingController.php +++ b/tests/End2End/App/Controller/LoggingController.php @@ -46,9 +46,9 @@ public function loggingWithError() public function beforeSendLog() { - $this->logger->warning("warn 1"); - $this->logger->error("before_send_log"); - $this->logger->warning("warn 2"); + $this->logger->warning('warn 1'); + $this->logger->error('before_send_log'); + $this->logger->warning('warn 2'); return new Response(); } diff --git a/tests/End2End/LoggingEnd2EndTest.php b/tests/End2End/LoggingEnd2EndTest.php index 3ce2ca8d..314eb817 100644 --- a/tests/End2End/LoggingEnd2EndTest.php +++ b/tests/End2End/LoggingEnd2EndTest.php @@ -57,7 +57,7 @@ public function testExceptionWithLogs(): void $this->assertCount(2, $logsEvent->getLogs()); } - public function testBeforeSendLogCallback() + public function testBeforeSendLogCallback(): void { $client = static::createClient(['debug' => false]);