Skip to content

Commit

Permalink
Merge pull request #4 from CMProductions/factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilari Moragrega authored Mar 9, 2018
2 parents 698d850 + 6d6713f commit 30e8cae
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
46 changes: 46 additions & 0 deletions spec/MonitorFactorySpec.php
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);
}
}
15 changes: 15 additions & 0 deletions src/Integrations/DataDogClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ public function __construct(
$this->port = $port;
}

/**
* @param string $host
* @param int $port
*
* @return static
*/
public static function create($host = self::DATADOG_DEFAULT_SERVER, $port = self::DATADOG_DEFAULT_PORT)
{
return new static(
new Socket(),
$host ? $host : self::DATADOG_DEFAULT_SERVER,
$port ? $port : self::DATADOG_DEFAULT_PORT
);
}

/**
* Send the message over UDP
*
Expand Down
78 changes: 78 additions & 0 deletions src/MonitorFactory.php
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);
}
}

0 comments on commit 30e8cae

Please sign in to comment.