From 43078b03a477257dd03233c3166e44c4df6961bf Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 17:23:02 +0100 Subject: [PATCH 01/20] Made all taggable collectors inline-taggable --- src/Beberlei/Metrics/Collector/DogStatsD.php | 2 +- src/Beberlei/Metrics/Collector/InfluxDB.php | 18 +- .../InlineTaggableGaugeableCollector.php | 64 ------ src/Beberlei/Metrics/Collector/Prometheus.php | 31 +-- .../Metrics/Collector/TaggableCollector.php | 36 +++ .../Collector/TaggableGaugeableCollector.php | 28 +++ src/Beberlei/Metrics/Collector/Telegraf.php | 48 ++-- .../Metrics/Tests/Collector/InfluxDBTest.php | 50 +++++ .../Tests/Collector/PrometheusTest.php | 208 ++++++++++++++++++ 9 files changed, 381 insertions(+), 104 deletions(-) mode change 100644 => 100755 src/Beberlei/Metrics/Collector/InfluxDB.php delete mode 100644 src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php create mode 100755 src/Beberlei/Metrics/Collector/TaggableGaugeableCollector.php mode change 100644 => 100755 src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php diff --git a/src/Beberlei/Metrics/Collector/DogStatsD.php b/src/Beberlei/Metrics/Collector/DogStatsD.php index 1207239..791b888 100644 --- a/src/Beberlei/Metrics/Collector/DogStatsD.php +++ b/src/Beberlei/Metrics/Collector/DogStatsD.php @@ -12,7 +12,7 @@ */ namespace Beberlei\Metrics\Collector; -class DogStatsD implements Collector, InlineTaggableGaugeableCollector +class DogStatsD implements Collector, TaggableCollector, TaggableGaugeableCollector { /** @var string */ private $host; diff --git a/src/Beberlei/Metrics/Collector/InfluxDB.php b/src/Beberlei/Metrics/Collector/InfluxDB.php old mode 100644 new mode 100755 index 67768cd..a5eafc1 --- a/src/Beberlei/Metrics/Collector/InfluxDB.php +++ b/src/Beberlei/Metrics/Collector/InfluxDB.php @@ -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); } /** @@ -78,7 +78,7 @@ public function flush() 'fields' => array('value' => $data[1]), ), ), - 'tags' => $this->tags, + 'tags' => array_merge($this->tags, $data[2]), )); } diff --git a/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php b/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php deleted file mode 100644 index 3873cec..0000000 --- a/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php +++ /dev/null @@ -1,64 +0,0 @@ -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); } /** @@ -98,22 +101,24 @@ public function flush() return; } - $tagsValues = array_values($this->tags); + $globalTagsValues = array_values($this->tags); foreach ($this->data['counters'] as $counterData) { - $gauge = $this->getOrRegisterGaugeForVariable($counterData['name']); + $gauge = $this->getOrRegisterGaugeForVariable($counterData['name'], $counterData['tags']); + $tagsValues = array_values($counterData['tags']); if ($counterData['value'] > 0) { - $gauge->inc($tagsValues); + $gauge->inc(array_merge($globalTagsValues, $tagsValues)); } elseif ($counterData['value'] < 0) { - $gauge->dec($tagsValues); + $gauge->dec(array_merge($globalTagsValues, $tagsValues)); } } foreach ($this->data['gauges'] as $gaugeData) { - $gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name']); + $gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name'], $counterData['tags']); - $gauge->set($gaugeData['value'], $tagsValues); + $tagsValues = array_values($counterData['tags']); + $gauge->set($gaugeData['value'], array_merge($globalTagsValues, $tagsValues)); } $this->data = array('counters' => array(), 'gauges' => array()); @@ -132,7 +137,7 @@ public function setTags($tags) * * @return \Prometheus\Gauge */ - private function getOrRegisterGaugeForVariable($variable) + private function getOrRegisterGaugeForVariable($variable, $tags = array()) { try { $gauge = $this->collectorRegistry->getGauge($this->namespace, $variable); @@ -141,7 +146,7 @@ private function getOrRegisterGaugeForVariable($variable) $this->namespace, $variable, '', - array_keys($this->tags) + array_keys(array_merge($this->tags, $tags)) ); } diff --git a/src/Beberlei/Metrics/Collector/TaggableCollector.php b/src/Beberlei/Metrics/Collector/TaggableCollector.php index 6ff2057..909b6f7 100644 --- a/src/Beberlei/Metrics/Collector/TaggableCollector.php +++ b/src/Beberlei/Metrics/Collector/TaggableCollector.php @@ -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()); } diff --git a/src/Beberlei/Metrics/Collector/TaggableGaugeableCollector.php b/src/Beberlei/Metrics/Collector/TaggableGaugeableCollector.php new file mode 100755 index 0000000..189f1a6 --- /dev/null +++ b/src/Beberlei/Metrics/Collector/TaggableGaugeableCollector.php @@ -0,0 +1,28 @@ +tags = http_build_query($tags, '', ','); - $this->tags = (strlen($this->tags) > 0 ? ','.$this->tags : $this->tags); - } + $this->tags = $tags; + } /** * {@inheritdoc} */ - public function timing($variable, $time) + public function timing($variable, $time, $tags = array()) { - $this->data[] = sprintf('%s%s:%s|ms', $variable, $this->tags, $time); + $this->data[] = sprintf('%s%s:%s|ms', $variable, $this->buildTagString(array_merge($this->tags, $tags)), $time); } /** * {@inheritdoc} */ - public function increment($variable) + public function increment($variable, $tags = array()) { - $this->data[] = $variable.$this->tags.':1|c'; + $this->data[] = $variable.$this->buildTagString(array_merge($this->tags, $tags)).':1|c'; } /** * {@inheritdoc} */ - public function decrement($variable) + public function decrement($variable, $tags = array()) { - $this->data[] = $variable.$this->tags.':-1|c'; + $this->data[] = $variable.$this->buildTagString(array_merge($this->tags, $tags)).':-1|c'; } /** * {@inheritdoc} */ - public function measure($variable, $value) + public function measure($variable, $value, $tags = array()) { - $this->data[] = sprintf('%s%s:%s|c', $variable, $this->tags, $value); + $this->data[] = sprintf('%s%s:%s|c', $variable, $this->buildTagString(array_merge($this->tags, $tags)), $value); } /** * {@inheritdoc} */ - public function gauge($variable, $value) + public function gauge($variable, $value, $tags = array()) { - $this->data[] = sprintf('%s%s:%s|g', $variable, $this->tags, $value); + $this->data[] = sprintf('%s%s:%s|g', $variable, $this->buildTagString(array_merge($this->tags, $tags)), $value); } /** @@ -102,7 +101,7 @@ public function gauge($variable, $value) */ public function set($variable, $value) { - $this->data[] = sprintf('%s%s:%s|s', $variable, $this->tags, $value); + $this->data[] = sprintf('%s%s:%s|s', $variable, $this->buildTagString(array_merge($this->tags, $tags)), $value); } /** @@ -130,4 +129,19 @@ public function flush() $this->data = array(); } + + /** + * Given a key/value map of metric tags, builds them into a + * telegraf statsd tag string and returns the string. + * + * @param $tags array + * + * @return string + */ + private function buildTagString($tags) + { + $tagString = http_build_query($tags, '', ','); + $tagString = (strlen($this->tags) > 0 ? ','.$this->tags : $this->tags); + return $tagString; + } } diff --git a/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php b/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php old mode 100644 new mode 100755 index 70ec9d8..0acd16d --- a/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php +++ b/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php @@ -140,4 +140,54 @@ public function testCollectMeasureWithTags() $this->collector->measure('series-name', 47.11); $this->collector->flush(); } + + public function testCollectMeasureWithInlineTags() + { + $globalTags = array( + 'dc' => 'west', + 'node' => 'nemesis101', + ); + + $inlineTags = array( + 'resource' => 'stuff' + ); + + $expectedTags = array_merge($globalTags, $inlineTags); + + $expectedArgs = array( + 'points' => array( + array( + 'measurement' => 'series-name', + 'fields' => array('value' => 47.11), + ), + ), + 'tags' => $expectedTags, + ); + + $expectedArgs2 = array( + 'points' => array( + array( + 'measurement' => 'series-name-2', + 'fields' => array('value' => 53.22), + ), + ), + 'tags' => $globalTags, + ); + + $this->client->expects($this->exactly(2)) + ->method('mark'); + + $this->client->expects($this->at(0)) + ->method('mark') + ->with($expectedArgs); + + $this->client->expects($this->at(1)) + ->method('mark') + ->with($expectedArgs2); + + $this->collector->setTags($globalTags); + $this->collector->measure('series-name', 47.11, $inlineTags); + $this->collector->measure('series-name-2', 53.22); + $this->collector->flush(); + } } diff --git a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php index 7d2aae4..846303e 100644 --- a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php +++ b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php @@ -96,6 +96,59 @@ public function testMeasureWithTags() $this->collector->flush(); } + public function testMeasureWithInlineTags() + { + $expectedVariableValue = 123; + $expectedVariableValue2 = 456; + $globalTags = array( + 'tag1' => 'value1', + 'tag2' => 'value2', + ); + $inlineTags = array( + 'tag3' => 'value3', + ); + + $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock2 = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock + ->expects($this->once()) + ->method('set') + ->with($expectedVariableValue, array_values(array_merge($globalTags, $inlineTags))) + ; + $gaugeMock + ->expects($this->once()) + ->method('set') + ->with($expectedVariableValue2, array_values($globalTags)) + ; + + $this->collectorRegistryMock + ->expects($this->exactly(2)); + $this->collectorRegistryMock + ->expects($this->at(0)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME) + ->willReturn($gaugeMock) + ; + $this->collectorRegistryMock + ->expects($this->at(1)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME.'_2') + ->willReturn($gaugeMock2) + ; + + $this->collector->setTags($globalTags); + + $this->collector->measure(self::TEST_VARIABLE_NAME, $expectedVariableValue, $inlineTags); + $this->collector->measure(self::TEST_VARIABLE_NAME.'_2', $expectedVariableValue2); + $this->collector->flush(); + } + public function testIncrement() { $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') @@ -149,6 +202,57 @@ public function testIncrementWithTags() $this->collector->flush(); } + public function testIncrementWithInlineTags() + { + $globalTags = array( + 'tag1' => 'value1', + 'tag2' => 'value2', + ); + $inlineTags = array( + 'tag3' => 'value3', + ); + + $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock2 = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock + ->expects($this->once()) + ->method('inc') + ->with(array_values(array_merge($globalTags, $inlineTags))) + ; + $gaugeMock2 + ->expects($this->once()) + ->method('inc') + ->with(array_values($globalTags)) + ; + + $this->collectorRegistryMock + ->expects($this->exactly(2)); + $this->collectorRegistryMock + ->expects($this->at(0)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME) + ->willReturn($gaugeMock) + ; + $this->collectorRegistryMock + ->expects($this->at(1)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME.'_2') + ->willReturn($gaugeMock2) + ; + + $this->collector->setTags($globalTags); + + $this->collector->increment(self::TEST_VARIABLE_NAME, $inlineTags); + $this->collector->increment(self::TEST_VARIABLE_NAME.'_2'); + $this->collector->flush(); + } + public function testDecrement() { $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') @@ -202,6 +306,57 @@ public function testDecrementWithTags() $this->collector->flush(); } + public function testDecrementWithInlineTags() + { + $globalTags = array( + 'tag1' => 'value1', + 'tag2' => 'value2', + ); + $inlineTags = array( + 'tag3' => 'value3', + ); + + $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock2 = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock + ->expects($this->once()) + ->method('dec') + ->with(array_merge($globalTags, $inlineTags)) + ; + $gaugeMock2 + ->expects($this->once()) + ->method('dec') + ->with(array_values($globalTags)) + ; + + $this->collectorRegistryMock + ->expects($this->exactly(2)); + $this->collectorRegistryMock + ->expects($this->at(0)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME) + ->willReturn($gaugeMock) + ; + $this->collectorRegistryMock + ->expects($this->at(1)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME.'_2') + ->willReturn($gaugeMock2) + ; + + $this->collector->setTags($globalTags); + + $this->collector->decrement(self::TEST_VARIABLE_NAME, $inlineTags); + $this->collector->decrement(self::TEST_VARIABLE_NAME.'_2'); + $this->collector->flush(); + } + public function testTiming() { $expectedVariableValue = 123; @@ -258,6 +413,59 @@ public function testTimingWithTags() $this->collector->flush(); } + public function testTimingWithInlineTags() + { + $expectedVariableValue = 123; + $expectedVariableValue2 = 456; + $globalTags = array( + 'tag1' => 'value1', + 'tag2' => 'value2', + ); + $inlineTags = array( + 'tag3' => 'value3', + ); + + $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock2 = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock + ->expects($this->once()) + ->method('set') + ->with($expectedVariableValue, array_values(array_merge($globalTags, $inlineTags))) + ; + $gaugeMock2 + ->expects($this->once()) + ->method('set') + ->with($expectedVariableValue, array_values($globalTags)) + ; + + $this->collectorRegistryMock + ->expects($this->exactly(2)); + $this->collectorRegistryMock + ->expects($this->at(0)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME) + ->willReturn($gaugeMock) + ; + $this->collectorRegistryMock + ->expects($this->at(0)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME.'_2') + ->willReturn($gaugeMock2) + ; + + $this->collector->setTags($globalTags); + + $this->collector->timing(self::TEST_VARIABLE_NAME, $expectedVariableValue, $inlineTags); + $this->collector->timing(self::TEST_VARIABLE_NAME.'_2', $expectedVariableValue); + $this->collector->flush(); + } + public function testMeasureWhenSetNewVariableWithTags() { $expectedVariableValue = 123; From bf3f66617f95b177bfa007609975196aa6f9df77 Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 17:26:16 +0100 Subject: [PATCH 02/20] Corrected interface name --- src/Beberlei/Metrics/Collector/Telegraf.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 src/Beberlei/Metrics/Collector/Telegraf.php diff --git a/src/Beberlei/Metrics/Collector/Telegraf.php b/src/Beberlei/Metrics/Collector/Telegraf.php old mode 100644 new mode 100755 index 93b3276..6d658fc --- a/src/Beberlei/Metrics/Collector/Telegraf.php +++ b/src/Beberlei/Metrics/Collector/Telegraf.php @@ -17,7 +17,7 @@ * ad hoc implementation for the StatsD - Telegraf integration, * support tagging. */ -class Telegraf implements Collector, InlineTaggableGaugeableCollector +class Telegraf implements Collector, TaggableGaugeableCollector { /** @var string */ private $host; From 61eee698936235cdc3671acb9c728f5e8fb2ee43 Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 17:26:36 +0100 Subject: [PATCH 03/20] Added global tags support --- src/Beberlei/Metrics/Collector/DogStatsD.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) mode change 100644 => 100755 src/Beberlei/Metrics/Collector/DogStatsD.php diff --git a/src/Beberlei/Metrics/Collector/DogStatsD.php b/src/Beberlei/Metrics/Collector/DogStatsD.php old mode 100644 new mode 100755 index 791b888..3e628eb --- a/src/Beberlei/Metrics/Collector/DogStatsD.php +++ b/src/Beberlei/Metrics/Collector/DogStatsD.php @@ -26,6 +26,9 @@ class DogStatsD implements Collector, TaggableCollector, TaggableGaugeableCollec /** @var array */ private $data; + /** @var array */ + private $tags = array(); + /** * @param string $host * @param string $port @@ -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. @@ -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); } From 5741bcef4c1bfda8d50856a9e2fd8c889547916a Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 17:46:28 +0100 Subject: [PATCH 04/20] Corrected phpunit method matchers --- src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) mode change 100644 => 100755 src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php diff --git a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php old mode 100644 new mode 100755 index 846303e..c16cfd2 --- a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php +++ b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php @@ -232,7 +232,8 @@ public function testIncrementWithInlineTags() ; $this->collectorRegistryMock - ->expects($this->exactly(2)); + ->expects($this->exactly(2)) + ->method('getGauge'); $this->collectorRegistryMock ->expects($this->at(0)) ->method('getGauge') @@ -336,7 +337,8 @@ public function testDecrementWithInlineTags() ; $this->collectorRegistryMock - ->expects($this->exactly(2)); + ->expects($this->exactly(2)) + ->method('getGauge'); $this->collectorRegistryMock ->expects($this->at(0)) ->method('getGauge') @@ -445,7 +447,8 @@ public function testTimingWithInlineTags() ; $this->collectorRegistryMock - ->expects($this->exactly(2)); + ->expects($this->exactly(2)) + ->method('getGauge'); $this->collectorRegistryMock ->expects($this->at(0)) ->method('getGauge') From c06766a765c3a1df686ebbd4144f3666d1756844 Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 17:56:54 +0100 Subject: [PATCH 05/20] Corrected my stupid logic --- src/Beberlei/Metrics/Collector/Prometheus.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) mode change 100644 => 100755 src/Beberlei/Metrics/Collector/Prometheus.php diff --git a/src/Beberlei/Metrics/Collector/Prometheus.php b/src/Beberlei/Metrics/Collector/Prometheus.php old mode 100644 new mode 100755 index 3aec30b..e53a681 --- a/src/Beberlei/Metrics/Collector/Prometheus.php +++ b/src/Beberlei/Metrics/Collector/Prometheus.php @@ -101,24 +101,20 @@ public function flush() return; } - $globalTagsValues = array_values($this->tags); - foreach ($this->data['counters'] as $counterData) { $gauge = $this->getOrRegisterGaugeForVariable($counterData['name'], $counterData['tags']); - $tagsValues = array_values($counterData['tags']); if ($counterData['value'] > 0) { - $gauge->inc(array_merge($globalTagsValues, $tagsValues)); + $gauge->inc(array_values(array_merge($this->tags, $counterData['tags']))); } elseif ($counterData['value'] < 0) { - $gauge->dec(array_merge($globalTagsValues, $tagsValues)); + $gauge->dec(array_values(array_merge($this->tags, $counterData['tags']))); } } foreach ($this->data['gauges'] as $gaugeData) { - $gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name'], $counterData['tags']); + $gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name'], $gaugeData['tags']); - $tagsValues = array_values($counterData['tags']); - $gauge->set($gaugeData['value'], array_merge($globalTagsValues, $tagsValues)); + $gauge->set($gaugeData['value'], array_values(array_merge($this->tags, $gaugeData['tags']))); } $this->data = array('counters' => array(), 'gauges' => array()); @@ -135,6 +131,7 @@ public function setTags($tags) /** * @param string $variable * + * @param array $tags * @return \Prometheus\Gauge */ private function getOrRegisterGaugeForVariable($variable, $tags = array()) From 0cfe45a99d8334b345fa49996b8137b5556a9dfd Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 18:01:48 +0100 Subject: [PATCH 06/20] Potential correction UT --- src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php index c16cfd2..217a176 100755 --- a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php +++ b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php @@ -121,7 +121,7 @@ public function testMeasureWithInlineTags() ->method('set') ->with($expectedVariableValue, array_values(array_merge($globalTags, $inlineTags))) ; - $gaugeMock + $gaugeMock2 ->expects($this->once()) ->method('set') ->with($expectedVariableValue2, array_values($globalTags)) From 466ee2705cef33515c4e4f3f7eeb08b8c17846b4 Mon Sep 17 00:00:00 2001 From: Bartosz Bialek Date: Thu, 23 Feb 2017 12:24:36 +0000 Subject: [PATCH 07/20] Added in memory collector --- .../BeberleiMetricsExtension.php | 1 + .../Resources/config/metrics.xml | 2 + .../BeberleiMetricsExtensionTest.php | 13 ++ src/Beberlei/Metrics/Collector/InMemory.php | 149 ++++++++++++++++++ .../Metrics/Tests/Collector/InMemoryTest.php | 134 ++++++++++++++++ 5 files changed, 299 insertions(+) create mode 100644 src/Beberlei/Metrics/Collector/InMemory.php create mode 100644 src/Beberlei/Metrics/Tests/Collector/InMemoryTest.php diff --git a/src/Beberlei/Bundle/MetricsBundle/DependencyInjection/BeberleiMetricsExtension.php b/src/Beberlei/Bundle/MetricsBundle/DependencyInjection/BeberleiMetricsExtension.php index c8ba150..0bfef52 100644 --- a/src/Beberlei/Bundle/MetricsBundle/DependencyInjection/BeberleiMetricsExtension.php +++ b/src/Beberlei/Bundle/MetricsBundle/DependencyInjection/BeberleiMetricsExtension.php @@ -78,6 +78,7 @@ private function createCollector($type, $config) return $definition; case 'logger': case 'null': + case 'memory': return $definition; case 'prometheus': $definition->replaceArgument(0, new Reference($config['prometheus_collector_registry'])); diff --git a/src/Beberlei/Bundle/MetricsBundle/Resources/config/metrics.xml b/src/Beberlei/Bundle/MetricsBundle/Resources/config/metrics.xml index b383fdd..128fd63 100644 --- a/src/Beberlei/Bundle/MetricsBundle/Resources/config/metrics.xml +++ b/src/Beberlei/Bundle/MetricsBundle/Resources/config/metrics.xml @@ -60,6 +60,8 @@ + + diff --git a/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php b/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php index c9d6700..58ddb4d 100644 --- a/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php +++ b/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php @@ -330,6 +330,19 @@ public function testWithPrometheus() $this->assertSame('', $this->getProperty($collector, 'namespace')); } + public function testWithInMemory() + { + $container = $this->createContainer(array( + 'collectors' => array( + 'memory' => array( + 'type' => 'memory' + ) + ), + )); + $collector = $container->get('beberlei_metrics.collector.memory'); + $this->assertInstanceOf('Beberlei\Metrics\Collector\InMemory', $collector); + } + public function testWithPrometheusAndWithNamespace() { $expectedNamespace = 'some_namespace'; diff --git a/src/Beberlei/Metrics/Collector/InMemory.php b/src/Beberlei/Metrics/Collector/InMemory.php new file mode 100644 index 0000000..1344241 --- /dev/null +++ b/src/Beberlei/Metrics/Collector/InMemory.php @@ -0,0 +1,149 @@ +incrementData[$variable])) { + $this->incrementData[$variable] = 0; + } + $this->incrementData[$variable] += $value; + } + + /** + * Increments a counter. + * + * @param string $variable + */ + public function increment($variable) + { + $this->measure($variable, 1); + } + + /** + * Decrements a counter. + * + * @param string $variable + */ + public function decrement($variable) + { + $this->measure($variable, -1); + } + + /** + * Records a timing. + * + * @param string $variable + * @param int $time The duration of the timing in milliseconds + */ + public function timing($variable, $time) + { + if (!isset($this->timingData[$variable])) { + $this->timingData[$variable] = 0; + } + $this->timingData[$variable] = $time; + } + + /** + * Sends the metrics to the adapter backend. + */ + public function flush() + { + $this->timingData = []; + $this->gaugeData = []; + $this->incrementData = []; + } + + /** + * Updates a gauge by an arbitrary amount. + * + * @param string $variable + * @param int $value + */ + public function gauge($variable, $value) + { + $sign = substr($value, 0, 1); + + if (in_array($sign, ['-', '+'])) { + $this->gaugeIncrement($variable, (int)$value); + return; + } + + $this->gaugeData[$variable] = $value; + } + + /** + * Returns current value of incremented/decremented/measured variable + * + * @param string $variable + * @return int + */ + public function getMeasure($variable) + { + return isset($this->incrementData[$variable]) ? $this->incrementData[$variable] : 0; + } + + /** + * Returns current value of gauged variable + * + * @param string $variable + * @return int + */ + public function getGauge($variable) + { + return isset($this->gaugeData[$variable]) ? $this->gaugeData[$variable] : 0; + } + + /** + * Returns current value of timed variable + * + * @param string $variable + * @return int + */ + public function getTiming($variable) + { + return isset($this->timingData[$variable]) ? $this->timingData[$variable] : 0; + } + + /** + * @param string $variable + * @param int $value + */ + private function gaugeIncrement($variable, $value) + { + if (!isset($this->gaugeData[$variable])) { + $this->gaugeData[$variable] = 0; + } + + $this->gaugeData[$variable] += $value; + } +} diff --git a/src/Beberlei/Metrics/Tests/Collector/InMemoryTest.php b/src/Beberlei/Metrics/Tests/Collector/InMemoryTest.php new file mode 100644 index 0000000..06b7a65 --- /dev/null +++ b/src/Beberlei/Metrics/Tests/Collector/InMemoryTest.php @@ -0,0 +1,134 @@ +collector = new InMemory(); + } + + public function testIncrement() + { + $this->collector->increment(self::VARIABLE_A); + $this->collector->increment(self::VARIABLE_A); + + $this->collector->increment(self::VARIABLE_B); + + $this->assertEquals(2, $this->collector->getMeasure(self::VARIABLE_A)); + $this->assertEquals(1, $this->collector->getMeasure(self::VARIABLE_B)); + } + + public function testDecrement() + { + $this->collector->increment(self::VARIABLE_A); + $this->collector->increment(self::VARIABLE_A); + $this->collector->decrement(self::VARIABLE_A); + + $this->collector->decrement(self::VARIABLE_B); + $this->collector->decrement(self::VARIABLE_B); + + $this->assertEquals(1, $this->collector->getMeasure(self::VARIABLE_A)); + $this->assertEquals(-2, $this->collector->getMeasure(self::VARIABLE_B)); + } + + public function testTiming() + { + $this->collector->timing(self::VARIABLE_A, 123); + + $this->collector->timing(self::VARIABLE_B, 111); + $this->collector->timing(self::VARIABLE_B, 112); + + $this->assertEquals(123, $this->collector->getTiming(self::VARIABLE_A)); + $this->assertEquals(112, $this->collector->getTiming(self::VARIABLE_B)); + } + + public function testMeasure() + { + $this->collector->measure(self::VARIABLE_A, 2); + $this->collector->measure(self::VARIABLE_A, -5); + + $this->collector->measure(self::VARIABLE_B, 123); + $this->collector->measure(self::VARIABLE_B, 0); + + $this->assertEquals(-3, $this->collector->getMeasure(self::VARIABLE_A)); + $this->assertEquals(123, $this->collector->getMeasure(self::VARIABLE_B)); + } + + public function testSettingGauge() + { + $this->collector->gauge(self::VARIABLE_A, 2); + $this->collector->gauge(self::VARIABLE_A, 5); + + + $this->collector->gauge(self::VARIABLE_B, 123); + $this->collector->gauge(self::VARIABLE_B, 0); + + $this->assertEquals(5, $this->collector->getGauge(self::VARIABLE_A)); + $this->assertEquals(0, $this->collector->getGauge(self::VARIABLE_B)); + } + + public function testIncrementingGauge() + { + $this->collector->gauge(self::VARIABLE_A, '10'); + $this->collector->gauge(self::VARIABLE_A, '+2'); + $this->collector->gauge(self::VARIABLE_A, '-3'); + + $this->assertEquals(9, $this->collector->getGauge(self::VARIABLE_A)); + } + + public function testSettingGaugeToNegativeValue() + { + $this->collector->gauge(self::VARIABLE_A, 1); //sets to 1 + $this->collector->gauge(self::VARIABLE_A, 2); //sets to 2 + $this->collector->gauge(self::VARIABLE_A, -5); //decreases by 5 + $this->assertEquals(-3, $this->collector->getGauge(self::VARIABLE_A)); + + $this->collector->gauge(self::VARIABLE_A, 0); + $this->collector->gauge(self::VARIABLE_A, -5); + $this->assertEquals(-5, $this->collector->getGauge(self::VARIABLE_A)); + } + + public function testTypesOfMetricsAreSeparate() + { + $this->collector->increment(self::VARIABLE_A); + $this->collector->gauge(self::VARIABLE_A, 2); + $this->collector->timing(self::VARIABLE_A, 3); + + $this->assertEquals(1, $this->collector->getMeasure(self::VARIABLE_A)); + $this->assertEquals(2, $this->collector->getGauge(self::VARIABLE_A)); + $this->assertEquals(3, $this->collector->getTiming(self::VARIABLE_A)); + } + + public function testFlushClearsData() + { + $this->collector->increment(self::VARIABLE_A); + $this->collector->gauge(self::VARIABLE_A, 2); + $this->collector->timing(self::VARIABLE_A, 3); + + $this->collector->flush(); + + $this->assertEquals(0, $this->collector->getMeasure(self::VARIABLE_A)); + $this->assertEquals(0, $this->collector->getGauge(self::VARIABLE_A)); + $this->assertEquals(0, $this->collector->getTiming(self::VARIABLE_A)); + } +} From 0fb6d89a1f16c1b230927527320614654af47da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Mon, 6 Mar 2017 14:15:34 +0100 Subject: [PATCH 08/20] Fixed CS --- .../MetricsBundle/BeberleiMetricsBundle.php | 1 + .../BeberleiMetricsExtensionTest.php | 7 ++--- src/Beberlei/Metrics/Collector/Collector.php | 1 + .../Metrics/Collector/DoctrineDBAL.php | 1 + src/Beberlei/Metrics/Collector/DogStatsD.php | 1 + .../Metrics/Collector/GaugeableCollector.php | 1 + src/Beberlei/Metrics/Collector/Graphite.php | 1 + src/Beberlei/Metrics/Collector/InMemory.php | 27 +++++++++++-------- src/Beberlei/Metrics/Collector/InfluxDB.php | 1 + .../InlineTaggableGaugeableCollector.php | 1 + src/Beberlei/Metrics/Collector/Librato.php | 1 + src/Beberlei/Metrics/Collector/Logger.php | 1 + src/Beberlei/Metrics/Collector/Null.php | 4 +-- .../Metrics/Collector/NullCollector.php | 1 + src/Beberlei/Metrics/Collector/Prometheus.php | 1 + src/Beberlei/Metrics/Collector/StatsD.php | 1 + .../Metrics/Collector/TaggableCollector.php | 1 + src/Beberlei/Metrics/Collector/Telegraf.php | 1 + src/Beberlei/Metrics/Collector/Zabbix.php | 1 + src/Beberlei/Metrics/Factory.php | 1 + src/Beberlei/Metrics/MetricsException.php | 1 + .../Metrics/Tests/Collector/InMemoryTest.php | 4 +-- .../Metrics/Tests/Collector/InfluxDBTest.php | 1 + .../Tests/Collector/PrometheusTest.php | 1 + 24 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/Beberlei/Bundle/MetricsBundle/BeberleiMetricsBundle.php b/src/Beberlei/Bundle/MetricsBundle/BeberleiMetricsBundle.php index 579a346..8eba1a6 100644 --- a/src/Beberlei/Bundle/MetricsBundle/BeberleiMetricsBundle.php +++ b/src/Beberlei/Bundle/MetricsBundle/BeberleiMetricsBundle.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Bundle\MetricsBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; diff --git a/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php b/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php index 58ddb4d..d9de71c 100644 --- a/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php +++ b/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Bundle\MetricsBundle\Tests\DependencyInjection; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -49,7 +50,7 @@ public function testWithGraphite() } /** - * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException + * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException * The source has to be specified to use a Librato */ public function testWithLibratoAndInvalidConfiguration() @@ -335,8 +336,8 @@ public function testWithInMemory() $container = $this->createContainer(array( 'collectors' => array( 'memory' => array( - 'type' => 'memory' - ) + 'type' => 'memory', + ), ), )); $collector = $container->get('beberlei_metrics.collector.memory'); diff --git a/src/Beberlei/Metrics/Collector/Collector.php b/src/Beberlei/Metrics/Collector/Collector.php index 4e7179b..51d44a0 100644 --- a/src/Beberlei/Metrics/Collector/Collector.php +++ b/src/Beberlei/Metrics/Collector/Collector.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Collector; /** diff --git a/src/Beberlei/Metrics/Collector/DoctrineDBAL.php b/src/Beberlei/Metrics/Collector/DoctrineDBAL.php index 83e0b56..33ac0f6 100644 --- a/src/Beberlei/Metrics/Collector/DoctrineDBAL.php +++ b/src/Beberlei/Metrics/Collector/DoctrineDBAL.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Collector; use Doctrine\DBAL\Connection; diff --git a/src/Beberlei/Metrics/Collector/DogStatsD.php b/src/Beberlei/Metrics/Collector/DogStatsD.php index 1207239..7f9a82d 100644 --- a/src/Beberlei/Metrics/Collector/DogStatsD.php +++ b/src/Beberlei/Metrics/Collector/DogStatsD.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Collector; class DogStatsD implements Collector, InlineTaggableGaugeableCollector diff --git a/src/Beberlei/Metrics/Collector/GaugeableCollector.php b/src/Beberlei/Metrics/Collector/GaugeableCollector.php index 68446aa..ffeb98f 100644 --- a/src/Beberlei/Metrics/Collector/GaugeableCollector.php +++ b/src/Beberlei/Metrics/Collector/GaugeableCollector.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Collector; interface GaugeableCollector diff --git a/src/Beberlei/Metrics/Collector/Graphite.php b/src/Beberlei/Metrics/Collector/Graphite.php index 1714b62..e052565 100644 --- a/src/Beberlei/Metrics/Collector/Graphite.php +++ b/src/Beberlei/Metrics/Collector/Graphite.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Collector; use Exception; diff --git a/src/Beberlei/Metrics/Collector/InMemory.php b/src/Beberlei/Metrics/Collector/InMemory.php index 1344241..95df43b 100644 --- a/src/Beberlei/Metrics/Collector/InMemory.php +++ b/src/Beberlei/Metrics/Collector/InMemory.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Collector; /** @@ -18,18 +19,18 @@ */ class InMemory implements Collector, GaugeableCollector { - /** @var int[] */ + /** @var int[] */ private $incrementData = []; - /** @var int[] */ + /** @var int[] */ private $gaugeData = []; - /** @var int[] */ + /** @var int[] */ private $timingData = []; /** * Updates a counter by some arbitrary amount. * * @param string $variable - * @param int $value The amount to increment the counter by + * @param int $value The amount to increment the counter by */ public function measure($variable, $value) { @@ -63,7 +64,7 @@ public function decrement($variable) * Records a timing. * * @param string $variable - * @param int $time The duration of the timing in milliseconds + * @param int $time The duration of the timing in milliseconds */ public function timing($variable, $time) { @@ -87,14 +88,15 @@ public function flush() * Updates a gauge by an arbitrary amount. * * @param string $variable - * @param int $value + * @param int $value */ public function gauge($variable, $value) { $sign = substr($value, 0, 1); if (in_array($sign, ['-', '+'])) { - $this->gaugeIncrement($variable, (int)$value); + $this->gaugeIncrement($variable, (int) $value); + return; } @@ -102,9 +104,10 @@ public function gauge($variable, $value) } /** - * Returns current value of incremented/decremented/measured variable + * Returns current value of incremented/decremented/measured variable. * * @param string $variable + * * @return int */ public function getMeasure($variable) @@ -113,9 +116,10 @@ public function getMeasure($variable) } /** - * Returns current value of gauged variable + * Returns current value of gauged variable. * * @param string $variable + * * @return int */ public function getGauge($variable) @@ -124,9 +128,10 @@ public function getGauge($variable) } /** - * Returns current value of timed variable + * Returns current value of timed variable. * * @param string $variable + * * @return int */ public function getTiming($variable) @@ -136,7 +141,7 @@ public function getTiming($variable) /** * @param string $variable - * @param int $value + * @param int $value */ private function gaugeIncrement($variable, $value) { diff --git a/src/Beberlei/Metrics/Collector/InfluxDB.php b/src/Beberlei/Metrics/Collector/InfluxDB.php index 67768cd..ae657b8 100644 --- a/src/Beberlei/Metrics/Collector/InfluxDB.php +++ b/src/Beberlei/Metrics/Collector/InfluxDB.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Collector; use InfluxDB\Client; diff --git a/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php b/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php index 3873cec..1fdebb1 100644 --- a/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php +++ b/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Collector; /** diff --git a/src/Beberlei/Metrics/Collector/Librato.php b/src/Beberlei/Metrics/Collector/Librato.php index 53502ce..9bbd96d 100644 --- a/src/Beberlei/Metrics/Collector/Librato.php +++ b/src/Beberlei/Metrics/Collector/Librato.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Collector; use Buzz\Browser; diff --git a/src/Beberlei/Metrics/Collector/Logger.php b/src/Beberlei/Metrics/Collector/Logger.php index aa689c7..c501981 100644 --- a/src/Beberlei/Metrics/Collector/Logger.php +++ b/src/Beberlei/Metrics/Collector/Logger.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Collector; use Psr\Log\LoggerInterface; diff --git a/src/Beberlei/Metrics/Collector/Null.php b/src/Beberlei/Metrics/Collector/Null.php index 76d531a..f651dc8 100644 --- a/src/Beberlei/Metrics/Collector/Null.php +++ b/src/Beberlei/Metrics/Collector/Null.php @@ -1,6 +1,6 @@ collector->gauge(self::VARIABLE_A, 2); $this->collector->gauge(self::VARIABLE_A, 5); - $this->collector->gauge(self::VARIABLE_B, 123); $this->collector->gauge(self::VARIABLE_B, 0); diff --git a/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php b/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php index 70ec9d8..a61e7da 100644 --- a/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php +++ b/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Tests\Collector; use PHPUnit_Framework_MockObject_MockObject; diff --git a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php index 7d2aae4..822d425 100644 --- a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php +++ b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php @@ -10,6 +10,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + namespace Beberlei\Metrics\Tests\Collector; use Beberlei\Metrics\Collector\Prometheus; From 13cf6815b05f9c5f4f8b2bab8acc6e4843149648 Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 17:23:02 +0100 Subject: [PATCH 09/20] Made all taggable collectors inline-taggable --- src/Beberlei/Metrics/Collector/DogStatsD.php | 2 +- src/Beberlei/Metrics/Collector/InfluxDB.php | 18 +- src/Beberlei/Metrics/Collector/Prometheus.php | 31 +-- .../Metrics/Collector/TaggableCollector.php | 36 +++ .../Collector/TaggableGaugeableCollector.php | 28 +++ src/Beberlei/Metrics/Collector/Telegraf.php | 48 ++-- .../Metrics/Tests/Collector/InfluxDBTest.php | 50 +++++ .../Tests/Collector/PrometheusTest.php | 208 ++++++++++++++++++ 8 files changed, 381 insertions(+), 40 deletions(-) mode change 100644 => 100755 src/Beberlei/Metrics/Collector/InfluxDB.php create mode 100755 src/Beberlei/Metrics/Collector/TaggableGaugeableCollector.php mode change 100644 => 100755 src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php diff --git a/src/Beberlei/Metrics/Collector/DogStatsD.php b/src/Beberlei/Metrics/Collector/DogStatsD.php index 7f9a82d..7320248 100644 --- a/src/Beberlei/Metrics/Collector/DogStatsD.php +++ b/src/Beberlei/Metrics/Collector/DogStatsD.php @@ -13,7 +13,7 @@ namespace Beberlei\Metrics\Collector; -class DogStatsD implements Collector, InlineTaggableGaugeableCollector +class DogStatsD implements Collector, TaggableCollector, TaggableGaugeableCollector { /** @var string */ private $host; diff --git a/src/Beberlei/Metrics/Collector/InfluxDB.php b/src/Beberlei/Metrics/Collector/InfluxDB.php old mode 100644 new mode 100755 index ae657b8..dca72e8 --- a/src/Beberlei/Metrics/Collector/InfluxDB.php +++ b/src/Beberlei/Metrics/Collector/InfluxDB.php @@ -37,33 +37,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); } /** @@ -79,7 +79,7 @@ public function flush() 'fields' => array('value' => $data[1]), ), ), - 'tags' => $this->tags, + 'tags' => array_merge($this->tags, $data[2]), )); } diff --git a/src/Beberlei/Metrics/Collector/Prometheus.php b/src/Beberlei/Metrics/Collector/Prometheus.php index ced78e2..ca33e1f 100644 --- a/src/Beberlei/Metrics/Collector/Prometheus.php +++ b/src/Beberlei/Metrics/Collector/Prometheus.php @@ -52,42 +52,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); } /** @@ -99,22 +102,24 @@ public function flush() return; } - $tagsValues = array_values($this->tags); + $globalTagsValues = array_values($this->tags); foreach ($this->data['counters'] as $counterData) { - $gauge = $this->getOrRegisterGaugeForVariable($counterData['name']); + $gauge = $this->getOrRegisterGaugeForVariable($counterData['name'], $counterData['tags']); + $tagsValues = array_values($counterData['tags']); if ($counterData['value'] > 0) { - $gauge->inc($tagsValues); + $gauge->inc(array_merge($globalTagsValues, $tagsValues)); } elseif ($counterData['value'] < 0) { - $gauge->dec($tagsValues); + $gauge->dec(array_merge($globalTagsValues, $tagsValues)); } } foreach ($this->data['gauges'] as $gaugeData) { - $gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name']); + $gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name'], $counterData['tags']); - $gauge->set($gaugeData['value'], $tagsValues); + $tagsValues = array_values($counterData['tags']); + $gauge->set($gaugeData['value'], array_merge($globalTagsValues, $tagsValues)); } $this->data = array('counters' => array(), 'gauges' => array()); @@ -133,7 +138,7 @@ public function setTags($tags) * * @return \Prometheus\Gauge */ - private function getOrRegisterGaugeForVariable($variable) + private function getOrRegisterGaugeForVariable($variable, $tags = array()) { try { $gauge = $this->collectorRegistry->getGauge($this->namespace, $variable); @@ -142,7 +147,7 @@ private function getOrRegisterGaugeForVariable($variable) $this->namespace, $variable, '', - array_keys($this->tags) + array_keys(array_merge($this->tags, $tags)) ); } diff --git a/src/Beberlei/Metrics/Collector/TaggableCollector.php b/src/Beberlei/Metrics/Collector/TaggableCollector.php index 2e9cda0..17fd969 100644 --- a/src/Beberlei/Metrics/Collector/TaggableCollector.php +++ b/src/Beberlei/Metrics/Collector/TaggableCollector.php @@ -24,4 +24,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()); } diff --git a/src/Beberlei/Metrics/Collector/TaggableGaugeableCollector.php b/src/Beberlei/Metrics/Collector/TaggableGaugeableCollector.php new file mode 100755 index 0000000..189f1a6 --- /dev/null +++ b/src/Beberlei/Metrics/Collector/TaggableGaugeableCollector.php @@ -0,0 +1,28 @@ +tags = http_build_query($tags, '', ','); - $this->tags = (strlen($this->tags) > 0 ? ','.$this->tags : $this->tags); - } + $this->tags = $tags; + } /** * {@inheritdoc} */ - public function timing($variable, $time) + public function timing($variable, $time, $tags = array()) { - $this->data[] = sprintf('%s%s:%s|ms', $variable, $this->tags, $time); + $this->data[] = sprintf('%s%s:%s|ms', $variable, $this->buildTagString(array_merge($this->tags, $tags)), $time); } /** * {@inheritdoc} */ - public function increment($variable) + public function increment($variable, $tags = array()) { - $this->data[] = $variable.$this->tags.':1|c'; + $this->data[] = $variable.$this->buildTagString(array_merge($this->tags, $tags)).':1|c'; } /** * {@inheritdoc} */ - public function decrement($variable) + public function decrement($variable, $tags = array()) { - $this->data[] = $variable.$this->tags.':-1|c'; + $this->data[] = $variable.$this->buildTagString(array_merge($this->tags, $tags)).':-1|c'; } /** * {@inheritdoc} */ - public function measure($variable, $value) + public function measure($variable, $value, $tags = array()) { - $this->data[] = sprintf('%s%s:%s|c', $variable, $this->tags, $value); + $this->data[] = sprintf('%s%s:%s|c', $variable, $this->buildTagString(array_merge($this->tags, $tags)), $value); } /** * {@inheritdoc} */ - public function gauge($variable, $value) + public function gauge($variable, $value, $tags = array()) { - $this->data[] = sprintf('%s%s:%s|g', $variable, $this->tags, $value); + $this->data[] = sprintf('%s%s:%s|g', $variable, $this->buildTagString(array_merge($this->tags, $tags)), $value); } /** @@ -103,7 +102,7 @@ public function gauge($variable, $value) */ public function set($variable, $value) { - $this->data[] = sprintf('%s%s:%s|s', $variable, $this->tags, $value); + $this->data[] = sprintf('%s%s:%s|s', $variable, $this->buildTagString(array_merge($this->tags, $tags)), $value); } /** @@ -131,4 +130,19 @@ public function flush() $this->data = array(); } + + /** + * Given a key/value map of metric tags, builds them into a + * telegraf statsd tag string and returns the string. + * + * @param $tags array + * + * @return string + */ + private function buildTagString($tags) + { + $tagString = http_build_query($tags, '', ','); + $tagString = (strlen($this->tags) > 0 ? ','.$this->tags : $this->tags); + return $tagString; + } } diff --git a/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php b/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php old mode 100644 new mode 100755 index a61e7da..c9b78cb --- a/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php +++ b/src/Beberlei/Metrics/Tests/Collector/InfluxDBTest.php @@ -141,4 +141,54 @@ public function testCollectMeasureWithTags() $this->collector->measure('series-name', 47.11); $this->collector->flush(); } + + public function testCollectMeasureWithInlineTags() + { + $globalTags = array( + 'dc' => 'west', + 'node' => 'nemesis101', + ); + + $inlineTags = array( + 'resource' => 'stuff' + ); + + $expectedTags = array_merge($globalTags, $inlineTags); + + $expectedArgs = array( + 'points' => array( + array( + 'measurement' => 'series-name', + 'fields' => array('value' => 47.11), + ), + ), + 'tags' => $expectedTags, + ); + + $expectedArgs2 = array( + 'points' => array( + array( + 'measurement' => 'series-name-2', + 'fields' => array('value' => 53.22), + ), + ), + 'tags' => $globalTags, + ); + + $this->client->expects($this->exactly(2)) + ->method('mark'); + + $this->client->expects($this->at(0)) + ->method('mark') + ->with($expectedArgs); + + $this->client->expects($this->at(1)) + ->method('mark') + ->with($expectedArgs2); + + $this->collector->setTags($globalTags); + $this->collector->measure('series-name', 47.11, $inlineTags); + $this->collector->measure('series-name-2', 53.22); + $this->collector->flush(); + } } diff --git a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php index 822d425..89cb34e 100644 --- a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php +++ b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php @@ -97,6 +97,59 @@ public function testMeasureWithTags() $this->collector->flush(); } + public function testMeasureWithInlineTags() + { + $expectedVariableValue = 123; + $expectedVariableValue2 = 456; + $globalTags = array( + 'tag1' => 'value1', + 'tag2' => 'value2', + ); + $inlineTags = array( + 'tag3' => 'value3', + ); + + $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock2 = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock + ->expects($this->once()) + ->method('set') + ->with($expectedVariableValue, array_values(array_merge($globalTags, $inlineTags))) + ; + $gaugeMock + ->expects($this->once()) + ->method('set') + ->with($expectedVariableValue2, array_values($globalTags)) + ; + + $this->collectorRegistryMock + ->expects($this->exactly(2)); + $this->collectorRegistryMock + ->expects($this->at(0)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME) + ->willReturn($gaugeMock) + ; + $this->collectorRegistryMock + ->expects($this->at(1)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME.'_2') + ->willReturn($gaugeMock2) + ; + + $this->collector->setTags($globalTags); + + $this->collector->measure(self::TEST_VARIABLE_NAME, $expectedVariableValue, $inlineTags); + $this->collector->measure(self::TEST_VARIABLE_NAME.'_2', $expectedVariableValue2); + $this->collector->flush(); + } + public function testIncrement() { $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') @@ -150,6 +203,57 @@ public function testIncrementWithTags() $this->collector->flush(); } + public function testIncrementWithInlineTags() + { + $globalTags = array( + 'tag1' => 'value1', + 'tag2' => 'value2', + ); + $inlineTags = array( + 'tag3' => 'value3', + ); + + $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock2 = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock + ->expects($this->once()) + ->method('inc') + ->with(array_values(array_merge($globalTags, $inlineTags))) + ; + $gaugeMock2 + ->expects($this->once()) + ->method('inc') + ->with(array_values($globalTags)) + ; + + $this->collectorRegistryMock + ->expects($this->exactly(2)); + $this->collectorRegistryMock + ->expects($this->at(0)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME) + ->willReturn($gaugeMock) + ; + $this->collectorRegistryMock + ->expects($this->at(1)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME.'_2') + ->willReturn($gaugeMock2) + ; + + $this->collector->setTags($globalTags); + + $this->collector->increment(self::TEST_VARIABLE_NAME, $inlineTags); + $this->collector->increment(self::TEST_VARIABLE_NAME.'_2'); + $this->collector->flush(); + } + public function testDecrement() { $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') @@ -203,6 +307,57 @@ public function testDecrementWithTags() $this->collector->flush(); } + public function testDecrementWithInlineTags() + { + $globalTags = array( + 'tag1' => 'value1', + 'tag2' => 'value2', + ); + $inlineTags = array( + 'tag3' => 'value3', + ); + + $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock2 = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock + ->expects($this->once()) + ->method('dec') + ->with(array_merge($globalTags, $inlineTags)) + ; + $gaugeMock2 + ->expects($this->once()) + ->method('dec') + ->with(array_values($globalTags)) + ; + + $this->collectorRegistryMock + ->expects($this->exactly(2)); + $this->collectorRegistryMock + ->expects($this->at(0)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME) + ->willReturn($gaugeMock) + ; + $this->collectorRegistryMock + ->expects($this->at(1)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME.'_2') + ->willReturn($gaugeMock2) + ; + + $this->collector->setTags($globalTags); + + $this->collector->decrement(self::TEST_VARIABLE_NAME, $inlineTags); + $this->collector->decrement(self::TEST_VARIABLE_NAME.'_2'); + $this->collector->flush(); + } + public function testTiming() { $expectedVariableValue = 123; @@ -259,6 +414,59 @@ public function testTimingWithTags() $this->collector->flush(); } + public function testTimingWithInlineTags() + { + $expectedVariableValue = 123; + $expectedVariableValue2 = 456; + $globalTags = array( + 'tag1' => 'value1', + 'tag2' => 'value2', + ); + $inlineTags = array( + 'tag3' => 'value3', + ); + + $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock2 = $this->getMockBuilder('\\Prometheus\\Gauge') + ->disableOriginalConstructor() + ->getMock() + ; + $gaugeMock + ->expects($this->once()) + ->method('set') + ->with($expectedVariableValue, array_values(array_merge($globalTags, $inlineTags))) + ; + $gaugeMock2 + ->expects($this->once()) + ->method('set') + ->with($expectedVariableValue, array_values($globalTags)) + ; + + $this->collectorRegistryMock + ->expects($this->exactly(2)); + $this->collectorRegistryMock + ->expects($this->at(0)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME) + ->willReturn($gaugeMock) + ; + $this->collectorRegistryMock + ->expects($this->at(0)) + ->method('getGauge') + ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME.'_2') + ->willReturn($gaugeMock2) + ; + + $this->collector->setTags($globalTags); + + $this->collector->timing(self::TEST_VARIABLE_NAME, $expectedVariableValue, $inlineTags); + $this->collector->timing(self::TEST_VARIABLE_NAME.'_2', $expectedVariableValue); + $this->collector->flush(); + } + public function testMeasureWhenSetNewVariableWithTags() { $expectedVariableValue = 123; From a1f9aefea95cae78c726166e6c2d98b08e1ed89e Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 17:26:16 +0100 Subject: [PATCH 10/20] Corrected interface name --- src/Beberlei/Metrics/Collector/Telegraf.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 src/Beberlei/Metrics/Collector/Telegraf.php diff --git a/src/Beberlei/Metrics/Collector/Telegraf.php b/src/Beberlei/Metrics/Collector/Telegraf.php old mode 100644 new mode 100755 index 2429742..8b2cb83 --- a/src/Beberlei/Metrics/Collector/Telegraf.php +++ b/src/Beberlei/Metrics/Collector/Telegraf.php @@ -18,7 +18,7 @@ * ad hoc implementation for the StatsD - Telegraf integration, * support tagging. */ -class Telegraf implements Collector, InlineTaggableGaugeableCollector +class Telegraf implements Collector, TaggableGaugeableCollector { /** @var string */ private $host; From 0010902cc07bf5cf445964e8a5b7eb028deef631 Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 17:26:36 +0100 Subject: [PATCH 11/20] Added global tags support --- src/Beberlei/Metrics/Collector/DogStatsD.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) mode change 100644 => 100755 src/Beberlei/Metrics/Collector/DogStatsD.php diff --git a/src/Beberlei/Metrics/Collector/DogStatsD.php b/src/Beberlei/Metrics/Collector/DogStatsD.php old mode 100644 new mode 100755 index 7320248..616bbcf --- a/src/Beberlei/Metrics/Collector/DogStatsD.php +++ b/src/Beberlei/Metrics/Collector/DogStatsD.php @@ -27,6 +27,9 @@ class DogStatsD implements Collector, TaggableCollector, TaggableGaugeableCollec /** @var array */ private $data; + /** @var array */ + private $tags = array(); + /** * @param string $host * @param string $port @@ -106,6 +109,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. @@ -118,6 +129,8 @@ private function buildTagString($tags) { $results = array(); + $tags = array_merge($this->tags, $tags); + foreach ($tags as $key => $value) { $results[] = sprintf('%s:%s', $key, $value); } From 1a12f8759716ee63bd290c942819cb49176732c8 Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 17:46:28 +0100 Subject: [PATCH 12/20] Corrected phpunit method matchers --- src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) mode change 100644 => 100755 src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php diff --git a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php old mode 100644 new mode 100755 index 89cb34e..63e79c3 --- a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php +++ b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php @@ -233,7 +233,8 @@ public function testIncrementWithInlineTags() ; $this->collectorRegistryMock - ->expects($this->exactly(2)); + ->expects($this->exactly(2)) + ->method('getGauge'); $this->collectorRegistryMock ->expects($this->at(0)) ->method('getGauge') @@ -337,7 +338,8 @@ public function testDecrementWithInlineTags() ; $this->collectorRegistryMock - ->expects($this->exactly(2)); + ->expects($this->exactly(2)) + ->method('getGauge'); $this->collectorRegistryMock ->expects($this->at(0)) ->method('getGauge') @@ -446,7 +448,8 @@ public function testTimingWithInlineTags() ; $this->collectorRegistryMock - ->expects($this->exactly(2)); + ->expects($this->exactly(2)) + ->method('getGauge'); $this->collectorRegistryMock ->expects($this->at(0)) ->method('getGauge') From d66320b3c65307ba75035b0d7ccb6b20d89b7f77 Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 17:56:54 +0100 Subject: [PATCH 13/20] Corrected my stupid logic --- src/Beberlei/Metrics/Collector/Prometheus.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) mode change 100644 => 100755 src/Beberlei/Metrics/Collector/Prometheus.php diff --git a/src/Beberlei/Metrics/Collector/Prometheus.php b/src/Beberlei/Metrics/Collector/Prometheus.php old mode 100644 new mode 100755 index ca33e1f..fad46ba --- a/src/Beberlei/Metrics/Collector/Prometheus.php +++ b/src/Beberlei/Metrics/Collector/Prometheus.php @@ -102,24 +102,20 @@ public function flush() return; } - $globalTagsValues = array_values($this->tags); - foreach ($this->data['counters'] as $counterData) { $gauge = $this->getOrRegisterGaugeForVariable($counterData['name'], $counterData['tags']); - $tagsValues = array_values($counterData['tags']); if ($counterData['value'] > 0) { - $gauge->inc(array_merge($globalTagsValues, $tagsValues)); + $gauge->inc(array_values(array_merge($this->tags, $counterData['tags']))); } elseif ($counterData['value'] < 0) { - $gauge->dec(array_merge($globalTagsValues, $tagsValues)); + $gauge->dec(array_values(array_merge($this->tags, $counterData['tags']))); } } foreach ($this->data['gauges'] as $gaugeData) { - $gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name'], $counterData['tags']); + $gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name'], $gaugeData['tags']); - $tagsValues = array_values($counterData['tags']); - $gauge->set($gaugeData['value'], array_merge($globalTagsValues, $tagsValues)); + $gauge->set($gaugeData['value'], array_values(array_merge($this->tags, $gaugeData['tags']))); } $this->data = array('counters' => array(), 'gauges' => array()); @@ -136,6 +132,7 @@ public function setTags($tags) /** * @param string $variable * + * @param array $tags * @return \Prometheus\Gauge */ private function getOrRegisterGaugeForVariable($variable, $tags = array()) From 12ebda8fc7911400f45ad1e9743b9c1e2fb999bc Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 23 Dec 2016 18:01:48 +0100 Subject: [PATCH 14/20] Potential correction UT --- src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php index 63e79c3..97d63a6 100755 --- a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php +++ b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php @@ -122,7 +122,7 @@ public function testMeasureWithInlineTags() ->method('set') ->with($expectedVariableValue, array_values(array_merge($globalTags, $inlineTags))) ; - $gaugeMock + $gaugeMock2 ->expects($this->once()) ->method('set') ->with($expectedVariableValue2, array_values($globalTags)) From 1bd9828cee196eb9e449cb88ceb7bf8336cb8591 Mon Sep 17 00:00:00 2001 From: jordan Date: Thu, 27 Apr 2017 20:02:27 +0200 Subject: [PATCH 15/20] Corrected Prometheus UT --- .../Metrics/Tests/Collector/PrometheusTest.php | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php index 97d63a6..4384a14 100755 --- a/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php +++ b/src/Beberlei/Metrics/Tests/Collector/PrometheusTest.php @@ -128,8 +128,6 @@ public function testMeasureWithInlineTags() ->with($expectedVariableValue2, array_values($globalTags)) ; - $this->collectorRegistryMock - ->expects($this->exactly(2)); $this->collectorRegistryMock ->expects($this->at(0)) ->method('getGauge') @@ -232,9 +230,6 @@ public function testIncrementWithInlineTags() ->with(array_values($globalTags)) ; - $this->collectorRegistryMock - ->expects($this->exactly(2)) - ->method('getGauge'); $this->collectorRegistryMock ->expects($this->at(0)) ->method('getGauge') @@ -329,7 +324,7 @@ public function testDecrementWithInlineTags() $gaugeMock ->expects($this->once()) ->method('dec') - ->with(array_merge($globalTags, $inlineTags)) + ->with(array_values(array_merge($globalTags, $inlineTags))) ; $gaugeMock2 ->expects($this->once()) @@ -337,9 +332,6 @@ public function testDecrementWithInlineTags() ->with(array_values($globalTags)) ; - $this->collectorRegistryMock - ->expects($this->exactly(2)) - ->method('getGauge'); $this->collectorRegistryMock ->expects($this->at(0)) ->method('getGauge') @@ -447,9 +439,6 @@ public function testTimingWithInlineTags() ->with($expectedVariableValue, array_values($globalTags)) ; - $this->collectorRegistryMock - ->expects($this->exactly(2)) - ->method('getGauge'); $this->collectorRegistryMock ->expects($this->at(0)) ->method('getGauge') @@ -457,7 +446,7 @@ public function testTimingWithInlineTags() ->willReturn($gaugeMock) ; $this->collectorRegistryMock - ->expects($this->at(0)) + ->expects($this->at(1)) ->method('getGauge') ->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME.'_2') ->willReturn($gaugeMock2) From 5cc73636bdd4b8ef38cfca88ed62eeb59173b921 Mon Sep 17 00:00:00 2001 From: jordan Date: Thu, 27 Apr 2017 20:16:16 +0200 Subject: [PATCH 16/20] Corrected Telegraf Symphony dependency injection UT --- composer.json | 3 ++- .../Tests/DependencyInjection/BeberleiMetricsExtensionTest.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index ca84d93..3fa7163 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "okitsu/zabbix-sender": "*@dev", "symfony/config": "~2.3", "symfony/dependency-injection": "~2.3", - "symfony/http-kernel": "~2.3" + "symfony/http-kernel": "~2.3", + "phpunit/phpunit": "4.5.0" }, "autoload": { "psr-4": { diff --git a/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php b/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php index d9de71c..2138fc4 100644 --- a/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php +++ b/src/Beberlei/Bundle/MetricsBundle/Tests/DependencyInjection/BeberleiMetricsExtensionTest.php @@ -207,7 +207,7 @@ public function testWithTelegraf() $this->assertSame(1234, $this->getProperty($collector, 'port')); $this->assertSame('application.com.symfony.', $this->getProperty($collector, 'prefix')); - $this->assertEquals(',string_tag=first_value,int_tag=123', $this->getProperty($collector, 'tags')); + $this->assertEquals($expectedTags, $this->getProperty($collector, 'tags')); } public function testWithZabbix() From 79146d289e202110eba48f2c09020d4d79907fab Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 28 Apr 2017 17:03:31 +0200 Subject: [PATCH 17/20] Added missing interface for Telegraf --- src/Beberlei/Metrics/Collector/Telegraf.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Beberlei/Metrics/Collector/Telegraf.php b/src/Beberlei/Metrics/Collector/Telegraf.php index 8b2cb83..6d9742f 100755 --- a/src/Beberlei/Metrics/Collector/Telegraf.php +++ b/src/Beberlei/Metrics/Collector/Telegraf.php @@ -18,7 +18,7 @@ * ad hoc implementation for the StatsD - Telegraf integration, * support tagging. */ -class Telegraf implements Collector, TaggableGaugeableCollector +class Telegraf implements Collector, TaggableCollector, TaggableGaugeableCollector { /** @var string */ private $host; From 941e4a3a54f135fb78a03eb0752b154329b7c8c4 Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 28 Apr 2017 17:37:06 +0200 Subject: [PATCH 18/20] Corrected Telegraf tag string construction --- src/Beberlei/Metrics/Collector/Telegraf.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Beberlei/Metrics/Collector/Telegraf.php b/src/Beberlei/Metrics/Collector/Telegraf.php index 6d9742f..e2cbc1d 100755 --- a/src/Beberlei/Metrics/Collector/Telegraf.php +++ b/src/Beberlei/Metrics/Collector/Telegraf.php @@ -142,7 +142,7 @@ public function flush() private function buildTagString($tags) { $tagString = http_build_query($tags, '', ','); - $tagString = (strlen($this->tags) > 0 ? ','.$this->tags : $this->tags); + $tagString = (strlen($tagString) > 0 ? ','.$tagString : $tagString); return $tagString; } } From 473d2966639d225dc60abd3dd50196dcd9cab604 Mon Sep 17 00:00:00 2001 From: jordan Date: Fri, 28 Apr 2017 17:43:45 +0200 Subject: [PATCH 19/20] Deprecated InlineTaggableGaugeableCollector, added it back to implems --- src/Beberlei/Metrics/Collector/DogStatsD.php | 2 +- .../Metrics/Collector/InlineTaggableGaugeableCollector.php | 1 + src/Beberlei/Metrics/Collector/Telegraf.php | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Beberlei/Metrics/Collector/DogStatsD.php b/src/Beberlei/Metrics/Collector/DogStatsD.php index 616bbcf..925dfde 100755 --- a/src/Beberlei/Metrics/Collector/DogStatsD.php +++ b/src/Beberlei/Metrics/Collector/DogStatsD.php @@ -13,7 +13,7 @@ namespace Beberlei\Metrics\Collector; -class DogStatsD implements Collector, TaggableCollector, TaggableGaugeableCollector +class DogStatsD implements Collector, TaggableCollector, TaggableGaugeableCollector, InlineTaggableGaugeableCollector { /** @var string */ private $host; diff --git a/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php b/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php index 1fdebb1..d250d30 100644 --- a/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php +++ b/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php @@ -15,6 +15,7 @@ /** * InlineTaggableGaugeableCollector interface. + * @deprecated Replaced by TaggableCollector + TaggableGaugeableCollector */ interface InlineTaggableGaugeableCollector { diff --git a/src/Beberlei/Metrics/Collector/Telegraf.php b/src/Beberlei/Metrics/Collector/Telegraf.php index e2cbc1d..ad1948a 100755 --- a/src/Beberlei/Metrics/Collector/Telegraf.php +++ b/src/Beberlei/Metrics/Collector/Telegraf.php @@ -18,7 +18,7 @@ * ad hoc implementation for the StatsD - Telegraf integration, * support tagging. */ -class Telegraf implements Collector, TaggableCollector, TaggableGaugeableCollector +class Telegraf implements Collector, TaggableCollector, TaggableGaugeableCollector, InlineTaggableGaugeableCollector { /** @var string */ private $host; From 9e54874b6586df8ada94ed3f59d2332b33c04fed Mon Sep 17 00:00:00 2001 From: "jordan.besly" Date: Wed, 6 Jun 2018 15:32:22 +0200 Subject: [PATCH 20/20] Added missing function parameter --- src/Beberlei/Metrics/Collector/Telegraf.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Beberlei/Metrics/Collector/Telegraf.php b/src/Beberlei/Metrics/Collector/Telegraf.php index ad1948a..67be04a 100755 --- a/src/Beberlei/Metrics/Collector/Telegraf.php +++ b/src/Beberlei/Metrics/Collector/Telegraf.php @@ -99,8 +99,9 @@ public function gauge($variable, $value, $tags = array()) /** * @param $variable * @param $value + * @param array $tags */ - public function set($variable, $value) + public function set($variable, $value, $tags = array()) { $this->data[] = sprintf('%s%s:%s|s', $variable, $this->buildTagString(array_merge($this->tags, $tags)), $value); }