Skip to content

Commit

Permalink
cleanup registry a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
schnipseljagd committed Jun 22, 2016
1 parent b9afb88 commit f81f264
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
27 changes: 26 additions & 1 deletion src/Prometheus/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
*/
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 2 additions & 9 deletions src/Prometheus/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ public function flush()
$this->counters,
$this->histograms
);
foreach ($metrics as $metric) {
$this->redisAdapter->storeMetric($metric);
}
$this->redisAdapter->storeMetrics($metrics);
}

/**
Expand All @@ -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());
}

/**
Expand Down

0 comments on commit f81f264

Please sign in to comment.