Skip to content

Commit

Permalink
Added configuration option for errors_filtered_params
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed Jan 11, 2022
1 parent 3788206 commit 7441783
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function __construct()
ConfigKey::URI_FILTERED_PARAMETERS => new CoerceJson(),
ConfigKey::ERRORS_ENABLED => new CoerceBoolean(),
ConfigKey::ERRORS_IGNORED_EXCEPTIONS => new CoerceJson(),
ConfigKey::ERRORS_FILTERED_PARAMETERS => new CoerceJson(),
];
}

Expand Down
6 changes: 6 additions & 0 deletions src/Config/ConfigKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ abstract class ConfigKey
public const ERRORS_IGNORED_EXCEPTIONS = 'errors_ignored_exceptions';
public const ERRORS_HOST = 'errors_host';
public const ERRORS_BATCH_SIZE = 'errors_batch_size';
public const ERRORS_FILTERED_PARAMETERS = 'errors_filtered_params';

private const SECRET_CONFIGURATIONS = [self::APPLICATION_KEY];

Expand Down Expand Up @@ -83,6 +84,11 @@ public static function allConfigurationKeys(): array
self::CORE_AGENT_PERMISSIONS,
self::URI_REPORTING,
self::URI_FILTERED_PARAMETERS,
self::ERRORS_ENABLED,
self::ERRORS_IGNORED_EXCEPTIONS,
self::ERRORS_HOST,
self::ERRORS_BATCH_SIZE,
self::ERRORS_FILTERED_PARAMETERS,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
use function is_array;
use function is_string;

final class RequireValidFilteredUriParameters
final class RequireValidFilteredParameters
{
/** @psalm-return list<string> */
public static function fromConfig(Config $config): array
/**
* @psalm-param ConfigKey::URI_FILTERED_PARAMETERS|ConfigKey::ERRORS_FILTERED_PARAMETERS $filteredParametersConfigKey
* @psalm-return list<string>
*/
private static function fromConfigForGivenKey(Config $config, string $filteredParametersConfigKey): array
{
/** @var mixed $uriFilteredParameters */
$uriFilteredParameters = $config->get(ConfigKey::URI_FILTERED_PARAMETERS);
$uriFilteredParameters = $config->get($filteredParametersConfigKey);

/** @var list<string> $defaultFilteredParameters */
$defaultFilteredParameters = (new Config\Source\DefaultSource())->get(ConfigKey::URI_FILTERED_PARAMETERS);
$defaultFilteredParameters = (new Config\Source\DefaultSource())->get($filteredParametersConfigKey);

if (! is_array($uriFilteredParameters)) {
return $defaultFilteredParameters;
Expand All @@ -35,4 +38,16 @@ public static function fromConfig(Config $config): array
/** @psalm-var array<array-key, string> $uriFilteredParameters */
return array_values($uriFilteredParameters);
}

/** @psalm-return list<string> */
public static function fromConfigForErrors(Config $config): array
{
return self::fromConfigForGivenKey($config, ConfigKey::ERRORS_FILTERED_PARAMETERS);
}

/** @psalm-return list<string> */
public static function fromConfigForUris(Config $config): array
{
return self::fromConfigForGivenKey($config, ConfigKey::URI_FILTERED_PARAMETERS);
}
}
6 changes: 6 additions & 0 deletions src/Config/Source/DefaultSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ private function getDefaultConfig(): array
ConfigKey::ERRORS_HOST => 'https://errors.scoutapm.com',
ConfigKey::ERRORS_IGNORED_EXCEPTIONS => [],
ConfigKey::ERRORS_BATCH_SIZE => 5,
/** @link https://scoutapm.com/docs/ruby/configuration#errors_filtered_params */
ConfigKey::ERRORS_FILTERED_PARAMETERS => [
'password',
's3-key',
'scout_key',
],
ConfigKey::URI_REPORTING => ConfigKey::URI_REPORTING_PATH_ONLY,
/** @link https://github.com/scoutapp/scout_apm_python/blob/ee3c4fe47e6d841b243935628bb21191ee59da4b/src/scout_apm/core/web_requests.py#L15-L40 */
ConfigKey::URI_FILTERED_PARAMETERS => [
Expand Down
2 changes: 1 addition & 1 deletion src/Errors/ErrorEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private function buildRequestUri(): string
*/
public function toJsonableArray(Config $config, array $session, array $env, array $request): array
{
$filteredParameters = Config\Helper\RequireValidFilteredUriParameters::fromConfig($config);
$filteredParameters = Config\Helper\RequireValidFilteredParameters::fromConfigForErrors($config);

$controllerName = explode(
'/',
Expand Down
2 changes: 1 addition & 1 deletion src/Events/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static function fromConfigAndOverrideTime(Config $config, ?float $overrid
{
return new self(
self::requireValidUriReportingValue($config),
Config\Helper\RequireValidFilteredUriParameters::fromConfig($config),
Config\Helper\RequireValidFilteredParameters::fromConfigForUris($config),
$override
);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Helper/FilterParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function is_object;
use function is_scalar;
use function sprintf;
use function strtolower;

use const ARRAY_FILTER_USE_KEY;

Expand All @@ -30,10 +31,12 @@ final class FilterParameters
*/
public static function forUriReportingConfiguration(array $parameterKeysToBeFiltered, array $parameters): array
{
$lowercasedParameterKeysToBeFiltered = array_map('strtolower', $parameterKeysToBeFiltered);

return array_filter(
$parameters,
static function (string $key) use ($parameterKeysToBeFiltered): bool {
return ! in_array($key, $parameterKeysToBeFiltered, true);
static function (string $key) use ($lowercasedParameterKeysToBeFiltered): bool {
return ! in_array(strtolower($key), $lowercasedParameterKeysToBeFiltered, true);
},
ARRAY_FILTER_USE_KEY
);
Expand Down

0 comments on commit 7441783

Please sign in to comment.