Skip to content

Commit

Permalink
Merge pull request endclothing#40 from 1player/redis-prefix
Browse files Browse the repository at this point in the history
Let users change Redis key prefix
  • Loading branch information
bracki authored Mar 15, 2017
2 parents 0378829 + 5bd48e8 commit 1d8c990
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/Prometheus/Storage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

class Redis implements Adapter
{
const PROMETHEUS_PREFIX = 'PROMETHEUS_';
const PROMETHEUS_METRIC_KEYS_SUFFIX = '_METRIC_KEYS';

private static $defaultOptions = array();
private static $prefix = 'PROMETHEUS_';

private $options;
private $redis;
Expand Down Expand Up @@ -51,6 +51,11 @@ public static function setDefaultOptions(array $options)
self::$defaultOptions = array_merge(self::$defaultOptions, $options);
}

public static function setPrefix($prefix)
{
self::$prefix = $prefix;
}

public function flushRedis()
{
$this->openConnection();
Expand Down Expand Up @@ -118,7 +123,7 @@ public function updateHistogram(array $data)
$this->toMetricKey($data),
json_encode(array('b' => 'sum', 'labelValues' => $data['labelValues'])),
json_encode(array('b' => $bucketToIncrease, 'labelValues' => $data['labelValues'])),
self::PROMETHEUS_PREFIX . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
$data['value'],
json_encode($metaData),
),
Expand Down Expand Up @@ -152,7 +157,7 @@ public function updateGauge(array $data)
array(
$this->toMetricKey($data),
$this->getRedisCommand($data['command']),
self::PROMETHEUS_PREFIX . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
json_encode($data['labelValues']),
$data['value'],
json_encode($metaData),
Expand Down Expand Up @@ -180,7 +185,7 @@ public function updateCounter(array $data)
array(
$this->toMetricKey($data),
$this->getRedisCommand($data['command']),
self::PROMETHEUS_PREFIX . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
json_encode($data['labelValues']),
$data['value'],
json_encode($metaData),
Expand All @@ -192,7 +197,7 @@ public function updateCounter(array $data)

private function collectHistograms()
{
$keys = $this->redis->sMembers(self::PROMETHEUS_PREFIX . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
$keys = $this->redis->sMembers(self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
$histograms = array();
foreach ($keys as $key) {
Expand Down Expand Up @@ -266,7 +271,7 @@ private function collectHistograms()

private function collectGauges()
{
$keys = $this->redis->sMembers(self::PROMETHEUS_PREFIX . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
$keys = $this->redis->sMembers(self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
$gauges = array();
foreach ($keys as $key) {
Expand All @@ -292,7 +297,7 @@ private function collectGauges()

private function collectCounters()
{
$keys = $this->redis->sMembers(self::PROMETHEUS_PREFIX . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
$keys = $this->redis->sMembers(self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
$counters = array();
foreach ($keys as $key) {
Expand Down Expand Up @@ -336,7 +341,7 @@ private function getRedisCommand($cmd)
*/
private function toMetricKey(array $data)
{
return implode(':', array(self::PROMETHEUS_PREFIX, $data['type'], $data['name']));
return implode(':', array(self::$prefix, $data['type'], $data['name']));
}

}

0 comments on commit 1d8c990

Please sign in to comment.