From ed9573d070bcf4c3e208c10074b535263adc73ea Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Mon, 9 Dec 2019 08:56:14 +0000 Subject: [PATCH 1/2] Added metadata exception catch - still send request even if we failed to gather metadata --- src/Agent.php | 20 +++++++++++------- tests/Unit/AgentTest.php | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/Agent.php b/src/Agent.php index fdf75c34..b8f8377f 100644 --- a/src/Agent.php +++ b/src/Agent.php @@ -29,6 +29,7 @@ use Scoutapm\Extension\ExtentionCapabilities; use Scoutapm\Extension\PotentiallyAvailableExtensionCapabilities; use Scoutapm\Logger\FilteredLogLevelDecorator; +use Throwable; use function is_string; use function json_encode; use function sprintf; @@ -353,16 +354,19 @@ private function sendMetadataIfRequired() : void return; } - if (! $this->connector->sendCommand(new Metadata( - new DateTimeImmutable('now', new DateTimeZone('UTC')), - $this->config - ))) { - $this->logger->debug('Send command returned false for Metadata'); + try { + $this->connector->sendCommand(new Metadata( + new DateTimeImmutable('now', new DateTimeZone('UTC')), + $this->config + )); - return; + $this->markMetadataSent(); + } catch (Throwable $exception) { + $this->logger->notice( + sprintf('Sending metadata raised an exception: %s', $exception->getMessage()), + ['exception' => $exception] + ); } - - $this->markMetadataSent(); } private function metadataWasSent() : bool diff --git a/tests/Unit/AgentTest.php b/tests/Unit/AgentTest.php index 9e13fb0e..598ad3eb 100644 --- a/tests/Unit/AgentTest.php +++ b/tests/Unit/AgentTest.php @@ -4,20 +4,28 @@ namespace Scoutapm\UnitTests; +use OutOfBoundsException; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; use Psr\Log\NullLogger; +use Psr\Log\Test\TestLogger; use Scoutapm\Agent; +use Scoutapm\Cache\DevNullCache; use Scoutapm\Config; use Scoutapm\Config\ConfigKey; +use Scoutapm\Connector\Connector; +use Scoutapm\Events\Metadata; +use Scoutapm\Events\RegisterMessage; +use Scoutapm\Events\Request\Request; use Scoutapm\Events\Span\Span; use Scoutapm\Events\Tag\TagRequest; use function array_map; use function count; use function end; use function sprintf; +use function uniqid; /** @covers \Scoutapm\Agent */ final class AgentTest extends TestCase @@ -271,6 +279,43 @@ public function testIgnoredEndpoints() : void self::assertFalse($agent->ignored('/bar')); } + public function testMetadataExceptionsAreLogged() : void + { + $logger = new TestLogger(); + $connector = $this->createMock(Connector::class); + + $agent = Agent::fromConfig( + Config::fromArray([ + ConfigKey::APPLICATION_NAME => 'My test app', + ConfigKey::APPLICATION_KEY => uniqid('applicationKey', true), + ConfigKey::MONITORING_ENABLED => true, + ConfigKey::LOG_LEVEL => LogLevel::NOTICE, + ]), + $logger, + new DevNullCache(), + $connector + ); + + $connector->method('connected')->wilLReturn(true); + + $connector->expects(self::at(1)) + ->method('sendCommand') + ->with(self::isInstanceOf(RegisterMessage::class)) + ->willReturn('{"Register":"success"}'); + $connector->expects(self::at(2)) + ->method('sendCommand') + ->with(self::isInstanceOf(Metadata::class)) + ->willThrowException(new OutOfBoundsException('Some obscure exception happened')); + $connector->expects(self::at(3)) + ->method('sendCommand') + ->with(self::isInstanceOf(Request::class)) + ->willReturn('{"Request":"success"}'); + + $agent->send(); + + self::assertTrue($logger->hasNoticeThatContains('Sending metadata raised an exception: Some obscure exception happened')); + } + /** * Many instrumentation calls are NOOPs when ignore is called. Make sure the sequence works as expected */ From 22e20af873a7a2336b7d8c55f9d9e7f4887100a7 Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Mon, 9 Dec 2019 16:21:27 +0000 Subject: [PATCH 2/2] Fix method name typo --- tests/Unit/AgentTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/AgentTest.php b/tests/Unit/AgentTest.php index 598ad3eb..98e0917a 100644 --- a/tests/Unit/AgentTest.php +++ b/tests/Unit/AgentTest.php @@ -296,7 +296,7 @@ public function testMetadataExceptionsAreLogged() : void $connector ); - $connector->method('connected')->wilLReturn(true); + $connector->method('connected')->willReturn(true); $connector->expects(self::at(1)) ->method('sendCommand')