From acd90ca0b545888af33be3ac8e37202023e01b1a Mon Sep 17 00:00:00 2001 From: Hilari Moragrega Date: Fri, 9 Mar 2018 16:11:54 +0100 Subject: [PATCH] Prefix (#5) * Prefixes for metrics --- README.md | 7 ++++--- spec/Metric/MetricFactorySpec.php | 8 +++++--- spec/MonitorFactorySpec.php | 2 ++ src/Metric/MetricFactory.php | 31 ++++++++++++++++++++++++------- src/MonitorFactory.php | 3 ++- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index ff4f1de..026ec4b 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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']); @@ -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 ], diff --git a/spec/Metric/MetricFactorySpec.php b/spec/Metric/MetricFactorySpec.php index 093ddf6..8e8fd2b 100644 --- a/spec/Metric/MetricFactorySpec.php +++ b/spec/Metric/MetricFactorySpec.php @@ -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() @@ -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() diff --git a/spec/MonitorFactorySpec.php b/spec/MonitorFactorySpec.php index e269b19..f7bedb3 100644 --- a/spec/MonitorFactorySpec.php +++ b/spec/MonitorFactorySpec.php @@ -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, @@ -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, diff --git a/src/Metric/MetricFactory.php b/src/Metric/MetricFactory.php index 0ccea85..db6b7d3 100644 --- a/src/Metric/MetricFactory.php +++ b/src/Metric/MetricFactory.php @@ -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; } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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; } } diff --git a/src/MonitorFactory.php b/src/MonitorFactory.php index 309adaf..b34568b 100644 --- a/src/MonitorFactory.php +++ b/src/MonitorFactory.php @@ -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']; @@ -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