Skip to content

Commit

Permalink
Merge pull request #278 from scoutapp/do-not-invoke-extension-if-moni…
Browse files Browse the repository at this point in the history
…toring-disabled

Do not enable scoutapm extension if monitoring disabled
  • Loading branch information
asgrim authored Jul 6, 2022
2 parents 06f318b + a3f64f4 commit fc23306
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ jobs:
extensions: ${{ matrix.extensions }}
env:
fail-fast: true
- name: "Composer allow-plugins to allow Laravel to actually install"
run: composer config --global allow-plugins true
- name: "Install Laravel quickstart project"
run: "composer create-project laravel/laravel:${{ matrix.laravel-version}} test-app --prefer-dist"
- name: "Add scout-apm-php as a repository"
Expand Down Expand Up @@ -649,6 +651,8 @@ jobs:
extensions: ${{ matrix.extensions }}
env:
fail-fast: true
- name: "Composer allow-plugins to allow Laravel to actually install"
run: composer config --global allow-plugins true
- name: "Install Lumen quickstart project"
run: "composer create-project laravel/lumen:${{ matrix.lumen-version}} test-app --prefer-dist"
- name: "Add scout-apm-php as a repository"
Expand Down
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@
"config": {
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/package-versions-deprecated": true
}
"allow-plugins": true
}
}
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Scoutapm\Events\Span\Span;
use Scoutapm\Events\Span\SpanReference;
use Scoutapm\Events\Tag\Tag;
use Scoutapm\Extension\DoNotInvokeAnyExtensionCapabilities;
use Scoutapm\Extension\ExtensionCapabilities;
use Scoutapm\Extension\PotentiallyAvailableExtensionCapabilities;
use Scoutapm\Extension\Version;
Expand Down Expand Up @@ -149,6 +150,40 @@ private static function createConnectorFromConfig(Config $config): SocketConnect
);
}

/**
* The {@see PotentiallyAvailableExtensionCapabilities} constructor implementation automatically invokes the
* {@see \scoutapm_enable_instrumentation()} function, which enables the extension, if it is available. However, if
* the extension is present, but monitoring is disabled, the extension is still enabled. Therefore, when setting up
* the Agent, if monitoring is NOT enabled, then use the new alternate implementation in
* {@see DoNotInvokeAnyExtensionCapabilities} which is essentially a no-op. This means the extension will remain
* dormant if monitoring is disabled.
*/
private static function createExtensionCapabilitiesFromConfig(Config $config): ExtensionCapabilities
{
return $config->get(ConfigKey::MONITORING_ENABLED)
? new PotentiallyAvailableExtensionCapabilities()
: new DoNotInvokeAnyExtensionCapabilities();
}

/**
* The only required arguments are the {@see Config}, and a {@see LoggerInterface} implementation.
*
* @example
*
* $agent = Agent::fromConfig(
* Config::fromArray([]), // Uses default configuration, which can be overridden with environment variables
* $logger // PSR-3 compatible logger, e.g. Monolog, or most framework loggers
* );
*
* If any of the other parameters are not provided, default configuration is used, which should be fine for most
* usual cases. Default implementations are as follows:
*
* - $cache - {@see DevNullCache} - i.e. no caching
* - $connector - {@see SocketConnector} - supports both UNIX sockets and TCP sockets
* - $extensionCapabilities - {@see PotentiallyAvailableExtensionCapabilities} or {@see DoNotInvokeAnyExtensionCapabilities} depending on configuration
* - $locateFileOrFolder - {@see LocateFileOrFolderUsingFilesystem}
* - $errorHandling - {@see ScoutErrorHandling} or {@see NoErrorHandling} depending on configuration
*/
public static function fromConfig(
Config $config,
LoggerInterface $logger,
Expand All @@ -171,7 +206,7 @@ public static function fromConfig(
$config,
$connector ?? self::createConnectorFromConfig($config),
$logger,
$extensionCapabilities ?? new PotentiallyAvailableExtensionCapabilities(),
$extensionCapabilities ?? self::createExtensionCapabilitiesFromConfig($config),
$cache ?? new DevNullCache(),
$locateFileOrFolder ?? new LocateFileOrFolderUsingFilesystem(),
$errorHandling ?? ErrorHandlingDiscoveryFactory::createAndListen($config, $logger, $superglobals),
Expand Down
26 changes: 26 additions & 0 deletions src/Extension/DoNotInvokeAnyExtensionCapabilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Scoutapm\Extension;

final class DoNotInvokeAnyExtensionCapabilities implements ExtensionCapabilities
{
/** @return array<empty, empty> */
public function getCalls(): array
{
// Intentionally, there are no calls
return [];
}

public function clearRecordedCalls(): void
{
// Intentially no-op, don't invoke the extension
}

public function version(): ?Version
{
// Extension is ignored/doesn't exist, so no version to return
return null;
}
}
33 changes: 33 additions & 0 deletions tests/Unit/Extension/DoNotInvokeAnyExtensionCapabilitiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Scoutapm\UnitTests\Extension;

use PHPUnit\Framework\TestCase;
use Scoutapm\Extension\DoNotInvokeAnyExtensionCapabilities;

use function file_get_contents;

/** @covers \Scoutapm\Extension\DoNotInvokeAnyExtensionCapabilities */
final class DoNotInvokeAnyExtensionCapabilitiesTest extends TestCase
{
public function testClearRecordedCalls(): void
{
$capabilities = new DoNotInvokeAnyExtensionCapabilities();
self::assertEquals([], $capabilities->getCalls());
$capabilities->clearRecordedCalls();
self::assertEquals([], $capabilities->getCalls());
}

public function testGetCalls(): void
{
file_get_contents(__FILE__);
self::assertEquals([], (new DoNotInvokeAnyExtensionCapabilities())->getCalls());
}

public function testVersion(): void
{
self::assertNull((new DoNotInvokeAnyExtensionCapabilities())->version());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
use Scoutapm\Logger\FilteredLogLevelDecorator;

use function class_exists;
use function mkdir;
use function sprintf;
use function sys_get_temp_dir;
use function uniqid;

use const DIRECTORY_SEPARATOR;

/** @covers \Scoutapm\Laravel\Providers\ScoutApmServiceProvider */
final class ScoutApmServiceProviderUsingLaravelTest extends ScoutApmServiceProviderTestBase
Expand Down Expand Up @@ -64,6 +68,10 @@ static function () use ($application): FilteredLogLevelDecorator {
}
);

$storagePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('scoutapm-test-laravel-storage', true) . DIRECTORY_SEPARATOR;
mkdir($storagePath);
$application->useStoragePath($storagePath);

$application->singleton(
HttpKernelInterface::class,
function () use ($application): HttpKernelInterface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
use Scoutapm\Logger\FilteredLogLevelDecorator;

use function class_exists;
use function method_exists;
use function mkdir;
use function sprintf;
use function sys_get_temp_dir;
use function uniqid;

use const DIRECTORY_SEPARATOR;

/** @covers \Scoutapm\Laravel\Providers\ScoutApmServiceProvider */
final class ScoutApmServiceProviderUsingLumenTest extends ScoutApmServiceProviderTestBase
Expand Down Expand Up @@ -80,6 +85,12 @@ static function () use ($application): FilteredLogLevelDecorator {
}
);

if (method_exists($application, 'useStoragePath')) {
$storagePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('scoutapm-test-lumen-storage', true) . DIRECTORY_SEPARATOR;
mkdir($storagePath);
$application->useStoragePath($storagePath);
}

$application->make('view');
$application->singleton(
'view',
Expand Down

0 comments on commit fc23306

Please sign in to comment.