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 ability to disable instrumentation by name #156

Merged
merged 1 commit into from
Jan 8, 2020
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
12 changes: 12 additions & 0 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use Scoutapm\Logger\FilteredLogLevelDecorator;
use Throwable;
use function count;
use function in_array;
use function is_array;
use function is_string;
use function json_encode;
use function sprintf;
Expand Down Expand Up @@ -322,6 +324,16 @@ public function ignore() : void
$this->isIgnored = true;
}

/** {@inheritDoc} */
public function shouldInstrument(string $functionality) : bool
{
$disabledInstruments = $this->config->get(ConfigKey::DISABLED_INSTRUMENTS);

return $disabledInstruments === null
|| ! is_array($disabledInstruments)
|| ! in_array($functionality, $disabledInstruments, true);
}

/** {@inheritDoc} */
public function changeRequestUri(string $newRequestUri) : void
{
Expand Down
1 change: 1 addition & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function __construct()
$this->coercions = [
ConfigKey::MONITORING_ENABLED => new CoerceBoolean(),
ConfigKey::IGNORED_ENDPOINTS => new CoerceJson(),
ConfigKey::DISABLED_INSTRUMENTS => new CoerceJson(),
];
}

Expand Down
2 changes: 2 additions & 0 deletions src/Config/ConfigKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ abstract class ConfigKey
public const SCM_SUBDIRECTORY = 'scm_subdirectory';
public const REVISION_SHA = 'revision_sha';
public const HOSTNAME = 'hostname';
public const DISABLED_INSTRUMENTS = 'disabled_instruments';
public const CORE_AGENT_LOG_LEVEL = 'core_agent_log_level';
public const CORE_AGENT_LOG_FILE = 'core_agent_log_file';
public const CORE_AGENT_CONFIG_FILE = 'core_agent_config_file';
Expand Down Expand Up @@ -52,6 +53,7 @@ public static function allConfigurationKeys() : array
self::SCM_SUBDIRECTORY,
self::REVISION_SHA,
self::HOSTNAME,
self::DISABLED_INSTRUMENTS,
self::CORE_AGENT_LOG_LEVEL,
self::CORE_AGENT_LOG_FILE,
self::CORE_AGENT_CONFIG_FILE,
Expand Down
8 changes: 8 additions & 0 deletions src/ScoutApmAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public function ignored(string $path) : bool;
*/
public function ignore() : void;

/**
* Should the instrumentation be enabled for a particular functionality. This checks the `disabled_instruments`
* configuration - if an instrumentation is not explicitly disabled, this will return true.
*
* The list of functionality that can be disabled depends on the library binding being used.
*/
public function shouldInstrument(string $functionality) : bool;

/**
* If the automatically determined request URI is incorrect, please report an issue so we can investigate. You may
* override the automatic determination of the request URI by calling this method.
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/AgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,38 @@ public function testIgnoredEndpoints() : void
self::assertFalse($agent->ignored('/bar'));
}

public function testInstrumentationIsNotDisabledWhenNoDisabledInstrumentsConfigured() : void
{
self::assertTrue(
$this->agentFromConfigArray([])
->shouldInstrument('some functionality')
);
}

public function testInstrumentationIsNotDisabledWhenDisabledInstrumentsConfigurationIsWrong() : void
{
self::assertTrue(
$this->agentFromConfigArray([ConfigKey::DISABLED_INSTRUMENTS => 'disabled functionality'])
->shouldInstrument('some functionality')
);
}

public function testInstrumentationIsNotDisabledWhenDisabledInstrumentsAreConfigured() : void
{
self::assertTrue(
$this->agentFromConfigArray([ConfigKey::DISABLED_INSTRUMENTS => '["disabled functionality"]'])
->shouldInstrument('some functionality')
);
}

public function testInstrumentationIsDisabledWhenDisabledInstrumentsAreConfigured() : void
{
self::assertFalse(
$this->agentFromConfigArray([ConfigKey::DISABLED_INSTRUMENTS => '["disabled functionality"]'])
->shouldInstrument('disabled functionality')
);
}

/** @throws Exception */
public function testMetadataExceptionsAreLogged() : void
{
Expand Down