diff --git a/doc/usage.md b/doc/usage.md index 359ccb0..029f6c2 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -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 ``` diff --git a/src/M6Web/Component/Statsd/Client.php b/src/M6Web/Component/Statsd/Client.php index 573e375..192342f 100644 --- a/src/M6Web/Component/Statsd/Client.php +++ b/src/M6Web/Component/Statsd/Client.php @@ -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 = [ @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/src/M6Web/Component/Statsd/MessageEntity.php b/src/M6Web/Component/Statsd/MessageEntity.php index 88ea728..bfa8f9b 100644 --- a/src/M6Web/Component/Statsd/MessageEntity.php +++ b/src/M6Web/Component/Statsd/MessageEntity.php @@ -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; @@ -48,6 +54,8 @@ public function __construct($node, $value, $unit = '', $sampleRate = 1.0) $this->unit = $unit; } + $this->tags = $tags ?: []; + $this->checkConstructor(); } @@ -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'); + } } /** @@ -117,6 +129,32 @@ 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 * @@ -124,7 +162,7 @@ public function getUnit() */ 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()); } diff --git a/src/M6Web/Component/Tests/Units/MessageEntity.php b/src/M6Web/Component/Tests/Units/MessageEntity.php index c719a20..d1320f5 100644 --- a/src/M6Web/Component/Tests/Units/MessageEntity.php +++ b/src/M6Web/Component/Tests/Units/MessageEntity.php @@ -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() @@ -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'); } -} \ No newline at end of file +}