From 7a764df48c1e2e1dd83da90a53da66e29bdc32aa Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Tue, 8 Mar 2022 11:28:46 +0000 Subject: [PATCH] Added scoutapp/scout-apm-php version into Scout connection banner if detected --- psalm.xml.dist | 2 +- src/Agent.php | 18 ++++++++++++------ src/Helper/ComposerPackagesCheck.php | 19 +++++++++++++++++++ tests/Unit/AgentTest.php | 6 ++++++ .../Unit/Helper/ComposerPackagesCheckTest.php | 17 +++++++++++++++++ 5 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 tests/Unit/Helper/ComposerPackagesCheckTest.php diff --git a/psalm.xml.dist b/psalm.xml.dist index 17c66507..60328f81 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -1,6 +1,6 @@ checkExtensionVersion(); + $scoutBannerMetadata = sprintf( + 'app=%s, ext=%s, lib=%s', + $this->config->get(ConfigKey::APPLICATION_NAME), + $this->extensionVersion(), + ComposerPackagesCheck::phpLibraryVersion() + ); + if (! $this->connector->connected()) { $this->logger->info(sprintf( - 'Scout Core Agent (app=%s, ext=%s) not connected yet, attempting to start', - $this->config->get(ConfigKey::APPLICATION_NAME), - $this->extensionVersion() + 'Scout Core Agent (%s) not connected yet, attempting to start', + $scoutBannerMetadata )); $coreAgentDownloadPath = $this->config->get(ConfigKey::CORE_AGENT_DIRECTORY) . '/' . $this->config->get(ConfigKey::CORE_AGENT_FULL_NAME); $manager = new AutomaticDownloadAndLaunchManager( @@ -261,9 +268,8 @@ public function connect(): void } } else { $this->logger->debug(sprintf( - 'Scout Core Agent Connected (app=%s, ext=%s)', - $this->config->get(ConfigKey::APPLICATION_NAME), - $this->extensionVersion() + 'Scout Core Agent Connected (%s)', + $scoutBannerMetadata )); } } diff --git a/src/Helper/ComposerPackagesCheck.php b/src/Helper/ComposerPackagesCheck.php index 345ae90f..d561f67e 100644 --- a/src/Helper/ComposerPackagesCheck.php +++ b/src/Helper/ComposerPackagesCheck.php @@ -12,6 +12,7 @@ final class ComposerPackagesCheck { + private const PHP_PACKAGE_NAME = 'scoutapp/scout-apm-php'; private const LARAVEL_PACKAGE_NAME = 'scoutapp/scout-apm-laravel'; private const SYMFONY_PACKAGE_NAME = 'scoutapp/scout-apm-symfony-bundle'; @@ -25,6 +26,24 @@ public static function logIfSymfonyPackageNotPresent(LoggerInterface $log): void self::logIfPackageNotPresent($log, 'Symfony', self::SYMFONY_PACKAGE_NAME); } + public static function phpLibraryVersion(): string + { + if ( + ! class_exists(InstalledVersions::class) + || ! self::packageIsInstalled(self::PHP_PACKAGE_NAME) + ) { + return 'none'; + } + + $version = (string) InstalledVersions::getPrettyVersion(self::PHP_PACKAGE_NAME); + + if ($version === '') { + return 'unknown'; + } + + return $version; + } + private static function logIfPackageNotPresent( LoggerInterface $log, string $frameworkDetected, diff --git a/tests/Unit/AgentTest.php b/tests/Unit/AgentTest.php index c445722c..6c67fde9 100644 --- a/tests/Unit/AgentTest.php +++ b/tests/Unit/AgentTest.php @@ -634,6 +634,10 @@ public function testAgentLogsDebugWhenConnectedToSocket(): void $agent->connect(); + self::assertTrue($this->logger->hasInfoThatContains('not connected yet, attempting to start')); + self::assertTrue($this->logger->hasInfoThatContains('app=My test app')); + self::assertTrue($this->logger->hasInfoThatContains('lib=dev-')); + self::assertTrue($this->logger->hasDebugThatContains('Connected to connector.')); } @@ -659,6 +663,8 @@ public function testAgentLogsDebugWhenAlreadyConnectedToSocket(): void $agent->connect(); self::assertTrue($this->logger->hasDebugThatContains('Scout Core Agent Connected')); + self::assertTrue($this->logger->hasDebugThatContains('app=My test app')); + self::assertTrue($this->logger->hasDebugThatContains('lib=dev-')); } public function testRequestUriCanBeChanged(): void diff --git a/tests/Unit/Helper/ComposerPackagesCheckTest.php b/tests/Unit/Helper/ComposerPackagesCheckTest.php new file mode 100644 index 00000000..85c8a85c --- /dev/null +++ b/tests/Unit/Helper/ComposerPackagesCheckTest.php @@ -0,0 +1,17 @@ +