From f81f264b1e798e36e801dfbae1e3fa33a8f2e843 Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 22 Jun 2016 18:20:14 +0200 Subject: [PATCH] cleanup registry a bit --- src/Prometheus/RedisAdapter.php | 27 ++++++++++++++++++++++++++- src/Prometheus/Registry.php | 11 ++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/Prometheus/RedisAdapter.php b/src/Prometheus/RedisAdapter.php index 84f83bb..1267673 100644 --- a/src/Prometheus/RedisAdapter.php +++ b/src/Prometheus/RedisAdapter.php @@ -18,6 +18,12 @@ class RedisAdapter const PROMETHEUS_SAMPLE_LABEL_VALUES_SUFFIX = '_LABEL_VALUES'; const PROMETHEUS_SAMPLE_NAME_SUFFIX = '_NAME'; + const METRIC_TYPES = [ + Gauge::TYPE, + Counter::TYPE, + Histogram::TYPE, + ]; + private $hostname; private $redis; @@ -33,6 +39,25 @@ public function flushRedis() $this->redis->flushAll(); } + /** + * @param Metric[] $metrics + */ + public function storeMetrics($metrics) + { + foreach ($metrics as $metric) { + $this->storeMetric($metric); + } + } + + public function fetchMetrics() + { + $metrics = array(); + foreach (self::METRIC_TYPES as $metricType) { + $metrics = array_merge($metrics, $this->fetchMetricsByType($metricType)); + } + return $metrics; + } + /** * @param Metric $metric */ @@ -89,7 +114,7 @@ public function storeMetric(Metric $metric) * @param string $metricType * @return MetricResponse[] */ - public function fetchMetrics($metricType) + public function fetchMetricsByType($metricType) { $this->openConnection(); $keys = $this->redis->zRange( diff --git a/src/Prometheus/Registry.php b/src/Prometheus/Registry.php index 57e481c..f78fc21 100644 --- a/src/Prometheus/Registry.php +++ b/src/Prometheus/Registry.php @@ -61,9 +61,7 @@ public function flush() $this->counters, $this->histograms ); - foreach ($metrics as $metric) { - $this->redisAdapter->storeMetric($metric); - } + $this->redisAdapter->storeMetrics($metrics); } /** @@ -72,12 +70,7 @@ public function flush() public function toText() { $renderer = new RenderTextFormat(); - $metrics = array_merge( - $this->redisAdapter->fetchMetrics(Gauge::TYPE), - $this->redisAdapter->fetchMetrics(Counter::TYPE), - $this->redisAdapter->fetchMetrics(Histogram::TYPE) - ); - return $renderer->render($metrics); + return $renderer->render($this->redisAdapter->fetchMetrics()); } /**