Skip to content

Commit

Permalink
Merge pull request #115 from scoutapp/more-config-and-debug
Browse files Browse the repository at this point in the history
More config and debug
  • Loading branch information
Chris Schneider authored Nov 22, 2019
2 parents 8eec0cb + acfbfa8 commit 3824a3d
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 69 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### Changed

- Added `language_version` key to be sent with metadata (#110)
- Added more debug logging to isolate issues easier (#111)
- Added more debug logging to isolate issues easier (#111, #115)

## [1.0.0] 2019-11-05

Expand Down
23 changes: 22 additions & 1 deletion src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Scoutapm\Extension\PotentiallyAvailableExtensionCapabilities;
use Scoutapm\Logger\FilteredLogLevelDecorator;
use function is_string;
use function json_encode;
use function sprintf;

final class Agent implements ScoutApmAgent
Expand Down Expand Up @@ -130,6 +131,8 @@ public static function fromConfig(Config $config, ?LoggerInterface $logger = nul

public function connect() : void
{
$this->logger->debug('Configuration: ' . json_encode($this->config->asArrayWithSecretsRemoved()));

if (! $this->enabled()) {
$this->logger->debug('Connection skipped, since monitoring is disabled');

Expand Down Expand Up @@ -282,15 +285,21 @@ public function send() : bool
{
// Don't send if the agent is not enabled.
if (! $this->enabled()) {
$this->logger->debug('Not sending payload, logging is not enabled');

return false;
}

// Don't send it if the request was ignored
if ($this->isIgnored) {
$this->logger->debug('Not sending payload, request has been ignored');

return false;
}

if ($this->request === null) {
$this->logger->debug('Not sending payload, request was not set');

// @todo throw exception? return false?
return false;
}
Expand All @@ -312,19 +321,31 @@ public function send() : bool
(string) $this->config->get(ConfigKey::APPLICATION_KEY),
$this->config->get(ConfigKey::API_VERSION)
))) {
$this->logger->debug('Send command returned false for RegisterMessage');

return false;
}

if (! $this->connector->sendCommand(new Metadata(
new DateTimeImmutable('now', new DateTimeZone('UTC')),
$this->config
))) {
$this->logger->debug('Send command returned false for Metadata');

return false;
}

$this->request->stopIfRunning();

return $this->connector->sendCommand($this->request);
if (! $this->connector->sendCommand($this->request)) {
$this->logger->debug('Send command returned false for Request');

return false;
}

$this->logger->debug('Sent whole payload successfully to core agent.');

return true;
} catch (NotConnected $notConnected) {
$this->logger->error($notConnected->getMessage());

Expand Down
26 changes: 26 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use Scoutapm\Config\TypeCoercion\CoerceBoolean;
use Scoutapm\Config\TypeCoercion\CoerceJson;
use Scoutapm\Config\TypeCoercion\CoerceType;
use function array_combine;
use function array_key_exists;
use function array_map;

// @todo needs interface
class Config
Expand Down Expand Up @@ -96,4 +98,28 @@ public function set(string $key, $value) : void
{
$this->userSettingsSource->set($key, $value);
}

/**
* Return the configuration **WITH ALL SECRETS REMOVED**. This must never return "secrets" (such as API
* keys).
*
* @return mixed[]
*
* @psalm-return array<string, mixed>
*/
public function asArrayWithSecretsRemoved() : array
{
$keys = ConfigKey::allConfigurationKeys();

return ConfigKey::filterSecretsFromConfigArray(array_combine(
$keys,
array_map(
/** @return mixed */
function (string $key) {
return $this->get($key);
},
$keys
)
));
}
}
67 changes: 67 additions & 0 deletions src/Config/ConfigKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace Scoutapm\Config;

use function array_combine;
use function array_keys;
use function array_map;
use function in_array;

abstract class ConfigKey
{
public const MONITORING_ENABLED = 'monitor';
Expand All @@ -28,4 +33,66 @@ abstract class ConfigKey
public const CORE_AGENT_VERSION = 'core_agent_version';
public const CORE_AGENT_TRIPLE = 'core_agent_triple';
public const CORE_AGENT_PERMISSIONS = 'core_agent_permissions';

private const SECRET_CONFIGURATIONS = [self::APPLICATION_KEY];

/** @return string[] */
public static function allConfigurationKeys() : array
{
return [
self::MONITORING_ENABLED,
self::APPLICATION_NAME,
self::APPLICATION_KEY,
self::LOG_LEVEL,
self::API_VERSION,
self::IGNORED_ENDPOINTS,
self::APPLICATION_ROOT,
self::SCM_SUBDIRECTORY,
self::REVISION_SHA,
self::HOSTNAME,
self::CORE_AGENT_LOG_LEVEL,
self::CORE_AGENT_LOG_FILE,
self::CORE_AGENT_CONFIG_FILE,
self::CORE_AGENT_SOCKET_PATH,
self::CORE_AGENT_DIRECTORY,
self::CORE_AGENT_FULL_NAME,
self::CORE_AGENT_DOWNLOAD_URL,
self::CORE_AGENT_LAUNCH_ENABLED,
self::CORE_AGENT_DOWNLOAD_ENABLED,
self::CORE_AGENT_VERSION,
self::CORE_AGENT_TRIPLE,
self::CORE_AGENT_PERMISSIONS,
];
}

/**
* @param mixed[] $configArray
*
* @return mixed[]
*
* @psalm-param array<string, mixed> $configArray
* @psalm-return array<string, mixed>
*/
public static function filterSecretsFromConfigArray(array $configArray) : array
{
return array_combine(
array_keys($configArray),
array_map(
/**
* @param mixed $v
*
* @return mixed
*/
static function (string $k, $v) {
if (in_array($k, self::SECRET_CONFIGURATIONS, true)) {
return '<redacted>';
}

return $v;
},
array_keys($configArray),
$configArray
)
);
}
}
22 changes: 22 additions & 0 deletions src/Config/Source/ConfigSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Scoutapm\Config\Source;

interface ConfigSource
{
/**
* Returns true if this config source knows for certain it has an answer for this key
*/
public function hasKey(string $key) : bool;

/**
* Returns the value for this configuration key.
*
* Only valid if the Source has previously returned "true" to `hasKey`
*
* @return mixed
*/
public function get(string $key);
}
16 changes: 4 additions & 12 deletions src/Config/Source/DefaultSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,23 @@
use function array_key_exists;

/** @internal */
class DefaultSource
final class DefaultSource implements ConfigSource
{
/** @var array<string, (string|bool|array<int, string>|int)> */
/** @var array<string, mixed> */
private $defaults;

public function __construct()
{
$this->defaults = $this->getDefaultConfig();
}

/**
* Returns true iff this config source knows for certain it has an answer for this key
*/
/** @inheritDoc */
public function hasKey(string $key) : bool
{
return array_key_exists($key, $this->defaults);
}

/**
* Returns the value for this configuration key.
*
* Only valid if the Source has previously returned "true" to `hasKey`
*
* @return string|bool|array<int, string>|int|null
*/
/** @inheritDoc */
public function get(string $key)
{
return $this->defaults[$key] ?? null;
Expand Down
24 changes: 12 additions & 12 deletions src/Config/Source/DerivedSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@

use Scoutapm\Config;
use Scoutapm\Config\ConfigKey;
use function in_array;
use function php_uname;

/** @internal */
class DerivedSource
final class DerivedSource implements ConfigSource
{
private const SUPPORTED_DERIVED_KEYS = [
ConfigKey::CORE_AGENT_SOCKET_PATH,
ConfigKey::CORE_AGENT_FULL_NAME,
ConfigKey::CORE_AGENT_TRIPLE,
'testing',
];

/** @var Config */
private $config;

Expand All @@ -27,21 +35,13 @@ public function __construct(Config $config)
$this->config = $config;
}

/**
* Returns true if this config source knows for certain it has an answer for this key
*/
/** @inheritDoc */
public function hasKey(string $key) : bool
{
return $this->get($key) !== null;
return in_array($key, self::SUPPORTED_DERIVED_KEYS, true);
}

/**
* Returns the value for this configuration key.
*
* Only valid if the Source has previously returned "true" to `hasKey`
*
* @return mixed
*/
/** @inheritDoc */
public function get(string $key)
{
switch ($key) {
Expand Down
20 changes: 6 additions & 14 deletions src/Config/Source/EnvSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,17 @@
use function strtoupper;

/** @internal */
class EnvSource
final class EnvSource implements ConfigSource
{
/**
* Returns true iff this config source knows for certain it has an answer for this key
*/
private const SCOUT_PREFIX = 'SCOUT_';

/** @inheritDoc */
public function hasKey(string $key) : bool
{
return getenv($this->envVarName($key)) !== false;
}

/**
* Returns the value for this configuration key.
*
* Only valid if the Source has previously returned "true" to `hasKey`
*
* @return mixed
*/
/** @inheritDoc */
public function get(string $key)
{
$value = getenv($this->envVarName($key));
Expand All @@ -46,8 +40,6 @@ public function get(string $key)

private function envVarName(string $key) : string
{
$upper = strtoupper($key);

return 'SCOUT_' . $upper;
return self::SCOUT_PREFIX . strtoupper($key);
}
}
16 changes: 3 additions & 13 deletions src/Config/Source/NullSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,15 @@
namespace Scoutapm\Config\Source;

/** @internal */
class NullSource
final class NullSource implements ConfigSource
{
/**
* Returns true
*
* @return bool (always True)
*/
/** @inheritDoc */
public function hasKey(string $key) : bool
{
return true;
}

/**
* Returns the value for this configuration key.
*
* Only valid if the Source has previously returned "true" to `hasKey`
*
* @return null
*/
/** @inheritDoc */
public function get(string $key)
{
return null;
Expand Down
Loading

0 comments on commit 3824a3d

Please sign in to comment.