Skip to content

Commit acd90ca

Browse files
author
Hilari Moragrega
authored
Prefix (#5)
* Prefixes for metrics
1 parent b6bced7 commit acd90ca

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The event entity describes an occurred event at one specific moment.
5353
### Creating the entities
5454
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
5555

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

5959
*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:
179179
This code is a simplified demonstration of the setup process without the object dependencies.
180180
```php
181181
// Build the metric factory with your choosen default tags
182-
$metricFactory = new MetricFactory(['environment' => 'dev', 'mode' => 'production']);
182+
$metricFactory = new MetricFactory(['environment' => 'dev', 'mode' => 'production'], 'myapp-prefix.');
183183

184184
// Build the event factory with the host name and your choosen default tags
185185
$eventFactory = new EventFactory('my_docker_hostname', ['environment' => 'dev', 'mode' => 'production', 'domain' => 'my_domain']);
@@ -207,10 +207,11 @@ $monitor->increment('wh.page_views');
207207
$monitor = MonitorFactory::create([
208208
'hostname' => 'fooserver', # Hostname of the server
209209
'default_tags' => ['foo' => 'bar'], # A key-value array with default tags for metrics and events
210+
'prefix' => 'my-app.', # A prefix for the metrics
210211
'logger' => [
211212
'instance' => $logger, # A Psr\LoggerInterface instance
212213
'debug' => true, # If true, it will log debug messages from the monitor
213-
'level' => LogLevel::DEBUG, # The level for debug message
214+
'level' => LogLevel::DEBUG, # The level for debug message
214215
'metrics' => true, # If true, metrics will be sent trough the provided logger instance
215216
'events' => true, # If true, events will be sent trough the provided logger instance
216217
],

spec/Metric/MetricFactorySpec.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class MetricFactorySpec extends ObjectBehavior
99
{
1010
function let()
1111
{
12-
$this->beConstructedWith(array('default_tag' => 'default'));
12+
$this->beConstructedWith(array('default_tag' => 'default'), 'prefix.');
1313
}
1414

1515
function it_can_be_initialized()
@@ -19,8 +19,10 @@ function it_can_be_initialized()
1919

2020
function it_can_create_counters()
2121
{
22-
$this->counter('name', 10, array('tags'), .5)
23-
->shouldReturnAnInstanceOf('\Cmp\Monitoring\Metric\Type\Counter');
22+
$counter = $this->counter('name', 10, array('tags'), .5);
23+
$counter->shouldBeAnInstanceOf('\Cmp\Monitoring\Metric\Type\Counter');
24+
25+
$counter->getName()->shouldReturn('prefix.name');
2426
}
2527

2628
function it_can_create_gauges()

spec/MonitorFactorySpec.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function it_can_built_an_working_monitor(LoggerInterface $logger)
2626
$monitor = $this->create([
2727
'hostname' => 'fooserver',
2828
'default_tags' => ['foo' => 'bar'],
29+
'prefix' => 'my-app.',
2930
'logger' => [
3031
'instance' => $logger,
3132
'debug' => true,
@@ -49,6 +50,7 @@ function it_can_built_an_working_monitor_from_half_config(LoggerInterface $logge
4950
$monitor = $this->create([
5051
'hostname' => 'fooserver',
5152
'default_tags' => ['foo' => 'bar'],
53+
'prefix' => 'my-app.',
5254
'logger' => [
5355
'instance' => $logger,
5456
'debug' => true,

src/Metric/MetricFactory.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ class MetricFactory extends AbstractHasDefaultTags
2424
const TIMER = 'timer';
2525

2626
/**
27-
* @param array $defaultTags
27+
* @var string
2828
*/
29-
public function __construct(array $defaultTags = array())
29+
private $prefix;
30+
31+
/**
32+
* @param array $defaultTags
33+
* @param string $prefix
34+
*/
35+
public function __construct(array $defaultTags = array(), $prefix = "")
3036
{
3137
$this->defaultTags = $defaultTags;
38+
$this->prefix = $prefix;
3239
}
3340

3441
/**
@@ -43,7 +50,7 @@ public function __construct(array $defaultTags = array())
4350
*/
4451
public function counter($metric, $count = 1, array $tags = array(), $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
4552
{
46-
return new Counter($metric, $count, array_merge($this->defaultTags, $tags), $sampleRate);
53+
return new Counter($this->prefix($metric), $count, array_merge($this->defaultTags, $tags), $sampleRate);
4754
}
4855

4956
/**
@@ -58,7 +65,7 @@ public function counter($metric, $count = 1, array $tags = array(), $sampleRate
5865
*/
5966
public function gauge($metric, $level, array $tags = array(), $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
6067
{
61-
return new Gauge($metric, $level, array_merge($this->defaultTags, $tags), $sampleRate);
68+
return new Gauge($this->prefix($metric), $level, array_merge($this->defaultTags, $tags), $sampleRate);
6269
}
6370

6471
/**
@@ -73,7 +80,7 @@ public function gauge($metric, $level, array $tags = array(), $sampleRate = Abst
7380
*/
7481
public function histogram($metric, $duration = null, array $tags = array(), $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
7582
{
76-
return new Histogram($metric, $duration, array_merge($this->defaultTags, $tags), $sampleRate);
83+
return new Histogram($this->prefix($metric), $duration, array_merge($this->defaultTags, $tags), $sampleRate);
7784
}
7885

7986
/**
@@ -88,7 +95,7 @@ public function histogram($metric, $duration = null, array $tags = array(), $sam
8895
*/
8996
public function timer($metric, $duration = null, array $tags = array(), $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
9097
{
91-
return new Timer($metric, $duration, array_merge($this->defaultTags, $tags), $sampleRate);
98+
return new Timer($this->prefix($metric), $duration, array_merge($this->defaultTags, $tags), $sampleRate);
9299
}
93100

94101
/**
@@ -103,6 +110,16 @@ public function timer($metric, $duration = null, array $tags = array(), $sampleR
103110
*/
104111
public function set($metric, $uniqueValue, array $tags =array(), $sampleRate = AbstractMetric::DEFAULT_SAMPLE_RATE)
105112
{
106-
return new Set($metric, $uniqueValue, array_merge($this->defaultTags, $tags), $sampleRate);
113+
return new Set($this->prefix($metric), $uniqueValue, array_merge($this->defaultTags, $tags), $sampleRate);
114+
}
115+
116+
/**
117+
* @param string $metric
118+
*
119+
* @return string
120+
*/
121+
private function prefix($metric)
122+
{
123+
return $this->prefix ? $this->prefix.$metric : $metric;
107124
}
108125
}

src/MonitorFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MonitorFactory
2525
public static function create(array $config = [])
2626
{
2727
$config = self::mergeDefaults($config);
28-
$metricFactory = new MetricFactory($config['default_tags']);
28+
$metricFactory = new MetricFactory($config['default_tags'], $config['prefix']);
2929
$eventFactory = new EventFactory($config['hostname']);
3030
$logger = $config['logger']['instance'] instanceof LoggerInterface ? $config['logger']['instance'] : null;
3131
$debug = $config['logger']['debug'];
@@ -60,6 +60,7 @@ private static function mergeDefaults(array $config)
6060
return array_replace_recursive([
6161
'hostname' => gethostname(), # Hostname of the server
6262
'default_tags' => [], # A key-value array with default tags for metrics and events
63+
'prefix' => '', # A prefix for the metrics
6364
'logger' => [
6465
'instance' => null, # A Psr\LoggerInterface instance
6566
'debug' => false, # If true, it will log debug messages from the monitor

0 commit comments

Comments
 (0)