From ef6d66bf672d1c08dfe1ee94e0f03f4963ba4fa8 Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Mon, 2 Nov 2020 09:40:55 +0000 Subject: [PATCH 1/2] Set a TTL to cached metadata --- src/Agent.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Agent.php b/src/Agent.php index 847251b6..3115750b 100644 --- a/src/Agent.php +++ b/src/Agent.php @@ -46,6 +46,8 @@ final class Agent implements ScoutApmAgent { private const CACHE_KEY_METADATA_SENT = 'scout_metadata_sent'; + private const METADATA_CACHE_TTL_SECONDS = 600; + private const WARN_WHEN_EXTENSION_IS_OLDER_THAN = '1.0.2'; /** @var Config */ @@ -533,7 +535,7 @@ private function markMetadataSent() : void return; } - $this->cache->set(self::CACHE_KEY_METADATA_SENT, true); + $this->cache->set(self::CACHE_KEY_METADATA_SENT, true, self::METADATA_CACHE_TTL_SECONDS); } /** From f9dbd03c9436c70ab100398030e6360356a3e6e8 Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Mon, 2 Nov 2020 09:52:34 +0000 Subject: [PATCH 2/2] Add test to ensure metadata is not sent twice --- tests/Unit/AgentTest.php | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/Unit/AgentTest.php b/tests/Unit/AgentTest.php index 07931947..7de3d991 100644 --- a/tests/Unit/AgentTest.php +++ b/tests/Unit/AgentTest.php @@ -4,12 +4,14 @@ namespace Scoutapm\UnitTests; +use Doctrine\Common\Cache\ArrayCache; use Exception; use OutOfBoundsException; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LogLevel; use Psr\Log\Test\TestLogger; +use Roave\DoctrineSimpleCache\SimpleCacheAdapter; use Scoutapm\Agent; use Scoutapm\Cache\DevNullCache; use Scoutapm\Config; @@ -902,4 +904,57 @@ public function testNumberOfSpansIsLimitedAndNoticeIsLogged() : void self::assertTrue($this->logger->hasNoticeThatContains('Span limit of 1500 has been reached trying to start span for "span 1500"')); } + + public function testMetadataIsNotSentIfCached() : void + { + $agent = Agent::fromConfig( + Config::fromArray([ + ConfigKey::APPLICATION_NAME => 'My test app', + ConfigKey::APPLICATION_KEY => uniqid('applicationKey', true), + ConfigKey::MONITORING_ENABLED => true, + ]), + $this->logger, + new SimpleCacheAdapter(new ArrayCache()), + $this->connector, + $this->phpExtension + ); + + $this->connector->expects(self::at(0)) + ->method('connected') + ->willReturn(true); + + /** @noinspection PhpParamsInspection */ + $this->connector->expects(self::at(1)) + ->method('sendCommand') + ->with(self::isInstanceOf(RegisterMessage::class)) + ->willReturn('{"Register":"Success"}'); + /** @noinspection PhpParamsInspection */ + $this->connector->expects(self::at(2)) + ->method('sendCommand') + ->with(self::isInstanceOf(Metadata::class)) + ->willReturn('{"Metadata":"Success"}'); + /** @noinspection PhpParamsInspection */ + $this->connector->expects(self::at(3)) + ->method('sendCommand') + ->with(self::isInstanceOf(Request::class)) + ->willReturn('{"Request":"Success"}'); + $this->connector->expects(self::at(4)) + ->method('connected') + ->willReturn(true); + /** @noinspection PhpParamsInspection */ + $this->connector->expects(self::at(5)) + ->method('sendCommand') + ->with(self::isInstanceOf(Request::class)) + ->willReturn('{"Request":"Success"}'); + + $agent->startSpan('a'); + $agent->stopSpan(); + + $agent->send(); + + $agent->startSpan('b'); + $agent->stopSpan(); + + $agent->send(); + } }