Skip to content

Commit 98b5f29

Browse files
authored
Merge pull request #156 from scoutapp/configuration-to-disable-instruments
Added ability to disable instrumentation by name
2 parents bc87ce0 + 808b9b1 commit 98b5f29

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

src/Agent.php

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
use Scoutapm\Logger\FilteredLogLevelDecorator;
3333
use Throwable;
3434
use function count;
35+
use function in_array;
36+
use function is_array;
3537
use function is_string;
3638
use function json_encode;
3739
use function sprintf;
@@ -322,6 +324,16 @@ public function ignore() : void
322324
$this->isIgnored = true;
323325
}
324326

327+
/** {@inheritDoc} */
328+
public function shouldInstrument(string $functionality) : bool
329+
{
330+
$disabledInstruments = $this->config->get(ConfigKey::DISABLED_INSTRUMENTS);
331+
332+
return $disabledInstruments === null
333+
|| ! is_array($disabledInstruments)
334+
|| ! in_array($functionality, $disabledInstruments, true);
335+
}
336+
325337
/** {@inheritDoc} */
326338
public function changeRequestUri(string $newRequestUri) : void
327339
{

src/Config.php

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function __construct()
4949
$this->coercions = [
5050
ConfigKey::MONITORING_ENABLED => new CoerceBoolean(),
5151
ConfigKey::IGNORED_ENDPOINTS => new CoerceJson(),
52+
ConfigKey::DISABLED_INSTRUMENTS => new CoerceJson(),
5253
];
5354
}
5455

src/Config/ConfigKey.php

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ abstract class ConfigKey
2121
public const SCM_SUBDIRECTORY = 'scm_subdirectory';
2222
public const REVISION_SHA = 'revision_sha';
2323
public const HOSTNAME = 'hostname';
24+
public const DISABLED_INSTRUMENTS = 'disabled_instruments';
2425
public const CORE_AGENT_LOG_LEVEL = 'core_agent_log_level';
2526
public const CORE_AGENT_LOG_FILE = 'core_agent_log_file';
2627
public const CORE_AGENT_CONFIG_FILE = 'core_agent_config_file';
@@ -52,6 +53,7 @@ public static function allConfigurationKeys() : array
5253
self::SCM_SUBDIRECTORY,
5354
self::REVISION_SHA,
5455
self::HOSTNAME,
56+
self::DISABLED_INSTRUMENTS,
5557
self::CORE_AGENT_LOG_LEVEL,
5658
self::CORE_AGENT_LOG_FILE,
5759
self::CORE_AGENT_CONFIG_FILE,

src/ScoutApmAgent.php

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public function ignored(string $path) : bool;
5959
*/
6060
public function ignore() : void;
6161

62+
/**
63+
* Should the instrumentation be enabled for a particular functionality. This checks the `disabled_instruments`
64+
* configuration - if an instrumentation is not explicitly disabled, this will return true.
65+
*
66+
* The list of functionality that can be disabled depends on the library binding being used.
67+
*/
68+
public function shouldInstrument(string $functionality) : bool;
69+
6270
/**
6371
* If the automatically determined request URI is incorrect, please report an issue so we can investigate. You may
6472
* override the automatic determination of the request URI by calling this method.

tests/Unit/AgentTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,38 @@ public function testIgnoredEndpoints() : void
345345
self::assertFalse($agent->ignored('/bar'));
346346
}
347347

348+
public function testInstrumentationIsNotDisabledWhenNoDisabledInstrumentsConfigured() : void
349+
{
350+
self::assertTrue(
351+
$this->agentFromConfigArray([])
352+
->shouldInstrument('some functionality')
353+
);
354+
}
355+
356+
public function testInstrumentationIsNotDisabledWhenDisabledInstrumentsConfigurationIsWrong() : void
357+
{
358+
self::assertTrue(
359+
$this->agentFromConfigArray([ConfigKey::DISABLED_INSTRUMENTS => 'disabled functionality'])
360+
->shouldInstrument('some functionality')
361+
);
362+
}
363+
364+
public function testInstrumentationIsNotDisabledWhenDisabledInstrumentsAreConfigured() : void
365+
{
366+
self::assertTrue(
367+
$this->agentFromConfigArray([ConfigKey::DISABLED_INSTRUMENTS => '["disabled functionality"]'])
368+
->shouldInstrument('some functionality')
369+
);
370+
}
371+
372+
public function testInstrumentationIsDisabledWhenDisabledInstrumentsAreConfigured() : void
373+
{
374+
self::assertFalse(
375+
$this->agentFromConfigArray([ConfigKey::DISABLED_INSTRUMENTS => '["disabled functionality"]'])
376+
->shouldInstrument('disabled functionality')
377+
);
378+
}
379+
348380
/** @throws Exception */
349381
public function testMetadataExceptionsAreLogged() : void
350382
{

0 commit comments

Comments
 (0)