Skip to content

Commit

Permalink
remove redundancy when storing metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
schnipseljagd committed Jun 22, 2016
1 parent a64b727 commit 000a783
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 114 deletions.
5 changes: 5 additions & 0 deletions src/Prometheus/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static function metricIdentifier($namespace, $name, $labels)

public abstract function getType();

/**
* @return Sample[]
*/
public abstract function getSamples();

public function getFullName()
{
return Metric::metricName($this->namespace, $this->name);
Expand Down
174 changes: 60 additions & 114 deletions src/Prometheus/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RedisAdapter
const PROMETHEUS_GAUGE_KEYS = 'PROMETHEUS_GAUGE_KEYS';
const PROMETHEUS_COUNTERS = 'PROMETHEUS_COUNTERS_';
const PROMETHEUS_COUNTER_KEYS = 'PROMETHEUS_COUNTER_KEYS';
const PROMETHEUS_HISTOGRAMS_KEYS = 'PROMETHEUS_HISTOGRAM_KEYS';
const PROMETHEUS_HISTOGRAM_KEYS = 'PROMETHEUS_HISTOGRAM_KEYS';
const PROMETHEUS_HISTOGRAMS = 'PROMETHEUS_HISTOGRAMS_';
const PROMETHEUS_METRICS_COUNTER = 'PROMETHEUS_METRICS_COUNTER';

Expand All @@ -28,142 +28,40 @@ public function __construct($hostname)
$this->redis = new \Redis();
}

public function storeGauge(Gauge $gauge)
public function flushRedis()
{
$this->openConnection();
$key = sha1($gauge->getFullName() . '_' . implode('_', $gauge->getLabelNames()));
$sampleKeys = array();
foreach ($gauge->getSamples() as $sample) {
$sampleKey = $sample->getKey();
$this->redis->hSet(
self::PROMETHEUS_GAUGES . $key . self::PROMETHEUS_SAMPLE_VALUE_SUFFIX,
$sampleKey,
$sample->getValue()
);
$this->redis->hSet(
self::PROMETHEUS_GAUGES . $key . self::PROMETHEUS_SAMPLE_LABEL_VALUES_SUFFIX,
$sampleKey,
serialize($sample->getLabelValues())
);
$this->redis->hSet(
self::PROMETHEUS_GAUGES . $key . self::PROMETHEUS_SAMPLE_LABEL_NAMES_SUFFIX,
$sampleKey,
serialize($sample->getLabelNames())
);
$this->redis->hSet(
self::PROMETHEUS_GAUGES . $key . self::PROMETHEUS_SAMPLE_NAME_SUFFIX,
$sampleKey,
$sample->getName()
);
$sampleKeys[] = $sampleKey;
}
$this->redis->hSet(self::PROMETHEUS_GAUGES . $key, 'name', $gauge->getFullName());
$this->redis->hSet(self::PROMETHEUS_GAUGES . $key, 'help', $gauge->getHelp());
$this->redis->hSet(self::PROMETHEUS_GAUGES . $key, 'type', $gauge->getType());
$this->redis->hSet(self::PROMETHEUS_GAUGES . $key, 'labelNames', serialize($gauge->getLabelNames()));
$this->redis->hSet(self::PROMETHEUS_GAUGES . $key, 'sampleKeys', serialize($sampleKeys));
$this->storeNewMetricKey(self::PROMETHEUS_GAUGE_KEYS, $key);
}

public function fetchGauges()
{
return $this->fetchMetricsByType(self::PROMETHEUS_GAUGE_KEYS, self::PROMETHEUS_GAUGES);
$this->redis->flushAll();
}

public function flushRedis()
public function storeGauge(Gauge $gauge)
{
$this->openConnection();

$this->redis->flushAll();
$this->storeMetricByType($gauge, 'hSet', self::PROMETHEUS_GAUGE_KEYS, self::PROMETHEUS_GAUGES);
}

private function openConnection()
public function fetchGauges()
{
$this->redis->connect($this->hostname);
return $this->fetchMetricsByType(self::PROMETHEUS_GAUGE_KEYS, self::PROMETHEUS_GAUGES);
}

public function storeCounter(Counter $counter)
{
$this->openConnection();
$key = sha1($counter->getFullName() . '_' . implode('_', $counter->getLabelNames()));
$sampleKeys = array();
foreach ($counter->getSamples() as $sample) {
$sampleKey = $sample->getKey();
$this->redis->hIncrBy(
self::PROMETHEUS_COUNTERS . $key . self::PROMETHEUS_SAMPLE_VALUE_SUFFIX,
$sampleKey,
$sample->getValue()
);
$this->redis->hSet(
self::PROMETHEUS_COUNTERS . $key . self::PROMETHEUS_SAMPLE_LABEL_VALUES_SUFFIX,
$sampleKey,
serialize($sample->getLabelValues())
);
$this->redis->hSet(
self::PROMETHEUS_COUNTERS . $key . self::PROMETHEUS_SAMPLE_LABEL_NAMES_SUFFIX,
$sampleKey,
serialize($sample->getLabelNames())
);
$this->redis->hSet(
self::PROMETHEUS_COUNTERS . $key . self::PROMETHEUS_SAMPLE_NAME_SUFFIX,
$sampleKey,
$sample->getName()
);
$sampleKeys[] = $sampleKey;
}
$this->redis->hSet(self::PROMETHEUS_COUNTERS . $key, 'name', $counter->getFullName());
$this->redis->hSet(self::PROMETHEUS_COUNTERS . $key, 'help', $counter->getHelp());
$this->redis->hSet(self::PROMETHEUS_COUNTERS . $key, 'type', $counter->getType());
$this->redis->hSet(self::PROMETHEUS_COUNTERS . $key, 'labelNames', serialize($counter->getLabelNames()));
$this->redis->hSet(self::PROMETHEUS_COUNTERS . $key, 'sampleKeys', serialize($sampleKeys));
$this->storeNewMetricKey(self::PROMETHEUS_COUNTER_KEYS, $key);
$this->storeMetricByType($counter, 'hIncrBy', self::PROMETHEUS_COUNTER_KEYS, self::PROMETHEUS_COUNTERS);
}

public function fetchCounters()
{
return $this->fetchMetricsByType(self::PROMETHEUS_COUNTER_KEYS, self::PROMETHEUS_COUNTERS);
}

public function fetchHistograms()
public function storeHistogram(Histogram $histogram)
{
return $this->fetchMetricsByType(self::PROMETHEUS_HISTOGRAMS_KEYS, self::PROMETHEUS_HISTOGRAMS);
$this->storeMetricByType($histogram, 'hIncrByFloat', self::PROMETHEUS_HISTOGRAM_KEYS, self::PROMETHEUS_HISTOGRAMS);
}

public function storeHistogram(Histogram $histogram)
public function fetchHistograms()
{
$this->openConnection();
$key = sha1($histogram->getFullName() . '_' . implode('_', $histogram->getLabelNames()));
$sampleKeys = array();
foreach ($histogram->getSamples() as $sample) {
$sampleKey = $sample->getKey();
$this->redis->hIncrByFloat(
self::PROMETHEUS_HISTOGRAMS . $key . self::PROMETHEUS_SAMPLE_VALUE_SUFFIX,
$sampleKey,
$sample->getValue()
);
$this->redis->hSet(
self::PROMETHEUS_HISTOGRAMS . $key . self::PROMETHEUS_SAMPLE_LABEL_VALUES_SUFFIX,
$sampleKey,
serialize($sample->getLabelValues())
);
$this->redis->hSet(
self::PROMETHEUS_HISTOGRAMS . $key . self::PROMETHEUS_SAMPLE_LABEL_NAMES_SUFFIX,
$sampleKey,
serialize($sample->getLabelNames())
);
$this->redis->hSet(
self::PROMETHEUS_HISTOGRAMS . $key . self::PROMETHEUS_SAMPLE_NAME_SUFFIX,
$sampleKey,
$sample->getName()
);
$sampleKeys[] = $sampleKey;
}
$this->redis->hSet(self::PROMETHEUS_HISTOGRAMS . $key, 'name', $histogram->getFullName());
$this->redis->hSet(self::PROMETHEUS_HISTOGRAMS . $key, 'help', $histogram->getHelp());
$this->redis->hSet(self::PROMETHEUS_HISTOGRAMS . $key, 'type', $histogram->getType());
$this->redis->hSet(self::PROMETHEUS_HISTOGRAMS . $key, 'labelNames', serialize($histogram->getLabelNames()));
$this->redis->hSet(self::PROMETHEUS_HISTOGRAMS . $key, 'sampleKeys', serialize($sampleKeys));
$this->storeNewMetricKey(self::PROMETHEUS_HISTOGRAMS_KEYS, $key);
return $this->fetchMetricsByType(self::PROMETHEUS_HISTOGRAM_KEYS, self::PROMETHEUS_HISTOGRAMS);
}

/**
Expand Down Expand Up @@ -204,6 +102,49 @@ private function fetchMetricsByType($typeKeysPrefix, $typePrefix)
return array_reverse($metrics);
}

/**
* @param Metric $metric
* @param string $storeValueCommand
* @param string $typeKeysPrefix
* @param string $typePrefix
*/
private function storeMetricByType(Metric $metric, $storeValueCommand, $typeKeysPrefix, $typePrefix)
{
$this->openConnection();
$key = sha1($metric->getFullName() . '_' . implode('_', $metric->getLabelNames()));
$sampleKeys = array();
foreach ($metric->getSamples() as $sample) {
$sampleKey = $sample->getKey();
$this->redis->$storeValueCommand(
$typePrefix . $key . self::PROMETHEUS_SAMPLE_VALUE_SUFFIX,
$sampleKey,
$sample->getValue()
);
$this->redis->hSet(
$typePrefix . $key . self::PROMETHEUS_SAMPLE_LABEL_VALUES_SUFFIX,
$sampleKey,
serialize($sample->getLabelValues())
);
$this->redis->hSet(
$typePrefix . $key . self::PROMETHEUS_SAMPLE_LABEL_NAMES_SUFFIX,
$sampleKey,
serialize($sample->getLabelNames())
);
$this->redis->hSet(
$typePrefix . $key . self::PROMETHEUS_SAMPLE_NAME_SUFFIX,
$sampleKey,
$sample->getName()
);
$sampleKeys[] = $sampleKey;
}
$this->redis->hSet($typePrefix . $key, 'name', $metric->getFullName());
$this->redis->hSet($typePrefix . $key, 'help', $metric->getHelp());
$this->redis->hSet($typePrefix . $key, 'type', $metric->getType());
$this->redis->hSet($typePrefix . $key, 'labelNames', serialize($metric->getLabelNames()));
$this->redis->hSet($typePrefix . $key, 'sampleKeys', serialize($sampleKeys));
$this->storeNewMetricKey($typeKeysPrefix, $key);
}

/**
* @param string $typePrefix
* @param string $key
Expand All @@ -213,4 +154,9 @@ private function storeNewMetricKey($typePrefix, $key)
$currentMetricCounter = $this->redis->incr(self::PROMETHEUS_METRICS_COUNTER);
$this->redis->zAdd($typePrefix, $currentMetricCounter, $key);
}

private function openConnection()
{
$this->redis->connect($this->hostname);
}
}

0 comments on commit 000a783

Please sign in to comment.