Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/inline tags for all #55

Open
wants to merge 23 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Beberlei/Metrics/Collector/DogStatsD.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
namespace Beberlei\Metrics\Collector;

class DogStatsD implements Collector, InlineTaggableGaugeableCollector
class DogStatsD implements Collector, TaggableCollector, TaggableGaugeableCollector
{
/** @var string */
private $host;
Expand All @@ -26,6 +26,9 @@ class DogStatsD implements Collector, InlineTaggableGaugeableCollector
/** @var array */
private $data;

/** @var array */
private $tags = array();

/**
* @param string $host
* @param string $port
Expand Down Expand Up @@ -105,6 +108,14 @@ public function flush()
$this->data = array();
}

/**
* {@inheritdoc}
*/
public function setTags($tags)
{
$this->tags = $tags;
}

/**
* Given a key/value map of metric tags, builds them into a
* DogStatsD tag string and returns the string.
Expand All @@ -117,6 +128,8 @@ private function buildTagString($tags)
{
$results = array();

$tags = array_merge($this->tags, $tags);

foreach ($tags as $key => $value) {
$results[] = sprintf('%s:%s', $key, $value);
}
Expand Down
18 changes: 9 additions & 9 deletions src/Beberlei/Metrics/Collector/InfluxDB.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,33 @@ public function __construct(Client $client)
/**
* {@inheritdoc}
*/
public function increment($variable)
public function increment($variable, $tags = array())
{
$this->data[] = array($variable, 1);
$this->data[] = array($variable, 1, $tags);
}

/**
* {@inheritdoc}
*/
public function decrement($variable)
public function decrement($variable, $tags = array())
{
$this->data[] = array($variable, -1);
$this->data[] = array($variable, -1, $tags);
}

/**
* {@inheritdoc}
*/
public function timing($variable, $time)
public function timing($variable, $time, $tags = array())
{
$this->data[] = array($variable, $time);
$this->data[] = array($variable, $time, $tags);
}

/**
* {@inheritdoc}
*/
public function measure($variable, $value)
public function measure($variable, $value, $tags = array())
{
$this->data[] = array($variable, $value);
$this->data[] = array($variable, $value, $tags);
}

/**
Expand All @@ -78,7 +78,7 @@ public function flush()
'fields' => array('value' => $data[1]),
),
),
'tags' => $this->tags,
'tags' => array_merge($this->tags, $data[2]),
));
}

Expand Down

This file was deleted.

30 changes: 16 additions & 14 deletions src/Beberlei/Metrics/Collector/Prometheus.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,45 @@ public function __construct(CollectorRegistry $collectorRegistry, $namespace = '
/**
* {@inheritdoc}
*/
public function measure($variable, $value)
public function measure($variable, $value, $tags = array())
{
$this->data['gauges'][] = array(
'name' => $variable,
'value' => $value,
'tags' => $tags,
);
}

/**
* {@inheritdoc}
*/
public function increment($variable)
public function increment($variable, $tags = array())
{
$this->data['counters'][] = array(
'name' => $variable,
'value' => 1,
'tags' => $tags,
);
}

/**
* {@inheritdoc}
*/
public function decrement($variable)
public function decrement($variable, $tags = array())
{
$this->data['counters'][] = array(
'name' => $variable,
'value' => -1,
'tags' => $tags,
);
}

/**
* {@inheritdoc}
*/
public function timing($variable, $time)
public function timing($variable, $time, $tags = array())
{
$this->measure($variable, $time);
$this->measure($variable, $time, $tags);
}

/**
Expand All @@ -98,22 +101,20 @@ public function flush()
return;
}

$tagsValues = array_values($this->tags);

foreach ($this->data['counters'] as $counterData) {
$gauge = $this->getOrRegisterGaugeForVariable($counterData['name']);
$gauge = $this->getOrRegisterGaugeForVariable($counterData['name'], $counterData['tags']);

if ($counterData['value'] > 0) {
$gauge->inc($tagsValues);
$gauge->inc(array_values(array_merge($this->tags, $counterData['tags'])));
} elseif ($counterData['value'] < 0) {
$gauge->dec($tagsValues);
$gauge->dec(array_values(array_merge($this->tags, $counterData['tags'])));
}
}

foreach ($this->data['gauges'] as $gaugeData) {
$gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name']);
$gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name'], $gaugeData['tags']);

$gauge->set($gaugeData['value'], $tagsValues);
$gauge->set($gaugeData['value'], array_values(array_merge($this->tags, $gaugeData['tags'])));
}

$this->data = array('counters' => array(), 'gauges' => array());
Expand All @@ -130,9 +131,10 @@ public function setTags($tags)
/**
* @param string $variable
*
* @param array $tags
* @return \Prometheus\Gauge
*/
private function getOrRegisterGaugeForVariable($variable)
private function getOrRegisterGaugeForVariable($variable, $tags = array())
{
try {
$gauge = $this->collectorRegistry->getGauge($this->namespace, $variable);
Expand All @@ -141,7 +143,7 @@ private function getOrRegisterGaugeForVariable($variable)
$this->namespace,
$variable,
'',
array_keys($this->tags)
array_keys(array_merge($this->tags, $tags))
);
}

Expand Down
36 changes: 36 additions & 0 deletions src/Beberlei/Metrics/Collector/TaggableCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,40 @@ interface TaggableCollector
* @param array $tags
*/
public function setTags($tags);

/**
* Updates a counter by some arbitrary amount.
*
* @param string $variable
* @param int $value The amount to increment the counter by
* @param array $tags Tags to be attached to the metric
*
* @return
*/
public function measure($variable, $value, $tags = array());

/**
* Increments a counter.
*
* @param string $variable
* @param array $tags Tags to be attached to the metric
*/
public function increment($variable, $tags = array());

/**
* Decrements a counter.
*
* @param string $variable
* @param array $tags Tags to be attached to the metric
*/
public function decrement($variable, $tags = array());

/**
* Records a timing.
*
* @param string $variable
* @param int $time The duration of the timing in milliseconds
* @param array $tags Tags to be attached to the metric
*/
public function timing($variable, $time, $tags = array());
}
28 changes: 28 additions & 0 deletions src/Beberlei/Metrics/Collector/TaggableGaugeableCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Beberlei Metrics.
*
* LICENSE
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/
namespace Beberlei\Metrics\Collector;

/**
* TaggableGaugeableCollector interface.
*/
interface TaggableGaugeableCollector
{
/**
* Updates a gauge by an arbitrary amount.
*
* @param string $variable
* @param int $value
* @param array $tags Tags to be attached to the metric
*/
public function gauge($variable, $value, $tags = array());
}
Loading