Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added scoutapp/scout-apm-php version into Scout connection banner if detected #259

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
totallyTyped="true"
errorLevel="1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
18 changes: 12 additions & 6 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Scoutapm\Extension\ExtensionCapabilities;
use Scoutapm\Extension\PotentiallyAvailableExtensionCapabilities;
use Scoutapm\Extension\Version;
use Scoutapm\Helper\ComposerPackagesCheck;
use Scoutapm\Helper\DetermineHostname\DetermineHostnameWithConfigOverride;
use Scoutapm\Helper\FindApplicationRoot\FindApplicationRootWithConfigOverride;
use Scoutapm\Helper\FindRequestHeaders\FindRequestHeaders;
Expand Down Expand Up @@ -217,11 +218,17 @@ public function connect(): void

$this->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(
Expand Down Expand Up @@ -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
));
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/Helper/ComposerPackagesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/AgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'));
}

Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Helper/ComposerPackagesCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Scoutapm\UnitTests\Helper;

use PHPUnit\Framework\TestCase;
use Scoutapm\Helper\ComposerPackagesCheck;

/** @covers \Scoutapm\Helper\ComposerPackagesCheck */
final class ComposerPackagesCheckTest extends TestCase
{
public function testPhpLibraryVersion(): void
{
self::assertStringStartsWith('dev-', ComposerPackagesCheck::phpLibraryVersion());
}
}