Skip to content

Commit

Permalink
Prefix (#5)
Browse files Browse the repository at this point in the history
* Prefixes for metrics
  • Loading branch information
Hilari Moragrega authored Mar 9, 2018
1 parent b6bced7 commit acd90ca
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The event entity describes an occurred event at one specific moment.
### Creating the entities
There are two factories created to facilitate the creation of the entities, both allow the creation of the entities and provide default data and tags for them in one step

- **MetricFactory**: Allows the creation of metrics with default tags
- **MetricFactory**: Allows the creation of metrics with default tags and prefixes
- **EventFactory**: Allow the creation of Event entities with default host and tags

*NOTE:* Both factories allow to add more default tags after they are instantiated using the method:
Expand Down Expand Up @@ -179,7 +179,7 @@ For using the monitor the typical steps are:
This code is a simplified demonstration of the setup process without the object dependencies.
```php
// Build the metric factory with your choosen default tags
$metricFactory = new MetricFactory(['environment' => 'dev', 'mode' => 'production']);
$metricFactory = new MetricFactory(['environment' => 'dev', 'mode' => 'production'], 'myapp-prefix.');

// Build the event factory with the host name and your choosen default tags
$eventFactory = new EventFactory('my_docker_hostname', ['environment' => 'dev', 'mode' => 'production', 'domain' => 'my_domain']);
Expand Down Expand Up @@ -207,10 +207,11 @@ $monitor->increment('wh.page_views');
$monitor = MonitorFactory::create([
'hostname' => 'fooserver', # Hostname of the server
'default_tags' => ['foo' => 'bar'], # A key-value array with default tags for metrics and events
'prefix' => 'my-app.', # A prefix for the metrics
'logger' => [
'instance' => $logger, # A Psr\LoggerInterface instance
'debug' => true, # If true, it will log debug messages from the monitor
'level' => LogLevel::DEBUG, # The level for debug message
'level' => LogLevel::DEBUG, # The level for debug message
'metrics' => true, # If true, metrics will be sent trough the provided logger instance
'events' => true, # If true, events will be sent trough the provided logger instance
],
Expand Down
8 changes: 5 additions & 3 deletions spec/Metric/MetricFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MetricFactorySpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith(array('default_tag' => 'default'));
$this->beConstructedWith(array('default_tag' => 'default'), 'prefix.');
}

function it_can_be_initialized()
Expand All @@ -19,8 +19,10 @@ function it_can_be_initialized()

function it_can_create_counters()
{
$this->counter('name', 10, array('tags'), .5)
->shouldReturnAnInstanceOf('\Cmp\Monitoring\Metric\Type\Counter');
$counter = $this->counter('name', 10, array('tags'), .5);
$counter->shouldBeAnInstanceOf('\Cmp\Monitoring\Metric\Type\Counter');

$counter->getName()->shouldReturn('prefix.name');
}

function it_can_create_gauges()
Expand Down
2 changes: 2 additions & 0 deletions spec/MonitorFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function it_can_built_an_working_monitor(LoggerInterface $logger)
$monitor = $this->create([
'hostname' => 'fooserver',
'default_tags' => ['foo' => 'bar'],
'prefix' => 'my-app.',
'logger' => [
'instance' => $logger,
'debug' => true,
Expand All @@ -49,6 +50,7 @@ function it_can_built_an_working_monitor_from_half_config(LoggerInterface $logge
$monitor = $this->create([
'hostname' => 'fooserver',
'default_tags' => ['foo' => 'bar'],
'prefix' => 'my-app.',
'logger' => [
'instance' => $logger,
'debug' => true,
Expand Down
31 changes: 24 additions & 7 deletions src/Metric/MetricFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ class MetricFactory extends AbstractHasDefaultTags
const TIMER = 'timer';

/**
* @param array $defaultTags
* @var string
*/
public function __construct(array $defaultTags = array())
private $prefix;

/**
* @param array $defaultTags
* @param string $prefix
*/
public function __construct(array $defaultTags = array(), $prefix = "")
{
$this->defaultTags = $defaultTags;
$this->prefix = $prefix;
}

/**
Expand All @@ -43,7 +50,7 @@ public function __construct(array $defaultTags = array())
*/
public function counter($metric, $count = 1, array $tags = array(), $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
{
return new Counter($metric, $count, array_merge($this->defaultTags, $tags), $sampleRate);
return new Counter($this->prefix($metric), $count, array_merge($this->defaultTags, $tags), $sampleRate);
}

/**
Expand All @@ -58,7 +65,7 @@ public function counter($metric, $count = 1, array $tags = array(), $sampleRate
*/
public function gauge($metric, $level, array $tags = array(), $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
{
return new Gauge($metric, $level, array_merge($this->defaultTags, $tags), $sampleRate);
return new Gauge($this->prefix($metric), $level, array_merge($this->defaultTags, $tags), $sampleRate);
}

/**
Expand All @@ -73,7 +80,7 @@ public function gauge($metric, $level, array $tags = array(), $sampleRate = Abst
*/
public function histogram($metric, $duration = null, array $tags = array(), $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
{
return new Histogram($metric, $duration, array_merge($this->defaultTags, $tags), $sampleRate);
return new Histogram($this->prefix($metric), $duration, array_merge($this->defaultTags, $tags), $sampleRate);
}

/**
Expand All @@ -88,7 +95,7 @@ public function histogram($metric, $duration = null, array $tags = array(), $sam
*/
public function timer($metric, $duration = null, array $tags = array(), $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
{
return new Timer($metric, $duration, array_merge($this->defaultTags, $tags), $sampleRate);
return new Timer($this->prefix($metric), $duration, array_merge($this->defaultTags, $tags), $sampleRate);
}

/**
Expand All @@ -103,6 +110,16 @@ public function timer($metric, $duration = null, array $tags = array(), $sampleR
*/
public function set($metric, $uniqueValue, array $tags =array(), $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
{
return new Set($metric, $uniqueValue, array_merge($this->defaultTags, $tags), $sampleRate);
return new Set($this->prefix($metric), $uniqueValue, array_merge($this->defaultTags, $tags), $sampleRate);
}

/**
* @param string $metric
*
* @return string
*/
private function prefix($metric)
{
return $this->prefix ? $this->prefix.$metric : $metric;
}
}
3 changes: 2 additions & 1 deletion src/MonitorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MonitorFactory
public static function create(array $config = [])
{
$config = self::mergeDefaults($config);
$metricFactory = new MetricFactory($config['default_tags']);
$metricFactory = new MetricFactory($config['default_tags'], $config['prefix']);
$eventFactory = new EventFactory($config['hostname']);
$logger = $config['logger']['instance'] instanceof LoggerInterface ? $config['logger']['instance'] : null;
$debug = $config['logger']['debug'];
Expand Down Expand Up @@ -60,6 +60,7 @@ private static function mergeDefaults(array $config)
return array_replace_recursive([
'hostname' => gethostname(), # Hostname of the server
'default_tags' => [], # A key-value array with default tags for metrics and events
'prefix' => '', # A prefix for the metrics
'logger' => [
'instance' => null, # A Psr\LoggerInterface instance
'debug' => false, # If true, it will log debug messages from the monitor
Expand Down

0 comments on commit acd90ca

Please sign in to comment.