From d12e37d79b6241d320e6c001318549833de50520 Mon Sep 17 00:00:00 2001 From: o0h Date: Sat, 29 May 2021 19:28:13 +0900 Subject: [PATCH 1/3] Pass the Logger Context when calling capture() --- src/Http/Client.php | 5 ++++- tests/TestCase/Http/ClientTest.php | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Http/Client.php b/src/Http/Client.php index c9ac467..6da5bd7 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -87,7 +87,6 @@ public function capture($level, string $message, array $context): void $lastEventId = $this->hub->captureException($exception); } else { $hint = new EventHint(); - $hint->extra = $context; $stackTrace = $context['stackTrace'] ?? false; if ($stackTrace) { @@ -96,7 +95,11 @@ public function capture($level, string $message, array $context): void $stackTrace[0]['file'], $stackTrace[0]['line'] ); + unset($context['stackTrace']); } + $this->hub->configureScope(function (\Sentry\State\Scope $scope) use ($context) { + $scope->setExtras($context); + }); $severity = $this->convertLevelToSeverity($level); $lastEventId = $this->hub->captureMessage($message, $severity, $hint); diff --git a/tests/TestCase/Http/ClientTest.php b/tests/TestCase/Http/ClientTest.php index 21bd7b0..3227fb7 100644 --- a/tests/TestCase/Http/ClientTest.php +++ b/tests/TestCase/Http/ClientTest.php @@ -243,6 +243,29 @@ public function testCaptureErrorBuildBreadcrumbs(): void $subject->capture('warning', 'some error', compact('stackTrace')); } + /** + * Test capture error pass cakephp-log's context as additional data + */ + public function testCaptureErrorWithAdditionalData(): void + { + $callback = function (\Sentry\Event $event, ?\Sentry\EventHint $hint) use (&$actualEvent) { + $actualEvent = $event; + }; + + $userConfig = [ + 'dsn' => false, + 'before_send' => $callback, + ]; + + Configure::write('Sentry', $userConfig); + $subject = new Client([]); + + $context = ['this is' => 'additional']; + $subject->capture('warning', 'some error', $context); + + $this->assertSame($context, $actualEvent->getExtra()); + } + /** * Check capture dispatch beforeCapture */ From 7780d06a3727f6b3d2e6c375689fdb7789d84c7f Mon Sep 17 00:00:00 2001 From: o0h Date: Sat, 29 May 2021 19:35:47 +0900 Subject: [PATCH 2/3] Change the description to consider use cases other than error logs. --- tests/TestCase/Http/ClientTest.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/TestCase/Http/ClientTest.php b/tests/TestCase/Http/ClientTest.php index 3227fb7..b9a8597 100644 --- a/tests/TestCase/Http/ClientTest.php +++ b/tests/TestCase/Http/ClientTest.php @@ -151,11 +151,11 @@ public function testCaptureException(): void } /** - * Test capture error + * Test capture other than exception * * @return array // FIXME: In fact array, but getMethodProphecies declare as MethodProphecy[] */ - public function testCaptureError(): array + public function testCaptureNotHavingException(): array { $subject = new Client([]); $sentryClientP = $this->prophesize(ClientInterface::class); @@ -185,14 +185,14 @@ public function testCaptureError(): array } /** - * Test capture error compatible with the error-level is specified by int or string + * Test capture compatible with the error-level is specified by int or string * - * @depends testCaptureError + * @depends testCaptureNotHavingException * @param array&array $mockMethodList */ - public function testCaptureErrorWithErrorLevelInteger(array $mockMethodList): void + public function testCaptureWithErrorLevelInteger(array $mockMethodList): void { - // Rebuild ObjectProphecy in the same context with testCaptureError. + // Rebuild ObjectProphecy in the same context with testCaptureNotHavingException. $sentryClientP = $this->prophesize(ClientInterface::class); foreach ($mockMethodList as $mockMethod) { $sentryClientP->addMethodProphecy($mockMethod[0]); @@ -205,9 +205,9 @@ public function testCaptureErrorWithErrorLevelInteger(array $mockMethodList): vo } /** - * Test capture error fill with injected breadcrumbs + * Test capture fill with injected breadcrumbs */ - public function testCaptureErrorBuildBreadcrumbs(): void + public function testCaptureBuildBreadcrumbs(): void { $stackTrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT); @@ -244,9 +244,9 @@ public function testCaptureErrorBuildBreadcrumbs(): void } /** - * Test capture error pass cakephp-log's context as additional data + * Test capture pass cakephp-log's context as additional data */ - public function testCaptureErrorWithAdditionalData(): void + public function testCaptureWithAdditionalData(): void { $callback = function (\Sentry\Event $event, ?\Sentry\EventHint $hint) use (&$actualEvent) { $actualEvent = $event; From a234f00e57c1ef4c0b54558c94fecfb391f563a2 Mon Sep 17 00:00:00 2001 From: o0h Date: Sat, 29 May 2021 19:35:59 +0900 Subject: [PATCH 3/3] Fix annotation --- src/Http/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Client.php b/src/Http/Client.php index 6da5bd7..1fd858a 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -72,7 +72,7 @@ public function getHub(): Hub /** * Capture exception for sentry. * - * @param mixed $level error level + * @param string|int $level error level * @param string $message error message * @param array $context subject * @return void