Skip to content

Commit

Permalink
Merge pull request #17 from johndodev/tags-influxDb-0.9
Browse files Browse the repository at this point in the history
POC : Add tags support for influxDb 0.9
  • Loading branch information
omansour committed Feb 3, 2016
2 parents 47effc6 + 9cf4e96 commit 2aafc1d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 17 deletions.
10 changes: 10 additions & 0 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,15 @@ $client->timing('another.graphite.node', (float) $timing);
$client->set('a.graphite.set', 12);
$client->gauge('a.gauge.node', 8);

// with tags for influxDb 0.9 : add the tags array at the end of each function
$tags = ['foo' => 'bar', 'site' => 'www', 'country' => 'fr'];
$sampleRate = 1;

$client->increment('a.influxDb.node', $sampleRate, $tags);
$client->count('a.influxDb.node', 5, $sampleRate, $tags);
$client->timing('another.influxDb.node', (float) $timing, $sampleRate, $tags);
$client->set('a.influxDb.set', 12, $sampleRate, $tags);
$client->gauge('a.influxDb.node', 8, $sampleRate, $tags);

$client->send(); // Send the metrics to the servers
```
35 changes: 21 additions & 14 deletions src/M6Web/Component/Statsd/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,15 @@ public function getServerKey($stats)
* @param string $value value
* @param float $sampleRate sampling rate
* @param string $unit unit
* @param array $tags Tags key => value for influxDb
*
* @return Client
*/
protected function addToSend($stats, $value, $sampleRate, $unit)
protected function addToSend($stats, $value, $sampleRate, $unit, $tags)
{

$message = new MessageEntity(
(string) $stats, (int) $value, (string) $unit, (float) $sampleRate
(string) $stats, (int) $value, (string) $unit, (float) $sampleRate, $tags
);

$queue = [
Expand Down Expand Up @@ -175,12 +176,13 @@ protected function buildSampledData()
* @param string $stats The metric to in log timing info for.
* @param int $time The ellapsed time (ms) to log
* @param float|int $sampleRate the rate (0-1) for sampling.
* @param array $tags Tags key => value for influxDb
*
* @return Client
*/
public function timing($stats, $time, $sampleRate = 1.0)
public function timing($stats, $time, $sampleRate = 1.0, $tags = [])
{
$this->addToSend($stats, $time, $sampleRate, 'ms');
$this->addToSend($stats, $time, $sampleRate, 'ms', $tags);

return $this;
}
Expand All @@ -190,14 +192,15 @@ public function timing($stats, $time, $sampleRate = 1.0)
*
* @param string $stats The metric(s) to increment.
* @param float $sampleRate SamplingRate
* @param array $tags Tags key => value for influxDb
*
* @internal param $ float|1 $sampleRate the rate (0-1) for sampling.
*
* @return Client
*/
public function increment($stats, $sampleRate = 1.0)
public function increment($stats, $sampleRate = 1.0, $tags = [])
{
$this->count($stats, '1', $sampleRate);
$this->count($stats, '1', $sampleRate, $tags);

return $this;
}
Expand All @@ -208,12 +211,13 @@ public function increment($stats, $sampleRate = 1.0)
*
* @param string $stats The metric(s) to decrement.
* @param float|int $sampleRate the rate (0-1) for sampling.
* @param array $tags Tags key => value for influxDb
*
* @return Client
*/
public function decrement($stats, $sampleRate = 1)
public function decrement($stats, $sampleRate = 1, $tags = [])
{
$this->count($stats, '-1', $sampleRate);
$this->count($stats, '-1', $sampleRate, $tags);

return $this;
}
Expand All @@ -224,14 +228,15 @@ public function decrement($stats, $sampleRate = 1)
* @param string $stats The metric(s) to count
* @param int $value The count value
* @param float|int $sampleRate the rate (0-1) for sampling.
* @param array $tags Tags key => value for influxDb
*
* @access public
*
* @return Client
*/
public function count($stats, $value, $sampleRate = 1)
public function count($stats, $value, $sampleRate = 1, $tags = [])
{
$this->addToSend($stats, $value, $sampleRate, 'c');
$this->addToSend($stats, $value, $sampleRate, 'c', $tags);

return $this;
}
Expand All @@ -242,13 +247,14 @@ public function count($stats, $value, $sampleRate = 1)
* @param string $stats The metric(s) to count
* @param int $value The value
* @param float|int $sampleRate the rate (0-1) for sampling.
* @param array $tags Tags key => value for influxDb
*
* @access public
* @return Client
*/
public function gauge($stats, $value, $sampleRate = 1)
public function gauge($stats, $value, $sampleRate = 1, $tags = [])
{
$this->addToSend($stats, $value, $sampleRate, 'g');
$this->addToSend($stats, $value, $sampleRate, 'g', $tags);

return $this;
}
Expand All @@ -259,13 +265,14 @@ public function gauge($stats, $value, $sampleRate = 1)
* @param string $stats The metric(s) to count
* @param int $value The value
* @param float|int $sampleRate the rate (0-1) for sampling.
* @param array $tags Tags key => value for influxDb
*
* @access public
* @return Client
*/
public function set($stats, $value, $sampleRate = 1)
public function set($stats, $value, $sampleRate = 1, $tags = [])
{
$this->addToSend($stats, $value, $sampleRate, 's');
$this->addToSend($stats, $value, $sampleRate, 's', $tags);

return $this;
}
Expand Down
42 changes: 40 additions & 2 deletions src/M6Web/Component/Statsd/MessageEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,21 @@ class MessageEntity
*/
protected $unit;

/**
* @var array
*/
protected $tags = [];

/**
* @param string $node node
* @param int $value value of the node
* @param string $unit units (ms for timer, c for counting ...)
* @param float $sampleRate sampling rate
* @param array $tags Tags key => value for influxDb
*
* @return MessageEntity
*/
public function __construct($node, $value, $unit = '', $sampleRate = 1.0)
public function __construct($node, $value, $unit = '', $sampleRate = 1.0, $tags = [])
{
$this->node = $node;
$this->value = $value;
Expand All @@ -48,6 +54,8 @@ public function __construct($node, $value, $unit = '', $sampleRate = 1.0)
$this->unit = $unit;
}

$this->tags = $tags ?: [];

$this->checkConstructor();
}

Expand All @@ -69,6 +77,10 @@ protected function checkConstructor()
if (!is_float($this->sampleRate) or ($this->sampleRate <= 0)) {
throw new Exception('sampleRate has to be a non-zero posivite float');
}

if (!is_array($this->tags)) {
throw new Exception('Tags has to be an array');
}
}

/**
Expand Down Expand Up @@ -117,14 +129,40 @@ public function getUnit()
return $this->unit;
}

/**
* @return string Tags formatted for sending
* ex: "server=5,country=fr"
*/
private function getTagsAsString()
{
$tags = array_map(function($k, $v) {
return $k.'='.$v;
}, array_keys($this->tags), $this->tags);

return implode(',', $tags);
}

/**
* @return string the node with tags as string
* ex : node "foo.bar" and tag ["country" => "fr" ] Into "foo.bar,country=fr"
*/
private function getFullNode()
{
if ($this->tags) {
return $this->getNode().','.$this->getTagsAsString();
}

return $this->getNode();
}

/**
* format a statsd message
*
* @return string
*/
public function getStatsdMessage()
{
$message = sprintf('%s:%s|%s', $this->getNode(), $this->getValue(), $this->getUnit());
$message = sprintf('%s:%s|%s', $this->getFullNode(), $this->getValue(), $this->getUnit());
if ($this->useSampleRate()) {
$message .= sprintf('|@%s', $this->getSampleRate());
}
Expand Down
15 changes: 14 additions & 1 deletion src/M6Web/Component/Tests/Units/MessageEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public function testgetStatsdMessage()
->string($messageEntity->getStatsdMessage())
->isEqualTo('raoul.node:1|c|@0.2')
;

// with tags
$this->if($messageEntity = new Statsd\MessageEntity(
'raoul.node', 1, 'c', 0.2, ['foo' => 'bar']))
->then()
->string($messageEntity->getStatsdMessage())
->isEqualTo('raoul.node,foo=bar:1|c|@0.2')
;
}

public function testErrorConstructorStatsdMessage()
Expand Down Expand Up @@ -87,5 +95,10 @@ function () {
})
->isInstanceOf('\M6Web\Component\Statsd\Exception');

$this->exception(
function() {
new Statsd\MessageEntity('raoul.node', 1, 'c', 1, 'stringTag');
}
)->isInstanceOf('\M6Web\Component\Statsd\Exception');
}
}
}

0 comments on commit 2aafc1d

Please sign in to comment.