From 87b3b9263bbe728822b6303873d8f46a9cc1f1fe Mon Sep 17 00:00:00 2001 From: Martin Linzmayer Date: Wed, 26 Aug 2026 13:39:59 +0200 Subject: [PATCH] feat: ungate logs and metrics --- src/Logs/LogsAggregator.php | 10 --------- src/Metrics/MetricsAggregator.php | 4 ---- src/Monolog/Handler.php | 4 ++-- src/Options.php | 24 ++++++++++++++++++++-- tests/Logs/LogsAggregatorTest.php | 20 ++++-------------- tests/Logs/LogsTest.php | 33 +++++++++--------------------- tests/Metrics/TraceMetricsTest.php | 6 ++++-- tests/Monolog/LogsHandlerTest.php | 4 ++-- 8 files changed, 44 insertions(+), 61 deletions(-) diff --git a/src/Logs/LogsAggregator.php b/src/Logs/LogsAggregator.php index bc0b5f302..060d425c2 100644 --- a/src/Logs/LogsAggregator.php +++ b/src/Logs/LogsAggregator.php @@ -51,16 +51,6 @@ public function add( $options = $client->getOptions(); $sdkLogger = $options->getLogger(); - if (!$options->getEnableLogs()) { - if ($sdkLogger !== null) { - $sdkLogger->info( - 'Log will be discarded because "enable_logs" is "false".' - ); - } - - return; - } - $formattedMessage = Str::vsprintfOrNull($message, $values); if ($formattedMessage === null) { diff --git a/src/Metrics/MetricsAggregator.php b/src/Metrics/MetricsAggregator.php index a4a3aef3e..0ed9d88b3 100644 --- a/src/Metrics/MetricsAggregator.php +++ b/src/Metrics/MetricsAggregator.php @@ -67,10 +67,6 @@ public function add( $options = $client->getOptions(); $metricFlushThreshold = $options->getMetricFlushThreshold(); - if ($options->getEnableMetrics() === false) { - return; - } - $defaultAttributes = [ 'sentry.environment' => $options->getEnvironment() ?? Event::DEFAULT_ENVIRONMENT, 'server.address' => $options->getServerName(), diff --git a/src/Monolog/Handler.php b/src/Monolog/Handler.php index 6cc69fef7..ee1991f11 100644 --- a/src/Monolog/Handler.php +++ b/src/Monolog/Handler.php @@ -17,8 +17,8 @@ * hub instance. * * @deprecated since version 4.24. To be removed in version 5.0. Use {@see LogsHandler} - * with the `enable_logs` SDK option for Sentry logs, {@see ExceptionToSentryIssueHandler} - * to send Monolog exceptions to Sentry issues, and {@see LogToSentryIssueHandler} + * for Sentry logs, {@see ExceptionToSentryIssueHandler} to send Monolog exceptions + * to Sentry issues, and {@see LogToSentryIssueHandler} * to send Monolog log messages to Sentry issues. * * @author Stefano Arlandini diff --git a/src/Options.php b/src/Options.php index 73d8ffad7..15803a658 100644 --- a/src/Options.php +++ b/src/Options.php @@ -151,7 +151,12 @@ public function getEnableTracing(): ?bool /** * Sets if logs should be enabled or not. * + * This option no longer gates the manual logging API or logging integrations. + * To implement a kill switch, use a `before_send_log` callback that returns `null`. + * * @param bool|null $enableLogs Boolean if logs should be enabled or not + * + * @deprecated since version 4.31. To be removed in version 5.0 */ public function setEnableLogs(?bool $enableLogs): self { @@ -160,6 +165,11 @@ public function setEnableLogs(?bool $enableLogs): self /** * Gets if logs is enabled or not. + * + * This option no longer gates the manual logging API or logging integrations. + * To implement a kill switch, use a `before_send_log` callback that returns `null`. + * + * @deprecated since version 4.31. To be removed in version 5.0 */ public function getEnableLogs(): bool { @@ -212,14 +222,24 @@ public function setMetricFlushThreshold(?int $metricFlushThreshold): self /** * Sets if metrics should be enabled or not. + * + * This option no longer gates the manual metrics API or metrics integrations. + * To implement a kill switch, use a `before_send_metric` callback that returns `null`. + * + * @deprecated since version 4.31. To be removed in version 5.0 */ - public function setEnableMetrics(bool $enableTracing): self + public function setEnableMetrics(bool $enableMetrics): self { - return $this->updateOptions(['enable_metrics' => $enableTracing]); + return $this->updateOptions(['enable_metrics' => $enableMetrics]); } /** * Returns whether metrics are enabled or not. + * + * This option no longer gates the manual metrics API or metrics integrations. + * To implement a kill switch, use a `before_send_metric` callback that returns `null`. + * + * @deprecated since version 4.31. To be removed in version 5.0 */ public function getEnableMetrics(): bool { diff --git a/tests/Logs/LogsAggregatorTest.php b/tests/Logs/LogsAggregatorTest.php index 90be69244..19ccf41fa 100644 --- a/tests/Logs/LogsAggregatorTest.php +++ b/tests/Logs/LogsAggregatorTest.php @@ -31,9 +31,7 @@ final class LogsAggregatorTest extends TestCase */ public function testAttributes(array $attributes, array $expected): void { - $client = ClientBuilder::create([ - 'enable_logs' => true, - ])->getClient(); + $client = ClientBuilder::create()->getClient(); $hub = new Hub($client); SentrySdk::setCurrentHub($hub); @@ -89,9 +87,7 @@ public static function attributesDataProvider(): \Generator */ public function testMessageFormatting(string $message, array $values, string $expected): void { - $client = ClientBuilder::create([ - 'enable_logs' => true, - ])->getClient(); + $client = ClientBuilder::create()->getClient(); $hub = new Hub($client); SentrySdk::setCurrentHub($hub); @@ -163,7 +159,6 @@ public static function messageFormattingDataProvider(): \Generator public function testAttributesAreAddedToLogMessage(): void { $client = ClientBuilder::create([ - 'enable_logs' => true, 'send_default_pii' => true, 'release' => '1.0.0', 'environment' => 'production', @@ -214,7 +209,6 @@ public function testAttributesAreAddedToLogMessage(): void public function testUserAttributesCanBeSetManuallyWithDefaultPiiOff(): void { $client = ClientBuilder::create([ - 'enable_logs' => true, 'send_default_pii' => false, ])->getClient(); @@ -248,7 +242,6 @@ public function testFlushesImmediatelyWhenThresholdIsReached(): void $transport = new StubTransport(); $client = ClientBuilder::create([ - 'enable_logs' => true, 'log_flush_threshold' => 2, ])->setTransport($transport)->getClient(); @@ -277,7 +270,6 @@ public function testDoesNotFlushImmediatelyWhenThresholdIsNull(): void $transport = new StubTransport(); $client = ClientBuilder::create([ - 'enable_logs' => true, 'log_flush_threshold' => null, ])->setTransport($transport)->getClient(); @@ -295,9 +287,7 @@ public function testDoesNotFlushImmediatelyWhenThresholdIsNull(): void public function testDoesNotUsePropagationContextSpanIdAsParentSpanIdWhenNoLocalSpanExists(): void { - $client = ClientBuilder::create([ - 'enable_logs' => true, - ])->getClient(); + $client = ClientBuilder::create()->getClient(); $propagationContext = PropagationContext::fromDefaults(); $propagationContext->setTraceId(new TraceId('771a43a4192642f0b136d5159a501700')); @@ -321,9 +311,7 @@ public function testDoesNotUsePropagationContextSpanIdAsParentSpanIdWhenNoLocalS public function testUsesExternalPropagationContextWhenNoLocalSpanExists(): void { - $client = ClientBuilder::create([ - 'enable_logs' => true, - ])->getClient(); + $client = ClientBuilder::create()->getClient(); $hub = new Hub($client); SentrySdk::setCurrentHub($hub); diff --git a/tests/Logs/LogsTest.php b/tests/Logs/LogsTest.php index 4dd361b56..6feda2bd2 100644 --- a/tests/Logs/LogsTest.php +++ b/tests/Logs/LogsTest.php @@ -11,7 +11,6 @@ use Sentry\Event; use Sentry\Logs\Log; use Sentry\Logs\LogLevel; -use Sentry\Options; use Sentry\SentrySdk; use Sentry\State\Hub; use Sentry\Transport\Result; @@ -22,29 +21,21 @@ final class LogsTest extends TestCase { - public function testLogNotSentWhenDisabled(): void + public function testLogSentWhenEnableLogsIsFalse(): void { - /** @var ClientInterface&MockObject $client */ - $client = $this->createMock(ClientInterface::class); - $client->expects($this->any()) - ->method('getOptions') - ->willReturn(new Options([ - 'dsn' => 'https://public@example.com/1', - 'enable_logs' => false, - ])); - - $client->expects($this->never()) - ->method('captureEvent'); - - $hub = new Hub($client); - SentrySdk::setCurrentHub($hub); + $this->assertEvent(function (Event $event) { + $this->assertCount(1, $event->getLogs()); + $this->assertSame('Some info message', $event->getLogs()[0]->getBody()); + }, [ + 'enable_logs' => false, + ]); logger()->info('Some info message'); - $this->assertNull(logger()->flush()); + $this->assertNotNull(logger()->flush()); } - public function testLogSentWhenEnabled(): void + public function testLogSentWithoutEnableLogsOption(): void { $this->assertEvent(function (Event $event) { $this->assertCount(1, $event->getLogs()); @@ -180,11 +171,7 @@ private function assertEvent(callable $assert, array $options = []): ClientInter return new Result(ResultStatus::success(), $event); }); - $clientOptions = array_merge([ - 'enable_logs' => true, - ], $options); - - $client = ClientBuilder::create($clientOptions)->setTransport($transport)->getClient(); + $client = ClientBuilder::create($options)->setTransport($transport)->getClient(); $hub = new Hub($client); SentrySdk::setCurrentHub($hub); diff --git a/tests/Metrics/TraceMetricsTest.php b/tests/Metrics/TraceMetricsTest.php index 1191e076e..d5aa8b444 100644 --- a/tests/Metrics/TraceMetricsTest.php +++ b/tests/Metrics/TraceMetricsTest.php @@ -129,7 +129,7 @@ public function testMetricsBufferFullWhenMetricFlushThresholdIsNull(): void $this->assertCount(MetricsAggregator::METRICS_BUFFER_SIZE, $metrics); } - public function testEnableMetrics(): void + public function testMetricSentWhenEnableMetricsIsFalse(): void { HubAdapter::getInstance()->bindClient(new Client(new Options([ 'enable_metrics' => false, @@ -138,7 +138,9 @@ public function testEnableMetrics(): void traceMetrics()->count('test-count', 2, ['foo' => 'bar']); traceMetrics()->flush(); - $this->assertEmpty(StubTransport::$events); + $this->assertCount(1, StubTransport::$events); + $this->assertCount(1, StubTransport::$events[0]->getMetrics()); + $this->assertSame('test-count', StubTransport::$events[0]->getMetrics()[0]->getName()); } public function testBeforeSendMetricAltersContent(): void diff --git a/tests/Monolog/LogsHandlerTest.php b/tests/Monolog/LogsHandlerTest.php index b8039d716..d7dcdd427 100644 --- a/tests/Monolog/LogsHandlerTest.php +++ b/tests/Monolog/LogsHandlerTest.php @@ -22,7 +22,7 @@ protected function setUp(): void { Logs::getInstance()->flush(); $client = ClientBuilder::create([ - 'enable_logs' => true, + 'enable_logs' => false, 'before_send' => static function () { return null; // we don't need to send the event, we are just testing the Monolog handler }, @@ -104,7 +104,7 @@ public function testLogsHandlerDestructor(): void { $transport = new StubTransport(); $client = ClientBuilder::create([ - 'enable_logs' => true, + 'enable_logs' => false, ])->setTransport($transport) ->getClient();