From b43f56feccc61e1059eed2bb0978c536417aefec Mon Sep 17 00:00:00 2001 From: Julius Ehrlich Date: Tue, 12 Jun 2018 10:26:10 +0200 Subject: [PATCH 1/6] Delete only the metric data instead of flushing the complete Redis server. --- src/Prometheus/Storage/Redis.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Prometheus/Storage/Redis.php b/src/Prometheus/Storage/Redis.php index f38391b..3008118 100644 --- a/src/Prometheus/Storage/Redis.php +++ b/src/Prometheus/Storage/Redis.php @@ -62,7 +62,23 @@ public static function setPrefix($prefix) public function flushRedis() { $this->openConnection(); - $this->redis->flushAll(); + $this->redis->eval(<< Date: Tue, 12 Jun 2018 10:26:28 +0200 Subject: [PATCH 2/6] Updated to PHPUnit ~5.7 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b941542..2487051 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "symfony/polyfill-apcu": "^1.6" }, "require-dev": { - "phpunit/phpunit": "4.1.0" + "phpunit/phpunit": "~5.7" }, "suggest": { "ext-redis": "Required if using Redis.", From f8945fbaba8867f6a27904cc43ab7bd17912e37d Mon Sep 17 00:00:00 2001 From: Julius Ehrlich Date: Thu, 9 Aug 2018 11:19:40 +0200 Subject: [PATCH 3/6] Made the registry nullable to prevent argument errors --- src/Prometheus/PushGateway.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Prometheus/PushGateway.php b/src/Prometheus/PushGateway.php index a4a3fd2..c788ad8 100644 --- a/src/Prometheus/PushGateway.php +++ b/src/Prometheus/PushGateway.php @@ -55,12 +55,12 @@ public function delete($job, $groupingKey = null) } /** - * @param CollectorRegistry $collectorRegistry + * @param CollectorRegistry|null $collectorRegistry * @param $job * @param $groupingKey * @param $method */ - private function doRequest(CollectorRegistry $collectorRegistry, $job, $groupingKey, $method) + private function doRequest(CollectorRegistry $collectorRegistry = null, $job, $groupingKey, $method) { $url = "http://" . $this->address . "/metrics/job/" . $job; if (!empty($groupingKey)) { From 743122bc349123c5e74aab71b96371b2fff4f1ed Mon Sep 17 00:00:00 2001 From: Julius Ehrlich Date: Thu, 9 Aug 2018 11:20:16 +0200 Subject: [PATCH 4/6] Added a unit test for the Redis flushing to ensure only metric data is being flushed --- .../Redis/CollectorRegistryTest.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/Test/Prometheus/Redis/CollectorRegistryTest.php b/tests/Test/Prometheus/Redis/CollectorRegistryTest.php index 2e69ce9..cd5133f 100644 --- a/tests/Test/Prometheus/Redis/CollectorRegistryTest.php +++ b/tests/Test/Prometheus/Redis/CollectorRegistryTest.php @@ -4,6 +4,8 @@ namespace Test\Prometheus\Redis; use function class_exists; +use Prometheus\CollectorRegistry; +use Prometheus\Counter; use Prometheus\Storage\Redis; use Test\Prometheus\AbstractCollectorRegistryTest; @@ -17,4 +19,28 @@ public function configureAdapter() $this->adapter = new Redis(array('host' => REDIS_HOST)); $this->adapter->flushRedis(); } + + /** + * @test + */ + public function itShouldOnlyFlushMetricData() + { + $redis = new \Redis(); + $redis->connect(REDIS_HOST); + $redis->set('foo', 'bar'); + + $registry = new CollectorRegistry($this->adapter); + $counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']); + $counter->incBy(6, ['blue']); + + $counterRedisKey = 'PROMETHEUS_' . Counter::TYPE . Redis::PROMETHEUS_METRIC_KEYS_SUFFIX; + $this->assertEquals(['PROMETHEUS_:counter:test_some_counter'], $redis->sMembers($counterRedisKey)); + + $this->adapter->flushRedis(); + + $this->assertEquals('bar', $redis->get('foo')); + $this->assertEquals([], $redis->sMembers($counterRedisKey)); + + $redis->del('foo'); + } } From 147d3651bd37c7610885386cf8f09b7dc14ce685 Mon Sep 17 00:00:00 2001 From: Julius Ehrlich Date: Thu, 9 Aug 2018 11:33:10 +0200 Subject: [PATCH 5/6] Test the Redis flush also with gauge and histogram metrics --- .../Redis/CollectorRegistryTest.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/Test/Prometheus/Redis/CollectorRegistryTest.php b/tests/Test/Prometheus/Redis/CollectorRegistryTest.php index cd5133f..860c0bd 100644 --- a/tests/Test/Prometheus/Redis/CollectorRegistryTest.php +++ b/tests/Test/Prometheus/Redis/CollectorRegistryTest.php @@ -6,6 +6,8 @@ use function class_exists; use Prometheus\CollectorRegistry; use Prometheus\Counter; +use Prometheus\Gauge; +use Prometheus\Histogram; use Prometheus\Storage\Redis; use Test\Prometheus\AbstractCollectorRegistryTest; @@ -30,16 +32,31 @@ public function itShouldOnlyFlushMetricData() $redis->set('foo', 'bar'); $registry = new CollectorRegistry($this->adapter); + $counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']); $counter->incBy(6, ['blue']); - $counterRedisKey = 'PROMETHEUS_' . Counter::TYPE . Redis::PROMETHEUS_METRIC_KEYS_SUFFIX; $this->assertEquals(['PROMETHEUS_:counter:test_some_counter'], $redis->sMembers($counterRedisKey)); + $gauge = $registry->registerGauge('test', 'some_gauge', 'this is for testing', array('foo')); + $gauge->set(35, array('bar')); + $gaugeRedisKey = 'PROMETHEUS_' . Gauge::TYPE . Redis::PROMETHEUS_METRIC_KEYS_SUFFIX; + $this->assertEquals(['PROMETHEUS_:gauge:test_some_gauge'], $redis->sMembers($gaugeRedisKey)); + + $histogram = $registry->registerHistogram('test', 'some_histogram', 'this is for testing', array('foo', 'bar'), array(0.1, 1, 5, 10)); + $histogram->observe(2, array('cat', 'meow')); + $histogramRedisKey = 'PROMETHEUS_' . Histogram::TYPE . Redis::PROMETHEUS_METRIC_KEYS_SUFFIX; + $this->assertEquals(['PROMETHEUS_:histogram:test_some_histogram'], $redis->sMembers($histogramRedisKey)); + $this->adapter->flushRedis(); $this->assertEquals('bar', $redis->get('foo')); $this->assertEquals([], $redis->sMembers($counterRedisKey)); + $this->assertFalse($redis->get('PROMETHEUS_:counter:test_some_counter')); + $this->assertEquals([], $redis->sMembers($gaugeRedisKey)); + $this->assertFalse($redis->get('PROMETHEUS_:gauge:test_some_gauge')); + $this->assertEquals([], $redis->sMembers($histogramRedisKey)); + $this->assertFalse($redis->get('PROMETHEUS_:histogram:test_some_histogram')); $redis->del('foo'); } From 160babec3bd8b00ffeebf4cb7fa871aa21a3e233 Mon Sep 17 00:00:00 2001 From: Julius Ehrlich Date: Thu, 9 Aug 2018 11:40:16 +0200 Subject: [PATCH 6/6] Ensure the output of the text renderer is empty when flushing all Redis metrics --- tests/Test/Prometheus/Redis/CollectorRegistryTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Test/Prometheus/Redis/CollectorRegistryTest.php b/tests/Test/Prometheus/Redis/CollectorRegistryTest.php index 860c0bd..e09319b 100644 --- a/tests/Test/Prometheus/Redis/CollectorRegistryTest.php +++ b/tests/Test/Prometheus/Redis/CollectorRegistryTest.php @@ -8,6 +8,7 @@ use Prometheus\Counter; use Prometheus\Gauge; use Prometheus\Histogram; +use Prometheus\RenderTextFormat; use Prometheus\Storage\Redis; use Test\Prometheus\AbstractCollectorRegistryTest; @@ -51,6 +52,7 @@ public function itShouldOnlyFlushMetricData() $this->adapter->flushRedis(); $this->assertEquals('bar', $redis->get('foo')); + $this->assertEquals([], $redis->sMembers($counterRedisKey)); $this->assertFalse($redis->get('PROMETHEUS_:counter:test_some_counter')); $this->assertEquals([], $redis->sMembers($gaugeRedisKey)); @@ -58,6 +60,8 @@ public function itShouldOnlyFlushMetricData() $this->assertEquals([], $redis->sMembers($histogramRedisKey)); $this->assertFalse($redis->get('PROMETHEUS_:histogram:test_some_histogram')); + $this->assertEquals("\n", (new RenderTextFormat())->render($registry->getMetricFamilySamples())); + $redis->del('foo'); } }