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.", 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)) { 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(<<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)); + + $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')); + + $this->assertEquals("\n", (new RenderTextFormat())->render($registry->getMetricFamilySamples())); + + $redis->del('foo'); + } }