diff --git a/.travis.yml b/.travis.yml index 1d9e00f..1680e7d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,19 @@ language: php php: - 5.6 - - 5.3 services: - redis-server before_script: + - echo yes | pecl install apcu-4.0.11 + - echo "apc.enabled = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini + - echo "apc.enable_cli = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - composer self-update - composer install --no-interaction --prefer-source --dev - phpenv rehash + - php -i script: - vendor/bin/phpunit --verbose --colors diff --git a/README.md b/README.md index 53e9bff..d5e1ff0 100644 --- a/README.md +++ b/README.md @@ -4,20 +4,15 @@ [![Code Climate](https://codeclimate.com/github/Jimdo/prometheus_client_php.png)](https://codeclimate.com/github/Jimdo/prometheus_client_php) -This library uses Redis to do the client side aggregation. -We recommend to run a local Redis instance next to your PHP workers. +This library uses Redis or APCu to do the client side aggregation. +If using Redis, we recommend to run a local Redis instance next to your PHP workers. -## Why Redis? +## How does it work? Usually PHP worker processes don't share any state. - -We decided to use Redis because: - * It is easy to deploy as a sidecar to the PHP worker processes (see [docker-compose.yml](docker-compose.yml)). - * It provides us with easy to use concurrency mechanisms we need for the metric aggregation (e.g. `incrByFloat`). - -We think this could be implemented with APCu as well and we might do so in the future. -Of course we would also appreciate a pull-request. - +You can pick from two adapters. +One uses Redis the other APC. +While the former needs a separate binary running, the latter just needs the [APC](https://pecl.php.net/package/APCU) extension to be installed. ## Usage @@ -73,8 +68,9 @@ Also look at the [examples](examples). ### Dependencies -* PHP 5.3/5.6 (at least these versions are tested at the moment) +* PHP 5.6 * PHP Redis extension +* PHP APCu extension * [Composer](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx) * Redis @@ -98,5 +94,10 @@ Just start the nginx, fpm & Redis setup with docker-compose: ``` composer require guzzlehttp/guzzle=~6.0 docker-compose up -vendor/bin/phpunit tests/Test/BlackBoxTest.php +``` +Pick the adapter you want to test. + +``` +ADAPTER=redis vendor/bin/phpunit tests/Test/BlackBoxTest.php +ADAPTER=apc vendor/bin/phpunit tests/Test/BlackBoxTest.php ``` diff --git a/composer.json b/composer.json index 27f2a60..121d587 100644 --- a/composer.json +++ b/composer.json @@ -4,17 +4,22 @@ { "name": "Joscha", "email": "joscha@schnipseljagd.org" + }, + { + "name": "Jan Brauer", + "email": "jan.brauer@jimdo.com" } ], "require": { - "php": ">=5.3.3", - "ext-redis": "*" + "php": ">=5.6.3" }, "require-dev": { "phpunit/phpunit": "4.1.0" }, "suggest": { - "guzzlehttp/guzzle": "~6.0 for running the blackbox tests." + "guzzlehttp/guzzle": "~6.0 for running the blackbox tests.", + "ext-redis": "Required if using Redis.", + "ext-apc": "Required if using APCu." }, "autoload": { "psr-0": { diff --git a/examples/flush_adapter.php b/examples/flush_adapter.php new file mode 100644 index 0000000..5c58091 --- /dev/null +++ b/examples/flush_adapter.php @@ -0,0 +1,16 @@ + REDIS_HOST)); + $redisAdapter->flushRedis(); +} + +if ($adapter == 'apc') { + $apcAdapter = new Prometheus\Storage\APC(); + $apcAdapter->flushAPC(); +} diff --git a/examples/flush_redis.php b/examples/flush_redis.php deleted file mode 100644 index f33dde4..0000000 --- a/examples/flush_redis.php +++ /dev/null @@ -1,7 +0,0 @@ - REDIS_HOST)); -$redisAdapter->flushRedis(); diff --git a/examples/metrics.php b/examples/metrics.php index 7ed5fc9..79c0f92 100644 --- a/examples/metrics.php +++ b/examples/metrics.php @@ -6,8 +6,16 @@ use Prometheus\RenderTextFormat; use Prometheus\Storage\Redis; -Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1')); -$registry = CollectorRegistry::getDefault(); +$adapter = $_GET['adapter']; + +if ($adapter == 'redis') { + Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1')); + $adapter = new Prometheus\Storage\Redis(); +} +if ($adapter == 'apc') { + $adapter = new Prometheus\Storage\APC(); +} +$registry = new CollectorRegistry($adapter); $renderer = new RenderTextFormat(); $result = $renderer->render($registry->getMetricFamilySamples()); diff --git a/examples/some_counter.php b/examples/some_counter.php index 5851449..738ff22 100644 --- a/examples/some_counter.php +++ b/examples/some_counter.php @@ -7,8 +7,16 @@ error_log('c='. $_GET['c']); -Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1')); -$registry = CollectorRegistry::getDefault(); +$adapter = $_GET['adapter']; + +if ($adapter == 'redis') { + Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1')); + $adapter = new Prometheus\Storage\Redis(); +} +if ($adapter == 'apc') { + $adapter = new Prometheus\Storage\APC(); +} +$registry = new CollectorRegistry($adapter); $counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']); $counter->incBy($_GET['c'], ['blue']); diff --git a/examples/some_gauge.php b/examples/some_gauge.php index 14f1553..65aecc6 100644 --- a/examples/some_gauge.php +++ b/examples/some_gauge.php @@ -8,8 +8,16 @@ error_log('c='. $_GET['c']); -Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1')); -$registry = CollectorRegistry::getDefault(); +$adapter = $_GET['adapter']; + +if ($adapter == 'redis') { + Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1')); + $adapter = new Prometheus\Storage\Redis(); +} +if ($adapter == 'apc') { + $adapter = new Prometheus\Storage\APC(); +} +$registry = new CollectorRegistry($adapter); $gauge = $registry->registerGauge('test', 'some_gauge', 'it sets', ['type']); $gauge->set($_GET['c'], ['blue']); diff --git a/examples/some_histogram.php b/examples/some_histogram.php index ff2a60a..684f9f1 100644 --- a/examples/some_histogram.php +++ b/examples/some_histogram.php @@ -7,8 +7,16 @@ error_log('c='. $_GET['c']); -Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1')); -$registry = CollectorRegistry::getDefault(); +$adapter = $_GET['adapter']; + +if ($adapter == 'redis') { + Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1')); + $adapter = new Prometheus\Storage\Redis(); +} +if ($adapter == 'apc') { + $adapter = new Prometheus\Storage\APC(); +} +$registry = new CollectorRegistry($adapter); $histogram = $registry->registerHistogram('test', 'some_histogram', 'it observes', ['type'], [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9]); $histogram->observe($_GET['c'], ['blue']); diff --git a/php-fpm/Dockerfile b/php-fpm/Dockerfile index 71fe82c..0fbe56c 100644 --- a/php-fpm/Dockerfile +++ b/php-fpm/Dockerfile @@ -1,5 +1,6 @@ FROM php:5.6-fpm RUN pecl install redis-2.2.8 && docker-php-ext-enable redis +RUN pecl install apcu-4.0.11 && docker-php-ext-enable apcu COPY www.conf /usr/local/etc/php-fpm.d/ diff --git a/src/Prometheus/Storage/APC.php b/src/Prometheus/Storage/APC.php new file mode 100644 index 0000000..b5a9c4b --- /dev/null +++ b/src/Prometheus/Storage/APC.php @@ -0,0 +1,292 @@ +collectHistograms(); + $metrics = array_merge($metrics, $this->collectGauges()); + $metrics = array_merge($metrics, $this->collectCounters()); + return $metrics; + } + + public function updateHistogram(array $data) + { + // Initialize the sum + $sumKey = $this->histogramBucketValueKey($data, 'sum'); + $new = apc_add($sumKey, $this->toInteger(0)); + + // If sum does not exist, assume a new histogram and store the metadata + if ($new) { + apc_store($this->metaKey($data), json_encode($this->metaData($data))); + } + + // Atomically increment the sum + // Taken from https://github.com/prometheus/client_golang/blob/66058aac3a83021948e5fb12f1f408ff556b9037/prometheus/value.go#L91 + $done = false; + while (!$done) { + $old = apc_fetch($sumKey); + $done = apc_cas($sumKey, $old, $this->toInteger($this->fromInteger($old) + $data['value'])); + } + + // Figure out in which bucket the observation belongs + $bucketToIncrease = '+Inf'; + foreach ($data['buckets'] as $bucket) { + if ($data['value'] <= $bucket) { + $bucketToIncrease = $bucket; + break; + } + } + + // Initialize and increment the bucket + apc_add($this->histogramBucketValueKey($data, $bucketToIncrease), 0); + apc_inc($this->histogramBucketValueKey($data, $bucketToIncrease)); + } + + public function updateGauge(array $data) + { + $valueKey = $this->valueKey($data); + if ($data['command'] == Adapter::COMMAND_SET) { + apc_store($valueKey, $this->toInteger($data['value'])); + apc_store($this->metaKey($data), json_encode($this->metaData($data))); + } else { + $new = apc_add($valueKey, $this->toInteger(0)); + if ($new) { + apc_store($this->metaKey($data), json_encode($this->metaData($data))); + } + // Taken from https://github.com/prometheus/client_golang/blob/66058aac3a83021948e5fb12f1f408ff556b9037/prometheus/value.go#L91 + $done = false; + while (!$done) { + $old = apc_fetch($valueKey); + $done = apc_cas($valueKey, $old, $this->toInteger($this->fromInteger($old) + $data['value'])); + } + } + } + + public function updateCounter(array $data) + { + $new = apc_add($this->valueKey($data), 0); + if ($new) { + apc_store($this->metaKey($data), json_encode($this->metaData($data))); + } + apc_inc($this->valueKey($data), $data['value']); + } + + public function flushAPC() + { + apc_clear_cache('user'); + } + + /** + * @param array $data + * @return string + */ + private function metaKey(array $data) + { + return implode(':', array(self::PROMETHEUS_PREFIX, $data['type'], $data['name'], 'meta')); + } + + /** + * @param array $data + * @return string + */ + private function valueKey(array $data) + { + return implode(':', array(self::PROMETHEUS_PREFIX, $data['type'], $data['name'], json_encode($data['labelValues']), 'value')); + } + + /** + * @param array $data + * @return string + */ + private function histogramBucketValueKey(array $data, $bucket) + { + return implode(':', array(self::PROMETHEUS_PREFIX, $data['type'], $data['name'], json_encode($data['labelValues']), $bucket, 'value')); + } + + /** + * @param array $data + * @return array + */ + private function metaData(array $data) + { + $metricsMetaData = $data; + unset($metricsMetaData['value']); + unset($metricsMetaData['command']); + unset($metricsMetaData['labelValues']); + return $metricsMetaData; + } + + /** + * @return array + */ + private function collectCounters() + { + $counters = array(); + foreach (new \APCIterator('user', '/^prom:counter:.*:meta/') as $counter) { + $metaData = json_decode($counter['value'], true); + $data = array( + 'name' => $metaData['name'], + 'help' => $metaData['help'], + 'type' => $metaData['type'], + 'labelNames' => $metaData['labelNames'], + ); + foreach (new \APCIterator('user', '/^prom:counter:' . $metaData['name'] . ':.*:value/') as $value) { + $parts = explode(':', $value['key']); + $labelValues = $parts[3]; + $data['samples'][] = array( + 'name' => $metaData['name'], + 'labelNames' => array(), + 'labelValues' => json_decode($labelValues), + 'value' => $value['value'] + ); + } + $this->sortSamples($data['samples']); + $counters[] = new MetricFamilySamples($data); + } + return $counters; + } + + /** + * @return array + */ + private function collectGauges() + { + $gauges = array(); + foreach (new \APCIterator('user', '/^prom:gauge:.*:meta/') as $gauge) { + $metaData = json_decode($gauge['value'], true); + $data = array( + 'name' => $metaData['name'], + 'help' => $metaData['help'], + 'type' => $metaData['type'], + 'labelNames' => $metaData['labelNames'], + ); + foreach (new \APCIterator('user', '/^prom:gauge:' . $metaData['name'] . ':.*:value/') as $value) { + $parts = explode(':', $value['key']); + $labelValues = $parts[3]; + $data['samples'][] = array( + 'name' => $metaData['name'], + 'labelNames' => array(), + 'labelValues' => json_decode($labelValues), + 'value' => $this->fromInteger($value['value']) + ); + } + + $this->sortSamples($data['samples']); + $gauges[] = new MetricFamilySamples($data); + } + return $gauges; + } + + /** + * @return array + */ + private function collectHistograms() + { + $histograms = array(); + foreach (new \APCIterator('user', '/^prom:histogram:.*:meta/') as $histogram) { + $metaData = json_decode($histogram['value'], true); + $data = array( + 'name' => $metaData['name'], + 'help' => $metaData['help'], + 'type' => $metaData['type'], + 'labelNames' => $metaData['labelNames'], + 'buckets' => $metaData['buckets'] + ); + + // Add the Inf bucket so we can compute it later on + $data['buckets'][] = '+Inf'; + + $histogramBuckets = array(); + foreach (new \APCIterator('user', '/^prom:histogram:' . $metaData['name'] . ':.*:value/') as $value) { + $parts = explode(':', $value['key']); + $labelValues = $parts[3]; + $bucket = $parts[4]; + // Key by labelValues + $histogramBuckets[$labelValues][$bucket] = $value['value']; + } + + // Compute all buckets + $labels = array_keys($histogramBuckets); + sort($labels); + foreach ($labels as $labelValues) { + $acc = 0; + $decodedLabelValues = json_decode($labelValues); + foreach ($data['buckets'] as $bucket) { + $bucket = (string) $bucket; + if (!isset($histogramBuckets[$labelValues][$bucket])) { + $data['samples'][] = array( + 'name' => $metaData['name'] . '_bucket', + 'labelNames' => array('le'), + 'labelValues' => array_merge($decodedLabelValues, array($bucket)), + 'value' => $acc + ); + } else { + $acc += $histogramBuckets[$labelValues][$bucket]; + $data['samples'][] = array( + 'name' => $metaData['name'] . '_' . 'bucket', + 'labelNames' => array('le'), + 'labelValues' => array_merge($decodedLabelValues, array($bucket)), + 'value' => $acc + ); + } + } + + // Add the count + $data['samples'][] = array( + 'name' => $metaData['name'] . '_count', + 'labelNames' => array(), + 'labelValues' => $decodedLabelValues, + 'value' => $acc + ); + + // Add the sum + $data['samples'][] = array( + 'name' => $metaData['name'] . '_sum', + 'labelNames' => array(), + 'labelValues' => $decodedLabelValues, + 'value' => $this->fromInteger($histogramBuckets[$labelValues]['sum']) + ); + + } + $histograms[] = new MetricFamilySamples($data); + } + return $histograms; + } + + /** + * @param mixed $val + * @return int + */ + private function toInteger($val) + { + return unpack('Q', pack('d', $val))[1]; + } + + /** + * @param mixed $val + * @return int + */ + private function fromInteger($val) + { + return unpack('d', pack('Q', $val))[1]; + } + + private function sortSamples(array &$samples) + { + usort($samples, function($a, $b){ + return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues'])); + }); + } +} diff --git a/tests/Test/BlackBoxTest.php b/tests/Test/BlackBoxTest.php index d6da60f..daff9dd 100644 --- a/tests/Test/BlackBoxTest.php +++ b/tests/Test/BlackBoxTest.php @@ -12,10 +12,16 @@ class BlackBoxTest extends PHPUnit_Framework_TestCase */ private $client; + /** + * @var string + */ + private $adapter; + public function setUp() { - $this->client = new Client(['base_uri' => 'http://localhost:8080/']); - $this->client->get('/examples/flush_redis.php'); + $this->adapter = getenv('ADAPTER'); + $this->client = new Client(['base_uri' => 'http://192.168.59.100:8080/']); + $this->client->get('/examples/flush_adapter.php?adapter=' . $this->adapter); } /** @@ -25,9 +31,9 @@ public function gaugesShouldBeOverwritten() { $start = microtime(true); $promises = [ - $this->client->getAsync('/examples/some_gauge.php?c=0'), - $this->client->getAsync('/examples/some_gauge.php?c=1'), - $this->client->getAsync('/examples/some_gauge.php?c=2'), + $this->client->getAsync('/examples/some_gauge.php?c=0&adapter=' . $this->adapter), + $this->client->getAsync('/examples/some_gauge.php?c=1&adapter=' . $this->adapter), + $this->client->getAsync('/examples/some_gauge.php?c=2&adapter=' . $this->adapter), ]; @@ -35,7 +41,7 @@ public function gaugesShouldBeOverwritten() $end = microtime(true); echo "\ntime: " . ($end - $start) . "\n"; - $metricsResult = $this->client->get('/examples/metrics.php'); + $metricsResult = $this->client->get('/examples/metrics.php?adapter=' . $this->adapter); $body = (string)$metricsResult->getBody(); echo "\nbody: " . $body . "\n"; $this->assertThat( @@ -57,7 +63,7 @@ public function countersShouldIncrementAtomically() $promises = []; $sum = 0; for ($i = 0; $i < 1100; $i++) { - $promises[] = $this->client->getAsync('/examples/some_counter.php?c=' . $i); + $promises[] = $this->client->getAsync('/examples/some_counter.php?c=' . $i . '&adapter=' . $this->adapter); $sum += $i; } @@ -65,7 +71,7 @@ public function countersShouldIncrementAtomically() $end = microtime(true); echo "\ntime: " . ($end - $start) . "\n"; - $metricsResult = $this->client->get('/examples/metrics.php'); + $metricsResult = $this->client->get('/examples/metrics.php?adapter=' . $this->adapter); $body = (string)$metricsResult->getBody(); $this->assertThat($body, $this->stringContains('test_some_counter{type="blue"} ' . $sum)); @@ -78,23 +84,23 @@ public function histogramsShouldIncrementAtomically() { $start = microtime(true); $promises = [ - $this->client->getAsync('/examples/some_histogram.php?c=0'), - $this->client->getAsync('/examples/some_histogram.php?c=1'), - $this->client->getAsync('/examples/some_histogram.php?c=2'), - $this->client->getAsync('/examples/some_histogram.php?c=3'), - $this->client->getAsync('/examples/some_histogram.php?c=4'), - $this->client->getAsync('/examples/some_histogram.php?c=5'), - $this->client->getAsync('/examples/some_histogram.php?c=6'), - $this->client->getAsync('/examples/some_histogram.php?c=7'), - $this->client->getAsync('/examples/some_histogram.php?c=8'), - $this->client->getAsync('/examples/some_histogram.php?c=9'), + $this->client->getAsync('/examples/some_histogram.php?c=0&adapter=' . $this->adapter), + $this->client->getAsync('/examples/some_histogram.php?c=1&adapter=' . $this->adapter), + $this->client->getAsync('/examples/some_histogram.php?c=2&adapter=' . $this->adapter), + $this->client->getAsync('/examples/some_histogram.php?c=3&adapter=' . $this->adapter), + $this->client->getAsync('/examples/some_histogram.php?c=4&adapter=' . $this->adapter), + $this->client->getAsync('/examples/some_histogram.php?c=5&adapter=' . $this->adapter), + $this->client->getAsync('/examples/some_histogram.php?c=6&adapter=' . $this->adapter), + $this->client->getAsync('/examples/some_histogram.php?c=7&adapter=' . $this->adapter), + $this->client->getAsync('/examples/some_histogram.php?c=8&adapter=' . $this->adapter), + $this->client->getAsync('/examples/some_histogram.php?c=9&adapter=' . $this->adapter), ]; Promise\settle($promises)->wait(); $end = microtime(true); echo "\ntime: " . ($end - $start) . "\n"; - $metricsResult = $this->client->get('/examples/metrics.php'); + $metricsResult = $this->client->get('/examples/metrics.php?adapter=' . $this->adapter); $body = (string)$metricsResult->getBody(); $this->assertThat($body, $this->stringContains(<<adapter = new APC(); + $this->adapter->flushAPC(); + } +} diff --git a/tests/Test/Prometheus/APC/CounterTest.php b/tests/Test/Prometheus/APC/CounterTest.php new file mode 100644 index 0000000..b8b5c94 --- /dev/null +++ b/tests/Test/Prometheus/APC/CounterTest.php @@ -0,0 +1,20 @@ +adapter = new APC(); + $this->adapter->flushAPC(); + } +} diff --git a/tests/Test/Prometheus/APC/GaugeTest.php b/tests/Test/Prometheus/APC/GaugeTest.php new file mode 100644 index 0000000..18a9dd9 --- /dev/null +++ b/tests/Test/Prometheus/APC/GaugeTest.php @@ -0,0 +1,20 @@ +adapter = new APC(); + $this->adapter->flushAPC(); + } +} diff --git a/tests/Test/Prometheus/APC/HistogramTest.php b/tests/Test/Prometheus/APC/HistogramTest.php new file mode 100644 index 0000000..71651f7 --- /dev/null +++ b/tests/Test/Prometheus/APC/HistogramTest.php @@ -0,0 +1,21 @@ +adapter = new APC(); + $this->adapter->flushAPC(); + } +} + diff --git a/tests/Test/Prometheus/CollectorRegistryTest.php b/tests/Test/Prometheus/AbstractCollectorRegistryTest.php similarity index 82% rename from tests/Test/Prometheus/CollectorRegistryTest.php rename to tests/Test/Prometheus/AbstractCollectorRegistryTest.php index ba25704..5cfdf82 100644 --- a/tests/Test/Prometheus/CollectorRegistryTest.php +++ b/tests/Test/Prometheus/AbstractCollectorRegistryTest.php @@ -9,11 +9,16 @@ use Prometheus\Exception\MetricsRegistrationException; use Prometheus\Histogram; use Prometheus\RenderTextFormat; +use Prometheus\Storage\Adapter; use Prometheus\Storage\Redis; -class CollectorRegistryTest extends PHPUnit_Framework_TestCase +abstract class AbstractCollectorRegistryTest extends PHPUnit_Framework_TestCase { - private $redisAdapter; + /** + * @var Adapter + */ + public $adapter; + /** * @var RenderTextFormat */ @@ -21,31 +26,34 @@ class CollectorRegistryTest extends PHPUnit_Framework_TestCase public function setUp() { - $this->redisAdapter = $this->newRedisAdapter(); - $this->redisAdapter->flushRedis(); + $this->configureAdapter(); $this->renderer = new RenderTextFormat(); } /** * @test */ - public function itShouldSaveGaugesInRedis() + public function itShouldSaveGauges() { - $registry = new CollectorRegistry($this->redisAdapter); + $registry = new CollectorRegistry($this->adapter); $g = $registry->registerGauge('test', 'some_metric', 'this is for testing', array('foo')); - $g->set(32, array('lalal')); - $g->set(35, array('lalab')); + $g->set(35, array('bbb')); + $g->set(35, array('ddd')); + $g->set(35, array('aaa')); + $g->set(35, array('ccc')); - $registry = new CollectorRegistry($this->newRedisAdapter()); + $registry = new CollectorRegistry($this->adapter); $this->assertThat( $this->renderer->render($registry->getMetricFamilySamples()), $this->equalTo(<<newRedisAdapter()); + $registry = new CollectorRegistry($this->adapter); $metric = $registry->registerCounter('test', 'some_metric', 'this is for testing', array('foo', 'bar')); $metric->incBy(2, array('lalal', 'lululu')); $registry->getCounter('test', 'some_metric', array('foo', 'bar'))->inc(array('lalal', 'lululu')); $registry->getCounter('test', 'some_metric', array('foo', 'bar'))->inc(array('lalal', 'lvlvlv')); - $registry = new CollectorRegistry($this->newRedisAdapter()); + $registry = new CollectorRegistry($this->adapter); $this->assertThat( $this->renderer->render($registry->getMetricFamilySamples()), $this->equalTo(<<redisAdapter); + $registry = new CollectorRegistry($this->adapter); $metric = $registry->registerHistogram('test', 'some_metric', 'this is for testing', array('foo', 'bar'), array(0.1, 1, 5, 10)); $metric->observe(2, array('lalal', 'lululu')); $registry->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(7.1, array('lalal', 'lvlvlv')); @@ -90,7 +98,7 @@ public function itShouldSaveHistogramsInRedis() $registry->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(7.1, array('lalal', 'lululu')); $registry->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(7.1, array('gnaaha', 'hihihi')); - $registry = new CollectorRegistry($this->redisAdapter); + $registry = new CollectorRegistry($this->adapter); $this->assertThat( $this->renderer->render($registry->getMetricFamilySamples()), $this->equalTo(<<redisAdapter); + $registry = new CollectorRegistry($this->adapter); $metric = $registry->registerHistogram('test', 'some_metric', 'this is for testing'); $metric->observe(2); $registry->getHistogram('test', 'some_metric')->observe(13); $registry->getHistogram('test', 'some_metric')->observe(7.1); - $registry = new CollectorRegistry($this->redisAdapter); + $registry = new CollectorRegistry($this->adapter); $this->assertThat( $this->renderer->render($registry->getMetricFamilySamples()), $this->equalTo(<<newRedisAdapter()); + $registry = new CollectorRegistry( $this->adapter); $registry ->registerCounter('', 'some_quick_counter', 'just a quick measurement') ->inc(); @@ -191,7 +199,7 @@ public function itShouldIncreaseACounterWithoutNamespace() */ public function itShouldForbidRegisteringTheSameCounterTwice() { - $registry = new CollectorRegistry($this->newRedisAdapter()); + $registry = new CollectorRegistry( $this->adapter); $registry->registerCounter('foo', 'metric', 'help'); $registry->registerCounter('foo', 'metric', 'help'); } @@ -202,7 +210,7 @@ public function itShouldForbidRegisteringTheSameCounterTwice() */ public function itShouldForbidRegisteringTheSameCounterWithDifferentLabels() { - $registry = new CollectorRegistry($this->newRedisAdapter()); + $registry = new CollectorRegistry( $this->adapter); $registry->registerCounter('foo', 'metric', 'help', array("foo", "bar")); $registry->registerCounter('foo', 'metric', 'help', array("spam", "eggs")); } @@ -213,7 +221,7 @@ public function itShouldForbidRegisteringTheSameCounterWithDifferentLabels() */ public function itShouldForbidRegisteringTheSameHistogramTwice() { - $registry = new CollectorRegistry($this->newRedisAdapter()); + $registry = new CollectorRegistry( $this->adapter); $registry->registerHistogram('foo', 'metric', 'help'); $registry->registerHistogram('foo', 'metric', 'help'); } @@ -224,7 +232,7 @@ public function itShouldForbidRegisteringTheSameHistogramTwice() */ public function itShouldForbidRegisteringTheSameHistogramWithDifferentLabels() { - $registry = new CollectorRegistry($this->newRedisAdapter()); + $registry = new CollectorRegistry( $this->adapter); $registry->registerCounter('foo', 'metric', 'help', array("foo", "bar")); $registry->registerCounter('foo', 'metric', 'help', array("spam", "eggs")); } @@ -235,7 +243,7 @@ public function itShouldForbidRegisteringTheSameHistogramWithDifferentLabels() */ public function itShouldForbidRegisteringTheSameGaugeTwice() { - $registry = new CollectorRegistry($this->newRedisAdapter()); + $registry = new CollectorRegistry( $this->adapter); $registry->registerGauge('foo', 'metric', 'help'); $registry->registerGauge('foo', 'metric', 'help'); } @@ -246,7 +254,7 @@ public function itShouldForbidRegisteringTheSameGaugeTwice() */ public function itShouldForbidRegisteringTheSameGaugeWithDifferentLabels() { - $registry = new CollectorRegistry($this->newRedisAdapter()); + $registry = new CollectorRegistry( $this->adapter); $registry->registerGauge('foo', 'metric', 'help', array("foo", "bar")); $registry->registerGauge('foo', 'metric', 'help', array("spam", "eggs")); } @@ -257,12 +265,9 @@ public function itShouldForbidRegisteringTheSameGaugeWithDifferentLabels() */ public function itShouldThrowAnExceptionWhenGettingANonExistentMetric() { - $registry = new CollectorRegistry($this->newRedisAdapter()); + $registry = new CollectorRegistry( $this->adapter); $registry->getGauge("not_here", "go_away"); } - private function newRedisAdapter() - { - return new Redis(array('host' => REDIS_HOST)); - } + public abstract function configureAdapter(); } diff --git a/tests/Test/Prometheus/CounterTest.php b/tests/Test/Prometheus/AbstractCounterTest.php similarity index 84% rename from tests/Test/Prometheus/CounterTest.php rename to tests/Test/Prometheus/AbstractCounterTest.php index 2273077..92501f5 100644 --- a/tests/Test/Prometheus/CounterTest.php +++ b/tests/Test/Prometheus/AbstractCounterTest.php @@ -6,24 +6,20 @@ use PHPUnit_Framework_TestCase; use Prometheus\Counter; use Prometheus\MetricFamilySamples; -use Prometheus\Sample; -use Prometheus\Storage\InMemory; -use Prometheus\Storage\Redis; /** * See https://prometheus.io/docs/instrumenting/exposition_formats/ */ -class CounterTest extends PHPUnit_Framework_TestCase +abstract class AbstractCounterTest extends PHPUnit_Framework_TestCase { /** - * @var Redis + * @var Adapter */ - private $storage; + public $adapter; public function setUp() { - $this->storage = new Redis(array('host' => REDIS_HOST)); - $this->storage->flushRedis(); + $this->configureAdapter(); } /** @@ -31,12 +27,12 @@ public function setUp() */ public function itShouldIncreaseWithLabels() { - $gauge = new Counter($this->storage, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); + $gauge = new Counter($this->adapter, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); $gauge->inc(array('lalal', 'lululu')); $gauge->inc(array('lalal', 'lululu')); $gauge->inc(array('lalal', 'lululu')); $this->assertThat( - $this->storage->collect(), + $this->adapter->collect(), $this->equalTo( array( new MetricFamilySamples( @@ -65,10 +61,10 @@ public function itShouldIncreaseWithLabels() */ public function itShouldIncreaseWithoutLabelWhenNoLabelsAreDefined() { - $gauge = new Counter($this->storage, 'test', 'some_metric', 'this is for testing'); + $gauge = new Counter($this->adapter, 'test', 'some_metric', 'this is for testing'); $gauge->inc(); $this->assertThat( - $this->storage->collect(), + $this->adapter->collect(), $this->equalTo( array( new MetricFamilySamples( @@ -97,11 +93,11 @@ public function itShouldIncreaseWithoutLabelWhenNoLabelsAreDefined() */ public function itShouldIncreaseTheCounterByAnArbitraryInteger() { - $gauge = new Counter($this->storage, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); + $gauge = new Counter($this->adapter, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); $gauge->inc(array('lalal', 'lululu')); $gauge->incBy(123, array('lalal', 'lululu')); $this->assertThat( - $this->storage->collect(), + $this->adapter->collect(), $this->equalTo( array( new MetricFamilySamples( @@ -131,7 +127,7 @@ public function itShouldIncreaseTheCounterByAnArbitraryInteger() */ public function itShouldRejectInvalidMetricsNames() { - new Counter($this->storage, 'test', 'some metric invalid metric', 'help'); + new Counter($this->adapter, 'test', 'some metric invalid metric', 'help'); } /** @@ -140,6 +136,8 @@ public function itShouldRejectInvalidMetricsNames() */ public function itShouldRejectInvalidLabelNames() { - new Counter($this->storage, 'test', 'some_metric', 'help', array('invalid label')); + new Counter($this->adapter, 'test', 'some_metric', 'help', array('invalid label')); } + + public abstract function configureAdapter(); } diff --git a/tests/Test/Prometheus/AbstractGaugeTest.php b/tests/Test/Prometheus/AbstractGaugeTest.php new file mode 100644 index 0000000..175f0b5 --- /dev/null +++ b/tests/Test/Prometheus/AbstractGaugeTest.php @@ -0,0 +1,312 @@ +configureAdapter(); + } + + /** + * @test + */ + public function itShouldAllowSetWithLabels() + { + $gauge = new Gauge($this->adapter, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); + $gauge->set(123, array('lalal', 'lululu')); + $this->assertThat( + $this->adapter->collect(), + $this->equalTo( + array( + new MetricFamilySamples( + array( + 'name' => 'test_some_metric', + 'help' => 'this is for testing', + 'type' => Gauge::TYPE, + 'labelNames' => array('foo', 'bar'), + 'samples' => array( + array( + 'name' => 'test_some_metric', + 'labelNames' => array(), + 'labelValues' => array('lalal', 'lululu'), + 'value' => 123, + ) + ) + ) + ) + ) + ) + ); + $this->assertThat($gauge->getHelp(), $this->equalTo('this is for testing')); + $this->assertThat($gauge->getType(), $this->equalTo(Gauge::TYPE)); + } + + /** + * @test + */ + public function itShouldAllowSetWithoutLabelWhenNoLabelsAreDefined() + { + $gauge = new Gauge($this->adapter, 'test', 'some_metric', 'this is for testing'); + $gauge->set(123); + $this->assertThat( + $this->adapter->collect(), + $this->equalTo( + array( + new MetricFamilySamples( + array( + 'name' => 'test_some_metric', + 'help' => 'this is for testing', + 'type' => Gauge::TYPE, + 'labelNames' => array(), + 'samples' => array( + array( + 'name' => 'test_some_metric', + 'labelNames' => array(), + 'labelValues' => array(), + 'value' => 123, + ) + ) + ) + ) + ) + ) + ); + $this->assertThat($gauge->getHelp(), $this->equalTo('this is for testing')); + $this->assertThat($gauge->getType(), $this->equalTo(Gauge::TYPE)); + } + + /** + * @test + */ + public function itShouldAllowSetWithAFloatValue() + { + $gauge = new Gauge($this->adapter, 'test', 'some_metric', 'this is for testing'); + $gauge->set(123.5); + $this->assertThat( + $this->adapter->collect(), + $this->equalTo( + array( + new MetricFamilySamples( + array( + 'name' => 'test_some_metric', + 'help' => 'this is for testing', + 'type' => Gauge::TYPE, + 'labelNames' => array(), + 'samples' => array( + array( + 'name' => 'test_some_metric', + 'labelNames' => array(), + 'labelValues' => array(), + 'value' => 123.5, + ) + ) + ) + ) + ) + ) + ); + $this->assertThat($gauge->getHelp(), $this->equalTo('this is for testing')); + $this->assertThat($gauge->getType(), $this->equalTo(Gauge::TYPE)); + } + + /** + * @test + */ + public function itShouldIncrementAValue() + { + $gauge = new Gauge($this->adapter, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); + $gauge->inc(array('lalal', 'lululu')); + $gauge->incBy(123, array('lalal', 'lululu')); + $this->assertThat( + $this->adapter->collect(), + $this->equalTo( + array( + new MetricFamilySamples( + array( + 'name' => 'test_some_metric', + 'help' => 'this is for testing', + 'type' => Gauge::TYPE, + 'labelNames' => array('foo', 'bar'), + 'samples' => array( + array( + 'name' => 'test_some_metric', + 'labelNames' => array(), + 'labelValues' => array('lalal', 'lululu'), + 'value' => 124, + ) + ) + ) + ) + ) + ) + ); + } + + /** + * @test + */ + public function itShouldIncrementWithFloatValue() + { + $gauge = new Gauge($this->adapter, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); + $gauge->inc(array('lalal', 'lululu')); + $gauge->incBy(123.5, array('lalal', 'lululu')); + $this->assertThat( + $this->adapter->collect(), + $this->equalTo( + array( + new MetricFamilySamples( + array( + 'name' => 'test_some_metric', + 'help' => 'this is for testing', + 'type' => Gauge::TYPE, + 'labelNames' => array('foo', 'bar'), + 'samples' => array( + array( + 'name' => 'test_some_metric', + 'labelNames' => array(), + 'labelValues' => array('lalal', 'lululu'), + 'value' => 124.5, + ) + ) + ) + ) + ) + ) + ); + } + + /** + * @test + */ + public function itShouldDecrementAValue() + { + $gauge = new Gauge($this->adapter, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); + $gauge->dec(array('lalal', 'lululu')); + $gauge->decBy(123, array('lalal', 'lululu')); + $this->assertThat( + $this->adapter->collect(), + $this->equalTo( + array( + new MetricFamilySamples( + array( + 'name' => 'test_some_metric', + 'help' => 'this is for testing', + 'type' => Gauge::TYPE, + 'labelNames' => array('foo', 'bar'), + 'samples' => array( + array( + 'name' => 'test_some_metric', + 'labelNames' => array(), + 'labelValues' => array('lalal', 'lululu'), + 'value' => -124, + ) + ) + ) + ) + ) + ) + ); + } + + /** + * @test + */ + public function itShouldDecrementWithFloatValue() + { + $gauge = new Gauge($this->adapter, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); + $gauge->dec(array('lalal', 'lululu')); + $gauge->decBy(123, array('lalal', 'lululu')); + $this->assertThat( + $this->adapter->collect(), + $this->equalTo( + array( + new MetricFamilySamples( + array( + 'name' => 'test_some_metric', + 'help' => 'this is for testing', + 'type' => Gauge::TYPE, + 'labelNames' => array('foo', 'bar'), + 'samples' => array( + array( + 'name' => 'test_some_metric', + 'labelNames' => array(), + 'labelValues' => array('lalal', 'lululu'), + 'value' => -124, + ) + ) + ) + ) + ) + ) + ); + } + + /** + * @test + */ + public function itShouldOverwriteWhenSettingTwice() + { + $gauge = new Gauge($this->adapter, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); + $gauge->set(123, array('lalal', 'lululu')); + $gauge->set(321, array('lalal', 'lululu')); + $this->assertThat( + $this->adapter->collect(), + $this->equalTo( + array( + new MetricFamilySamples( + array( + 'name' => 'test_some_metric', + 'help' => 'this is for testing', + 'type' => Gauge::TYPE, + 'labelNames' => array('foo', 'bar'), + 'samples' => array( + array( + 'name' => 'test_some_metric', + 'labelNames' => array(), + 'labelValues' => array('lalal', 'lululu'), + 'value' => 321, + ) + ) + ) + ) + ) + ) + ); + } + + /** + * @test + * @expectedException \InvalidArgumentException + */ + public function itShouldRejectInvalidMetricsNames() + { + new Gauge($this->adapter, 'test', 'some metric invalid metric', 'help'); + } + + /** + * @test + * @expectedException \InvalidArgumentException + */ + public function itShouldRejectInvalidLabelNames() + { + new Gauge($this->adapter, 'test', 'some_metric', 'help', array('invalid label')); + } + + public abstract function configureAdapter(); +} diff --git a/tests/Test/Prometheus/HistogramTest.php b/tests/Test/Prometheus/AbstractHistogramTest.php similarity index 95% rename from tests/Test/Prometheus/HistogramTest.php rename to tests/Test/Prometheus/AbstractHistogramTest.php index 7c3f998..e83ff39 100644 --- a/tests/Test/Prometheus/HistogramTest.php +++ b/tests/Test/Prometheus/AbstractHistogramTest.php @@ -6,23 +6,22 @@ use PHPUnit_Framework_TestCase; use Prometheus\Histogram; use Prometheus\MetricFamilySamples; -use Prometheus\Storage\InMemory; -use Prometheus\Storage\Redis; +use Prometheus\Storage\Adapter; + /** * See https://prometheus.io/docs/instrumenting/exposition_formats/ */ -class HistogramTest extends PHPUnit_Framework_TestCase +abstract class AbstractHistogramTest extends PHPUnit_Framework_TestCase { /** - * @var Redis + * @var Adapter */ - private $storage; + public $adapter; public function setUp() { - $this->storage = new Redis(array('host' => REDIS_HOST)); - $this->storage->flushRedis(); + $this->configureAdapter(); } /** @@ -31,7 +30,7 @@ public function setUp() public function itShouldObserveWithLabels() { $histogram = new Histogram( - $this->storage, + $this->adapter, 'test', 'some_metric', 'this is for testing', @@ -41,7 +40,7 @@ public function itShouldObserveWithLabels() $histogram->observe(123, array('lalal', 'lululu')); $histogram->observe(245, array('lalal', 'lululu')); $this->assertThat( - $this->storage->collect(), + $this->adapter->collect(), $this->equalTo( array( new MetricFamilySamples( @@ -101,7 +100,7 @@ public function itShouldObserveWithLabels() public function itShouldObserveWithoutLabelWhenNoLabelsAreDefined() { $histogram = new Histogram( - $this->storage, + $this->adapter, 'test', 'some_metric', 'this is for testing', @@ -110,7 +109,7 @@ public function itShouldObserveWithoutLabelWhenNoLabelsAreDefined() ); $histogram->observe(245); $this->assertThat( - $this->storage->collect(), + $this->adapter->collect(), $this->equalTo( array( new MetricFamilySamples( @@ -170,7 +169,7 @@ public function itShouldObserveWithoutLabelWhenNoLabelsAreDefined() public function itShouldObserveValuesOfTypeDouble() { $histogram = new Histogram( - $this->storage, + $this->adapter, 'test', 'some_metric', 'this is for testing', @@ -180,7 +179,7 @@ public function itShouldObserveValuesOfTypeDouble() $histogram->observe(0.11); $histogram->observe(0.3); $this->assertThat( - $this->storage->collect(), + $this->adapter->collect(), $this->equalTo( array( new MetricFamilySamples( @@ -242,7 +241,7 @@ public function itShouldProvideDefaultBuckets() // .005, .01, .025, .05, .075, .1, .25, .5, .75, 1.0, 2.5, 5.0, 7.5, 10.0 $histogram = new Histogram( - $this->storage, + $this->adapter, 'test', 'some_metric', 'this is for testing', @@ -252,7 +251,7 @@ public function itShouldProvideDefaultBuckets() $histogram->observe(0.11); $histogram->observe(0.03); $this->assertThat( - $this->storage->collect(), + $this->adapter->collect(), $this->equalTo( array( new MetricFamilySamples( @@ -378,7 +377,7 @@ public function itShouldProvideDefaultBuckets() */ public function itShouldThrowAnExceptionWhenTheBucketSizesAreNotIncreasing() { - new Histogram($this->storage, 'test', 'some_metric', 'this is for testing', array(), array(1, 1)); + new Histogram($this->adapter, 'test', 'some_metric', 'this is for testing', array(), array(1, 1)); } /** @@ -387,7 +386,7 @@ public function itShouldThrowAnExceptionWhenTheBucketSizesAreNotIncreasing() */ public function itShouldThrowAnExceptionWhenThereIsLessThanOneBucket() { - new Histogram($this->storage, 'test', 'some_metric', 'this is for testing', array(), array()); + new Histogram($this->adapter, 'test', 'some_metric', 'this is for testing', array(), array()); } /** @@ -396,7 +395,7 @@ public function itShouldThrowAnExceptionWhenThereIsLessThanOneBucket() */ public function itShouldThrowAnExceptionWhenThereIsALabelNamedLe() { - new Histogram($this->storage, 'test', 'some_metric', 'this is for testing', array('le'), array()); + new Histogram($this->adapter, 'test', 'some_metric', 'this is for testing', array('le'), array()); } /** @@ -405,7 +404,7 @@ public function itShouldThrowAnExceptionWhenThereIsALabelNamedLe() */ public function itShouldRejectInvalidMetricsNames() { - new Histogram($this->storage, 'test', 'some invalid metric', 'help', array(), array(1)); + new Histogram($this->adapter, 'test', 'some invalid metric', 'help', array(), array(1)); } /** @@ -414,6 +413,8 @@ public function itShouldRejectInvalidMetricsNames() */ public function itShouldRejectInvalidLabelNames() { - new Histogram($this->storage, 'test', 'some_metric', 'help', array('invalid label'), array(1)); + new Histogram($this->adapter, 'test', 'some_metric', 'help', array('invalid label'), array(1)); } + + public abstract function configureAdapter(); } diff --git a/tests/Test/Prometheus/GaugeTest.php b/tests/Test/Prometheus/GaugeTest.php deleted file mode 100644 index bbe4cc1..0000000 --- a/tests/Test/Prometheus/GaugeTest.php +++ /dev/null @@ -1,180 +0,0 @@ -storage = new Redis(array('host' => REDIS_HOST)); - $this->storage->flushRedis(); - } - - /** - * @test - */ - public function itShouldAllowSetWithLabels() - { - $gauge = new Gauge($this->storage, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); - $gauge->set(123, array('lalal', 'lululu')); - $this->assertThat( - $this->storage->collect(), - $this->equalTo( - array( - new MetricFamilySamples( - array( - 'name' => 'test_some_metric', - 'help' => 'this is for testing', - 'type' => Gauge::TYPE, - 'labelNames' => array('foo', 'bar'), - 'samples' => array( - array( - 'name' => 'test_some_metric', - 'labelNames' => array(), - 'labelValues' => array('lalal', 'lululu'), - 'value' => 123, - ) - ) - ) - ) - ) - ) - ); - $this->assertThat($gauge->getHelp(), $this->equalTo('this is for testing')); - $this->assertThat($gauge->getType(), $this->equalTo(Gauge::TYPE)); - } - - /** - * @test - */ - public function itShouldAllowSetWithoutLabelWhenNoLabelsAreDefined() - { - $gauge = new Gauge($this->storage, 'test', 'some_metric', 'this is for testing'); - $gauge->set(123); - $this->assertThat( - $this->storage->collect(), - $this->equalTo( - array( - new MetricFamilySamples( - array( - 'name' => 'test_some_metric', - 'help' => 'this is for testing', - 'type' => Gauge::TYPE, - 'labelNames' => array(), - 'samples' => array( - array( - 'name' => 'test_some_metric', - 'labelNames' => array(), - 'labelValues' => array(), - 'value' => 123, - ) - ) - ) - ) - ) - ) - ); - $this->assertThat($gauge->getHelp(), $this->equalTo('this is for testing')); - $this->assertThat($gauge->getType(), $this->equalTo(Gauge::TYPE)); - } - - /** - * @test - */ - public function itShouldIncrementAValue() - { - $gauge = new Gauge($this->storage, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); - $gauge->inc(array('lalal', 'lululu')); - $gauge->incBy(123, array('lalal', 'lululu')); - $this->assertThat( - $this->storage->collect(), - $this->equalTo( - array( - new MetricFamilySamples( - array( - 'name' => 'test_some_metric', - 'help' => 'this is for testing', - 'type' => Gauge::TYPE, - 'labelNames' => array('foo', 'bar'), - 'samples' => array( - array( - 'name' => 'test_some_metric', - 'labelNames' => array(), - 'labelValues' => array('lalal', 'lululu'), - 'value' => 124, - ) - ) - ) - ) - ) - ) - ); - } - - /** - * @test - */ - public function itShouldDecrementAValue() - { - $gauge = new Gauge($this->storage, 'test', 'some_metric', 'this is for testing', array('foo', 'bar')); - $gauge->dec(array('lalal', 'lululu')); - $gauge->decBy(123, array('lalal', 'lululu')); - $this->assertThat( - $this->storage->collect(), - $this->equalTo( - array( - new MetricFamilySamples( - array( - 'name' => 'test_some_metric', - 'help' => 'this is for testing', - 'type' => Gauge::TYPE, - 'labelNames' => array('foo', 'bar'), - 'samples' => array( - array( - 'name' => 'test_some_metric', - 'labelNames' => array(), - 'labelValues' => array('lalal', 'lululu'), - 'value' => -124, - ) - ) - ) - ) - ) - ) - ); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function itShouldRejectInvalidMetricsNames() - { - new Gauge($this->storage, 'test', 'some metric invalid metric', 'help'); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function itShouldRejectInvalidLabelNames() - { - new Gauge($this->storage, 'test', 'some_metric', 'help', array('invalid label')); - } -} diff --git a/tests/Test/Prometheus/Redis/CollectorRegistryTest.php b/tests/Test/Prometheus/Redis/CollectorRegistryTest.php new file mode 100644 index 0000000..ed2ad2b --- /dev/null +++ b/tests/Test/Prometheus/Redis/CollectorRegistryTest.php @@ -0,0 +1,16 @@ +adapter = new Redis(array('host' => REDIS_HOST)); + $this->adapter->flushRedis(); + } +} diff --git a/tests/Test/Prometheus/Redis/CounterTest.php b/tests/Test/Prometheus/Redis/CounterTest.php new file mode 100644 index 0000000..0d78272 --- /dev/null +++ b/tests/Test/Prometheus/Redis/CounterTest.php @@ -0,0 +1,20 @@ +adapter = new Redis(array('host' => REDIS_HOST)); + $this->adapter->flushRedis(); + } +} diff --git a/tests/Test/Prometheus/Redis/GaugeTest.php b/tests/Test/Prometheus/Redis/GaugeTest.php new file mode 100644 index 0000000..17d0c44 --- /dev/null +++ b/tests/Test/Prometheus/Redis/GaugeTest.php @@ -0,0 +1,20 @@ +adapter = new Redis(array('host' => REDIS_HOST)); + $this->adapter->flushRedis(); + } +} diff --git a/tests/Test/Prometheus/Redis/HistogramTest.php b/tests/Test/Prometheus/Redis/HistogramTest.php new file mode 100644 index 0000000..57f73f6 --- /dev/null +++ b/tests/Test/Prometheus/Redis/HistogramTest.php @@ -0,0 +1,20 @@ +adapter = new Redis(array('host' => REDIS_HOST)); + $this->adapter->flushRedis(); + } +}