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

#82 - configurable scout log level #83

Merged
merged 7 commits into from
Oct 8, 2019
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
23 changes: 21 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@

### Added

- New `\Scoutapm\Config\ConfigKey` class containing `public const`s for configuration key names (#83)
- Added config key `log_level` which overrides Scout APM's minimum log level (#83)

### Fixed

### Changed

- **[BC]** Renamed the following configuration keys (#83)
- `log_level` => `core_agent_log_level`
- `log_file` => `core_agent_log_file`
- `config_file` => `core_agent_config_file`
- `socket_path` => `core_agent_socket_path`
- `download_url` => `core_agent_download_url`
- Improved stack trace filtering (#61)

## [0.2.2] 2019-09-26

### Fixed

- Corrected naming of core agent config values (#80)

## [0.2.1] 2019-09-25

### Changed
Expand All @@ -16,8 +35,8 @@

### Changed

- Internal data model now perserves order (#47)
- Loosen several depencency version requirements (#50)
- Internal data model now preserves order (#47)
- Loosen several dependency version requirements (#50)
- Licensed as MIT
- Initial support for Scout Native Extension (#42, #54)

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ To install the ScoutAPM Agent for a specific framework, use the specific package
```php
use Scoutapm\Agent;
use Scoutapm\Config;
use Scoutapm\Config\ConfigKey;

$agent = Agent::fromConfig(Config::fromArray([
'name' => 'Your application name',
'key' => 'your scout key',
'monitor' => true,
ConfigKey::APPLICATION_NAME => 'Your application name',
ConfigKey::APPLICATION_KEY => 'your scout key',
ConfigKey::MONITORING_ENABLED => true,
]));
// If the core agent is not already running, this will download and run it (from /tmp by default)
$agent->connect();
Expand Down
27 changes: 18 additions & 9 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use DateTimeZone;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Scoutapm\Config\ConfigKey;
use Scoutapm\Config\IgnoredEndpoints;
use Scoutapm\Connector\Connector;
use Scoutapm\Connector\Exception\FailedToConnect;
Expand All @@ -24,6 +25,7 @@
use Scoutapm\Events\Span\Span;
use Scoutapm\Extension\ExtentionCapabilities;
use Scoutapm\Extension\PotentiallyAvailableExtensionCapabilities;
use Scoutapm\Logger\FilteredLogLevelDecorator;

final class Agent implements ScoutApmAgent
{
Expand Down Expand Up @@ -63,14 +65,21 @@ public function __construct(Config $configuration, Connector $connector, LoggerI
$this->logger = $logger;
$this->phpExtension = $phpExtension;

if (! $this->logger instanceof FilteredLogLevelDecorator) {
$this->logger = new FilteredLogLevelDecorator(
$this->logger,
$this->config->get(ConfigKey::LOG_LEVEL)
);
}

$this->request = new Request();

$this->ignoredEndpoints = new IgnoredEndpoints($configuration->get('ignore'));
$this->ignoredEndpoints = new IgnoredEndpoints($configuration->get(ConfigKey::IGNORED_ENDPOINTS));
}

private static function createConnectorFromConfig(Config $config) : SocketConnector
{
return new SocketConnector($config->get('socket_path'));
return new SocketConnector($config->get(ConfigKey::CORE_AGENT_SOCKET_PATH));
}

/**
Expand Down Expand Up @@ -108,10 +117,10 @@ public function connect() : void
$this->config,
$this->logger,
new Downloader(
$this->config->get('core_agent_dir') . '/' . $this->config->get('core_agent_full_name'),
$this->config->get('core_agent_full_name'),
$this->config->get(ConfigKey::CORE_AGENT_DIRECTORY) . '/' . $this->config->get(ConfigKey::CORE_AGENT_FULL_NAME),
$this->config->get(ConfigKey::CORE_AGENT_FULL_NAME),
$this->logger,
$this->config->get('download_url')
$this->config->get(ConfigKey::CORE_AGENT_DOWNLOAD_URL)
)
);
$manager->launch();
Expand All @@ -135,7 +144,7 @@ public function connect() : void
/** {@inheritDoc} */
public function enabled() : bool
{
return $this->config->get('monitor');
return $this->config->get(ConfigKey::MONITORING_ENABLED);
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -258,9 +267,9 @@ public function send() : bool

try {
if (! $this->connector->sendCommand(new RegisterMessage(
(string) $this->config->get('name'),
(string) $this->config->get('key'),
$this->config->get('api_version')
(string) $this->config->get(ConfigKey::APPLICATION_NAME),
(string) $this->config->get(ConfigKey::APPLICATION_KEY),
$this->config->get(ConfigKey::API_VERSION)
))) {
return false;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Scoutapm;

use Scoutapm\Config\ConfigKey;
use Scoutapm\Config\Source\DefaultSource;
use Scoutapm\Config\Source\DerivedSource;
use Scoutapm\Config\Source\EnvSource;
Expand Down Expand Up @@ -43,8 +44,8 @@ public function __construct()
];

$this->coercions = [
'monitor' => new CoerceBoolean(),
'ignore' => new CoerceJson(),
ConfigKey::MONITORING_ENABLED => new CoerceBoolean(),
ConfigKey::IGNORED_ENDPOINTS => new CoerceJson(),
];
}

Expand Down
26 changes: 26 additions & 0 deletions src/Config/ConfigKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Scoutapm\Config;

abstract class ConfigKey
{
public const MONITORING_ENABLED = 'monitor';
public const APPLICATION_NAME = 'name';
public const APPLICATION_KEY = 'key';
public const LOG_LEVEL = 'log_level';
public const API_VERSION = 'api_version';
public const IGNORED_ENDPOINTS = 'ignore';
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';
public const CORE_AGENT_SOCKET_PATH = 'core_agent_socket_path';
public const CORE_AGENT_DIRECTORY = 'core_agent_dir';
public const CORE_AGENT_FULL_NAME = 'core_agent_full_name';
public const CORE_AGENT_DOWNLOAD_URL = 'core_agent_download_url';
public const CORE_AGENT_LAUNCH_ENABLED = 'core_agent_launch';
public const CORE_AGENT_DOWNLOAD_ENABLED = 'core_agent_download';
public const CORE_AGENT_VERSION = 'core_agent_version';
public const CORE_AGENT_TRIPLE = 'core_agent_triple';
}
18 changes: 10 additions & 8 deletions src/Config/Source/DefaultSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Scoutapm\Config\Source;

use Scoutapm\Config\ConfigKey;
use function array_key_exists;

/** @internal */
Expand Down Expand Up @@ -53,14 +54,15 @@ public function get(string $key)
private function getDefaultConfig() : array
{
return [
'api_version' => '1.0',
'core_agent_dir' => '/tmp/scout_apm_core',
'core_agent_download' => true,
'core_agent_launch' => true,
'core_agent_version' => 'v1.2.2',
'download_url' => 'https://s3-us-west-1.amazonaws.com/scout-public-downloads/apm_core_agent/release',
'monitor' => false,
'ignore' => [],
ConfigKey::API_VERSION => '1.0',
ConfigKey::CORE_AGENT_DIRECTORY => '/tmp/scout_apm_core',
ConfigKey::CORE_AGENT_DOWNLOAD_ENABLED => true,
ConfigKey::CORE_AGENT_LAUNCH_ENABLED => true,
ConfigKey::CORE_AGENT_VERSION => 'v1.2.2',
ConfigKey::CORE_AGENT_DOWNLOAD_URL => 'https://s3-us-west-1.amazonaws.com/scout-public-downloads/apm_core_agent/release',
ConfigKey::MONITORING_ENABLED => false,
ConfigKey::IGNORED_ENDPOINTS => [],
ConfigKey::LOG_LEVEL => 'debug',
];
}
}
17 changes: 9 additions & 8 deletions src/Config/Source/DerivedSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Scoutapm\Config\Source;

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

/** @internal */
Expand Down Expand Up @@ -44,11 +45,11 @@ public function hasKey(string $key) : bool
public function get(string $key)
{
switch ($key) {
case 'socket_path':
case ConfigKey::CORE_AGENT_SOCKET_PATH:
return $this->socketPath();
case 'core_agent_full_name':
case ConfigKey::CORE_AGENT_FULL_NAME:
return $this->coreAgentFullName();
case 'core_agent_triple':
case ConfigKey::CORE_AGENT_TRIPLE:
return $this->coreAgentTriple();
case 'testing':
return $this->testing();
Expand All @@ -59,17 +60,17 @@ public function get(string $key)

private function socketPath() : string
{
$dir = $this->config->get('core_agent_dir');
$fullName = $this->config->get('core_agent_full_name');
$dir = $this->config->get(ConfigKey::CORE_AGENT_DIRECTORY);
$fullName = $this->config->get(ConfigKey::CORE_AGENT_FULL_NAME);

return $dir . '/' . $fullName . '/scout-agent.sock';
}

private function coreAgentFullName() : string
{
$name = 'scout_apm_core';
$version = $this->config->get('core_agent_version');
$triple = $this->config->get('core_agent_triple');
$version = $this->config->get(ConfigKey::CORE_AGENT_VERSION);
$triple = $this->config->get(ConfigKey::CORE_AGENT_TRIPLE);

return $name . '-' . $version . '-' . $triple;
}
Expand Down Expand Up @@ -100,7 +101,7 @@ private function coreAgentTriple() : string
*/
private function testing() : string
{
$version = $this->config->get('api_version');
$version = $this->config->get(ConfigKey::API_VERSION);

return 'derived api version: ' . $version;
}
Expand Down
28 changes: 17 additions & 11 deletions src/CoreAgent/AutomaticDownloadAndLaunchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

use Psr\Log\LoggerInterface;
use Scoutapm\Config;
use Scoutapm\Config\ConfigKey;
use Throwable;
use function array_map;
use function exec;
use function file_get_contents;
use function hash;
use function implode;
use function sprintf;

/** @internal */
final class AutomaticDownloadAndLaunchManager implements Manager
Expand All @@ -35,24 +37,28 @@ public function __construct(Config $config, LoggerInterface $logger, Downloader
{
$this->config = $config;
$this->logger = $logger;
$this->coreAgentDir = $config->get('core_agent_dir') . '/' . $config->get('core_agent_full_name');
$this->coreAgentDir = $config->get(ConfigKey::CORE_AGENT_DIRECTORY) . '/' . $config->get(ConfigKey::CORE_AGENT_FULL_NAME);

$this->downloader = $downloader;
}

public function launch() : bool
{
if (! $this->config->get('core_agent_launch')) {
$this->logger->debug("Not attempting to launch Core Agent due to 'core_agent_launch' setting.");
if (! $this->config->get(ConfigKey::CORE_AGENT_LAUNCH_ENABLED)) {
$this->logger->debug(sprintf(
"Not attempting to launch Core Agent due to '%s' setting.",
ConfigKey::CORE_AGENT_LAUNCH_ENABLED
));

return false;
}

if (! $this->verify()) {
if (! $this->config->get('core_agent_download')) {
$this->logger->debug(
"Not attempting to download Core Agent due to 'core_agent_download' setting."
);
if (! $this->config->get(ConfigKey::CORE_AGENT_DOWNLOAD_ENABLED)) {
$this->logger->debug(sprintf(
"Not attempting to download Core Agent due to '%s' setting.",
ConfigKey::CORE_AGENT_DOWNLOAD_ENABLED
));

return false;
}
Expand Down Expand Up @@ -108,9 +114,9 @@ private function run() : bool
{
$this->logger->debug('Core Agent Launch in Progress');
try {
$logLevel = $this->config->get('log_level');
$logFile = $this->config->get('log_file');
$configFile = $this->config->get('config_file');
$logLevel = $this->config->get(ConfigKey::CORE_AGENT_LOG_LEVEL);
$logFile = $this->config->get(ConfigKey::CORE_AGENT_LOG_FILE);
$configFile = $this->config->get(ConfigKey::CORE_AGENT_CONFIG_FILE);

if ($logFile === null) {
$logFile = '/dev/null';
Expand All @@ -136,7 +142,7 @@ private function run() : bool
}

$commandParts[] = '--socket';
$commandParts[] = $this->config->get('socket_path');
$commandParts[] = $this->config->get(ConfigKey::CORE_AGENT_SOCKET_PATH);

$escapedCommand = implode(' ', array_map('escapeshellarg', $commandParts));

Expand Down
57 changes: 57 additions & 0 deletions src/Logger/FilteredLogLevelDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Scoutapm\Logger;

use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Psr\Log\LogLevel;
use Webmozart\Assert\Assert;
use function strtolower;

/**
* This log decorator is used to squelch log messages below a configured threshold.
*/
final class FilteredLogLevelDecorator implements LoggerInterface
{
use LoggerTrait;

private const LOG_LEVEL_ORDER = [
LogLevel::DEBUG => 0,
LogLevel::INFO => 1,
LogLevel::NOTICE => 2,
LogLevel::WARNING => 3,
LogLevel::ERROR => 4,
LogLevel::CRITICAL => 5,
LogLevel::ALERT => 6,
LogLevel::EMERGENCY => 7,
];

/** @var LoggerInterface */
private $realLogger;

/** @var int */
private $minimumLogLevel;

/**
* @param string $minimumLogLevel e.g. `emergency`, `error`, etc. - {@see \Psr\Log\LogLevel}
*/
public function __construct(LoggerInterface $realLogger, string $minimumLogLevel)
{
Assert::keyExists(self::LOG_LEVEL_ORDER, strtolower($minimumLogLevel));

$this->minimumLogLevel = self::LOG_LEVEL_ORDER[strtolower($minimumLogLevel)];
$this->realLogger = $realLogger;
}

/** {@inheritDoc} */
public function log($level, $message, array $context = [])
{
if ($this->minimumLogLevel > self::LOG_LEVEL_ORDER[$level]) {
return;
}

$this->realLogger->log($level, $message, $context);
}
}
Loading