-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added DogStatsD support. Co-authored-by: Oliver THEBAULT <[email protected]>
- Loading branch information
Showing
10 changed files
with
269 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/M6Web/Component/Statsd/MessageFormatter/DogStatsDMessageFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace M6Web\Component\Statsd\MessageFormatter; | ||
|
||
use M6Web\Component\Statsd\MessageEntity; | ||
|
||
/** | ||
* Formats a StatsD message using the DogStatsD style: | ||
* https://docs.datadoghq.com/developers/dogstatsd/datagram_shell/?tab=metrics#tagging | ||
*/ | ||
class DogStatsDMessageFormatter implements MessageFormatterInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format(MessageEntity $message) | ||
{ | ||
$formatted = sprintf('%s:%s|%s', $message->getNode(), $message->getValue(), $message->getUnit()); | ||
|
||
if ($message->useSampleRate()) { | ||
$formatted .= sprintf('|@%s', $message->getSampleRate()); | ||
} | ||
|
||
if ($message->getTags()) { | ||
$formatted .= '|#'.$this->getTagsAsString($message); | ||
} | ||
|
||
return $formatted."\n"; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private function getTagsAsString(MessageEntity $message) | ||
{ | ||
$tags = array_map(static function ($k, $v) { | ||
return $k.':'.$v; | ||
}, array_keys($message->getTags()), $message->getTags()); | ||
|
||
return implode(',', $tags); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/M6Web/Component/Statsd/MessageFormatter/InfluxDBStatsDMessageFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace M6Web\Component\Statsd\MessageFormatter; | ||
|
||
use M6Web\Component\Statsd\MessageEntity; | ||
|
||
/** | ||
* Formats a StatsD message using the InfluxDB StatsD style: | ||
* https://www.influxdata.com/blog/getting-started-with-sending-statsd-metrics-to-telegraf-influxdb/#introducing-influx-statsd | ||
*/ | ||
class InfluxDBStatsDMessageFormatter implements MessageFormatterInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format(MessageEntity $message) | ||
{ | ||
$node = $message->getNode(); | ||
|
||
if ($message->getTags()) { | ||
$node .= ','.$this->getTagsAsString($message); | ||
} | ||
|
||
$formatted = sprintf('%s:%s|%s', $node, $message->getValue(), $message->getUnit()); | ||
|
||
if ($message->useSampleRate()) { | ||
$formatted .= sprintf('|@%s', $message->getSampleRate()); | ||
} | ||
|
||
return $formatted; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private function getTagsAsString(MessageEntity $message) | ||
{ | ||
$tags = array_map(static function ($k, $v) { | ||
return $k.'='.$v; | ||
}, array_keys($message->getTags()), $message->getTags()); | ||
|
||
return implode(',', $tags); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/M6Web/Component/Statsd/MessageFormatter/MessageFormatterInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace M6Web\Component\Statsd\MessageFormatter; | ||
|
||
use M6Web\Component\Statsd\MessageEntity; | ||
|
||
/** | ||
* Interface for formatting StatsD messages for different StatsD server implementations. | ||
*/ | ||
interface MessageFormatterInterface | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public function format(MessageEntity $message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/M6Web/Component/Tests/Units/MessageFormatter/DogStatsDMessageFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace M6Web\Component\Statsd\Tests\Units\MessageFormatter; | ||
|
||
use M6Web\Component\Statsd; | ||
use mageekguy\atoum; | ||
|
||
class DogStatsDMessageFormatter extends atoum\test | ||
{ | ||
public function testFormat() | ||
{ | ||
// not sampled message | ||
$message = new \mock\M6Web\Component\Statsd\MessageEntity('raoul.node', 1, 'c'); | ||
$this | ||
->if($formatter = new Statsd\MessageFormatter\DogStatsDMessageFormatter()) | ||
->then() | ||
->string($formatter->format($message)) | ||
->isEqualTo("raoul.node:1|c\n"); | ||
|
||
// sampled message | ||
$message = new \mock\M6Web\Component\Statsd\MessageEntity('raoul.node', 1, 'c', 0.2); | ||
$this->calling($message)->useSampleRate = true; | ||
$this | ||
->given($formatter = new Statsd\MessageFormatter\DogStatsDMessageFormatter()) | ||
->then() | ||
->string($formatter->format($message)) | ||
->isEqualTo("raoul.node:1|c|@0.2\n"); | ||
|
||
// with tags | ||
$message = new \mock\M6Web\Component\Statsd\MessageEntity('raoul.node', 1, 'c', 0.2, ['foo' => 'bar']); | ||
$this->calling($message)->useSampleRate = true; | ||
$this | ||
->if($formatter = new Statsd\MessageFormatter\DogStatsDMessageFormatter()) | ||
->then() | ||
->string($formatter->format($message)) | ||
->isEqualTo("raoul.node:1|c|@0.2|#foo:bar\n"); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/M6Web/Component/Tests/Units/MessageFormatter/InfluxDBStatsDMessageFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace M6Web\Component\Statsd\Tests\Units\MessageFormatter; | ||
|
||
use M6Web\Component\Statsd; | ||
use mageekguy\atoum; | ||
|
||
class InfluxDBStatsDMessageFormatter extends atoum\test | ||
{ | ||
public function testFormat() | ||
{ | ||
// not sampled message | ||
$message = new \mock\M6Web\Component\Statsd\MessageEntity('raoul.node', 1, 'c'); | ||
$this | ||
->if($formatter = new Statsd\MessageFormatter\InfluxDBStatsDMessageFormatter()) | ||
->then() | ||
->string($formatter->format($message)) | ||
->isEqualTo('raoul.node:1|c'); | ||
|
||
// sampled message | ||
$message = new \mock\M6Web\Component\Statsd\MessageEntity('raoul.node', 1, 'c', 0.2); | ||
$this->calling($message)->useSampleRate = true; | ||
$this | ||
->given($formatter = new Statsd\MessageFormatter\InfluxDBStatsDMessageFormatter()) | ||
->then() | ||
->string($formatter->format($message)) | ||
->isEqualTo('raoul.node:1|c|@0.2'); | ||
|
||
// with tags | ||
$message = new \mock\M6Web\Component\Statsd\MessageEntity('raoul.node', 1, 'c', 0.2, ['foo' => 'bar']); | ||
$this->calling($message)->useSampleRate = true; | ||
$this | ||
->if($formatter = new Statsd\MessageFormatter\InfluxDBStatsDMessageFormatter()) | ||
->then() | ||
->string($formatter->format($message)) | ||
->isEqualTo('raoul.node,foo=bar:1|c|@0.2'); | ||
} | ||
} |