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/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/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..2138fc4 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() @@ -206,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() @@ -330,6 +331,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/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 old mode 100644 new mode 100755 index 1207239..925dfde --- a/src/Beberlei/Metrics/Collector/DogStatsD.php +++ b/src/Beberlei/Metrics/Collector/DogStatsD.php @@ -10,9 +10,10 @@ * 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 +class DogStatsD implements Collector, TaggableCollector, TaggableGaugeableCollector, InlineTaggableGaugeableCollector { /** @var string */ private $host; @@ -26,6 +27,9 @@ class DogStatsD implements Collector, InlineTaggableGaugeableCollector /** @var array */ private $data; + /** @var array */ + private $tags = array(); + /** * @param string $host * @param string $port @@ -105,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. @@ -117,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); } 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 new file mode 100644 index 0000000..95df43b --- /dev/null +++ b/src/Beberlei/Metrics/Collector/InMemory.php @@ -0,0 +1,154 @@ +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/Collector/InfluxDB.php b/src/Beberlei/Metrics/Collector/InfluxDB.php old mode 100644 new mode 100755 index 67768cd..dca72e8 --- 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; @@ -36,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); } /** @@ -78,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/InlineTaggableGaugeableCollector.php b/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php index 3873cec..d250d30 100644 --- a/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php +++ b/src/Beberlei/Metrics/Collector/InlineTaggableGaugeableCollector.php @@ -10,10 +10,12 @@ * 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; /** * InlineTaggableGaugeableCollector interface. + * @deprecated Replaced by TaggableCollector + TaggableGaugeableCollector */ interface InlineTaggableGaugeableCollector { 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 @@ 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 +102,20 @@ public function flush() return; } - $tagsValues = array_values($this->tags); - foreach ($this->data['counters'] as $counterData) { - $gauge = $this->getOrRegisterGaugeForVariable($counterData['name']); + $gauge = $this->getOrRegisterGaugeForVariable($counterData['name'], $counterData['tags']); if ($counterData['value'] > 0) { - $gauge->inc($tagsValues); + $gauge->inc(array_values(array_merge($this->tags, $counterData['tags']))); } elseif ($counterData['value'] < 0) { - $gauge->dec($tagsValues); + $gauge->dec(array_values(array_merge($this->tags, $counterData['tags']))); } } foreach ($this->data['gauges'] as $gaugeData) { - $gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name']); + $gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name'], $gaugeData['tags']); - $gauge->set($gaugeData['value'], $tagsValues); + $gauge->set($gaugeData['value'], array_values(array_merge($this->tags, $gaugeData['tags']))); } $this->data = array('counters' => array(), 'gauges' => array()); @@ -130,9 +132,10 @@ public function setTags($tags) /** * @param string $variable * + * @param array $tags * @return \Prometheus\Gauge */ - private function getOrRegisterGaugeForVariable($variable) + private function getOrRegisterGaugeForVariable($variable, $tags = array()) { try { $gauge = $this->collectorRegistry->getGauge($this->namespace, $variable); @@ -141,7 +144,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/StatsD.php b/src/Beberlei/Metrics/Collector/StatsD.php index f3369ed..e6c92fc 100644 --- a/src/Beberlei/Metrics/Collector/StatsD.php +++ b/src/Beberlei/Metrics/Collector/StatsD.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/TaggableCollector.php b/src/Beberlei/Metrics/Collector/TaggableCollector.php index 6ff2057..17fd969 100644 --- a/src/Beberlei/Metrics/Collector/TaggableCollector.php +++ b/src/Beberlei/Metrics/Collector/TaggableCollector.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; /** @@ -23,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); } /** * @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->tags, $value); + $this->data[] = sprintf('%s%s:%s|s', $variable, $this->buildTagString(array_merge($this->tags, $tags)), $value); } /** @@ -130,4 +131,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($tagString) > 0 ? ','.$tagString : $tagString); + return $tagString; + } } diff --git a/src/Beberlei/Metrics/Collector/Zabbix.php b/src/Beberlei/Metrics/Collector/Zabbix.php index 0380680..214e064 100644 --- a/src/Beberlei/Metrics/Collector/Zabbix.php +++ b/src/Beberlei/Metrics/Collector/Zabbix.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 Net\Zabbix\Sender; diff --git a/src/Beberlei/Metrics/Factory.php b/src/Beberlei/Metrics/Factory.php index 47afdcb..940b44c 100644 --- a/src/Beberlei/Metrics/Factory.php +++ b/src/Beberlei/Metrics/Factory.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; use Net\Zabbix\Sender; diff --git a/src/Beberlei/Metrics/MetricsException.php b/src/Beberlei/Metrics/MetricsException.php index debb904..2100609 100644 --- a/src/Beberlei/Metrics/MetricsException.php +++ b/src/Beberlei/Metrics/MetricsException.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; class MetricsException extends \Exception diff --git a/src/Beberlei/Metrics/Tests/Collector/InMemoryTest.php b/src/Beberlei/Metrics/Tests/Collector/InMemoryTest.php new file mode 100644 index 0000000..ec8f760 --- /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)); + } +} 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..c9b78cb --- 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; @@ -140,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 old mode 100644 new mode 100755 index 7d2aae4..4384a14 --- 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; @@ -96,6 +97,57 @@ 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))) + ; + $gaugeMock2 + ->expects($this->once()) + ->method('set') + ->with($expectedVariableValue2, array_values($globalTags)) + ; + + $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 +201,55 @@ 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->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 +303,55 @@ 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_values(array_merge($globalTags, $inlineTags))) + ; + $gaugeMock2 + ->expects($this->once()) + ->method('dec') + ->with(array_values($globalTags)) + ; + + $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 +408,57 @@ 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->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->timing(self::TEST_VARIABLE_NAME, $expectedVariableValue, $inlineTags); + $this->collector->timing(self::TEST_VARIABLE_NAME.'_2', $expectedVariableValue); + $this->collector->flush(); + } + public function testMeasureWhenSetNewVariableWithTags() { $expectedVariableValue = 123;