-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from CMProductions/factory
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace spec\Cmp\Monitoring; | ||
|
||
use Cmp\Monitoring\Monitor; | ||
use Cmp\Monitoring\MonitorFactory; | ||
use PhpSpec\ObjectBehavior; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\LogLevel; | ||
|
||
class MonitorFactorySpec extends ObjectBehavior | ||
{ | ||
function it_can_be_initialized() | ||
{ | ||
$this->shouldHaveType(MonitorFactory::class); | ||
} | ||
|
||
function it_can_built_an_empty_monitor() | ||
{ | ||
$monitor = $this->create(); | ||
$monitor->shouldBeAnInstanceOf(Monitor::class); | ||
} | ||
|
||
function it_can_built_an_working_monitor(LoggerInterface $logger) | ||
{ | ||
$monitor = $this->create([ | ||
'hostname' => 'fooserver', | ||
'default_tags' => ['foo' => 'bar'], | ||
'logger' => [ | ||
'instance' => $logger, | ||
'debug' => true, | ||
'level' => LogLevel::WARNING, | ||
'metrics' => true, | ||
'events' => true, | ||
], | ||
'datadog' => [ | ||
'metrics' => true, | ||
'events' => true, | ||
'host' => '10.0.0.1', | ||
'port' => 8822, | ||
], | ||
]); | ||
|
||
$monitor->shouldBeAnInstanceOf(Monitor::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Cmp\Monitoring; | ||
|
||
use Cmp\Monitoring\Event\EventFactory; | ||
use Cmp\Monitoring\Event\Sender\DataDog as DataDogEvents; | ||
use Cmp\Monitoring\Event\Sender\PsrLogger as PsrLoggerEvents; | ||
use Cmp\Monitoring\Metric\MetricFactory; | ||
use Cmp\Monitoring\Metric\Sender\DataDog as DataDogMetrics; | ||
use Cmp\Monitoring\Metric\Sender\PsrLogger as PsrLoggerMetrics; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\LogLevel; | ||
|
||
class MonitorFactory | ||
{ | ||
/** | ||
* Creates a monitor in a single command by passing a configuration | ||
* | ||
* See the method 'mergeDefaults' for info on the config format | ||
* | ||
* @param array $config | ||
* | ||
* @return Monitor | ||
*/ | ||
public static function create(array $config = []) | ||
{ | ||
$config = self::mergeDefaults($config); | ||
$metricFactory = new MetricFactory($config['default_tags']); | ||
$eventFactory = new EventFactory($config['hostname']); | ||
$logger = $config['logger']['instance'] instanceof LoggerInterface ? $config['logger']['instance'] : null; | ||
$debug = $config['logger']['debug']; | ||
$monitor = new Monitor($metricFactory, $eventFactory, $debug ? $logger : null); | ||
|
||
if (!empty($config['datadog']['metrics'])) { | ||
$monitor->pushMetricSender(DataDogMetrics::create($config['datadog']['host'], $config['datadog']['port'])); | ||
} | ||
|
||
if (!empty($config['datadog']['events'])) { | ||
$monitor->pushEventSender(DataDogEvents::create($config['datadog']['host'], $config['datadog']['port'])); | ||
} | ||
|
||
if ($logger && $config['logger']['metrics']) { | ||
$monitor->pushMetricSender(new PsrLoggerMetrics($logger), $config['logger']['level']); | ||
} | ||
|
||
if ($logger && $config['logger']['events']) { | ||
$monitor->pushEventSender(new PsrLoggerEvents($logger), $config['logger']['level']); | ||
} | ||
|
||
return $monitor; | ||
} | ||
|
||
/** | ||
* @param array $config | ||
* | ||
* @return array | ||
*/ | ||
private static function mergeDefaults(array $config) | ||
{ | ||
return array_merge([ | ||
'hostname' => gethostname(), # Hostname of the server | ||
'default_tags' => [], # A key-value array with default tags for metrics and events | ||
'logger' => [ | ||
'instance' => null, # A Psr\LoggerInterface instance | ||
'debug' => false, # If true, it will log debug messages from the monitor | ||
'level' => LogLevel::INFO, # The level for debug message | ||
'metrics' => false, # If true, metrics will be sent trough the provided logger instance | ||
'events' => false, # If true, events will be sent trough the provided logger instance | ||
], | ||
'datadog' => [ | ||
'metrics' => false, # If true, metrics will be sent trough the datadog agent | ||
'events' => false, # If true, events will be sent trough the datadog agent | ||
'host' => null, # The datadog agent host, if null the default will be used | ||
'port' => null, # The datadog agent port, if null the default will be used | ||
], | ||
], $config); | ||
} | ||
} |