Skip to content

Commit

Permalink
simplify label definition like in all other prometheus sdks
Browse files Browse the repository at this point in the history
  • Loading branch information
schnipseljagd committed Jun 21, 2016
1 parent 371e3ab commit 8b362ac
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 90 deletions.
12 changes: 6 additions & 6 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
$redisAdapter = new \Prometheus\RedisAdapter('localhost');
$registry = new \Prometheus\Registry($redisAdapter);
$counter = $registry->registerGauge('test', 'some_gauge', 'it sets', ['type']);
$counter->set(234, ['type' => 'blue']);
$counter->set(123, ['type' => 'red']);
$counter->set(234, ['blue']);
$counter->set(123, ['red']);

$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter->increaseBy(123, ['type' => 'blue']);
$counter->increaseBy(65, ['type' => 'red']);
$counter->increaseBy(123, ['blue']);
$counter->increaseBy(65, ['red']);

$histogram = $registry->registerHistogram('test', 'some_histogram', 'it observes', ['type'], [1, 2, 3.5, 4]);
$histogram->observe(3.1, ['type' => 'blue']);
$histogram->observe(1.1, ['type' => 'red']);
$histogram->observe(3.1, ['blue']);
$histogram->observe(1.1, ['red']);

$registry->flush();
echo $registry->toText();
31 changes: 5 additions & 26 deletions src/Prometheus/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,10 @@
namespace Prometheus;


class Counter
class Counter extends Metric
{
const TYPE = 'counter';

private $namespace;
private $name;
private $help;
private $values = array();
private $labels;

/**
* @param string $namespace
* @param string $name
* @param string $help
* @param array $labels
*/
public function __construct($namespace, $name, $help, $labels = array())
{
$this->namespace = $namespace;
$this->name = $name;
$this->help = $help;
$this->labels = $labels;
}

/**
* @return Sample[]
*/
Expand All @@ -53,7 +33,7 @@ public function getType()
}

/**
* @param array $labels e.g. ['controller' => 'status', 'action' => 'opcode']
* @param array $labels e.g. ['status', 'opcode']
*/
public function increase(array $labels = array())
{
Expand All @@ -62,13 +42,12 @@ public function increase(array $labels = array())

/**
* @param int $count e.g. 2
* @param array $labels e.g. ['controller' => 'status', 'action' => 'opcode']
* @param array $labels e.g. ['status', 'opcode']
*/
public function increaseBy($count, array $labels = array())
{
if (array_keys($labels) != $this->labels) {
throw new \InvalidArgumentException(sprintf('Label %s is not defined.', $labels));
}
$this->assertLabelsAreDefinedCorrectly($labels);

if (!isset($this->values[serialize($labels)])) {
$this->values[serialize($labels)] = 0;
}
Expand Down
32 changes: 5 additions & 27 deletions src/Prometheus/Gauge.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,17 @@
namespace Prometheus;


class Gauge
class Gauge extends Metric
{
const TYPE = 'gauge';

private $namespace;
private $name;
private $help;
private $values = array();
private $labels;

/**
* @param string $namespace
* @param string $name
* @param string $help
* @param array $labels
*/
public function __construct($namespace, $name, $help, $labels = array())
{
$this->namespace = $namespace;
$this->name = $name;
$this->help = $help;
$this->labels = $labels;
}

/**
* @param double $value e.g. 123
* @param array $labels e.g. ['controller' => 'status', 'action' => 'opcode']
* @param array $labels e.g. ['status', 'opcode']
*/
public function set($value, $labels = array())
{
if (array_keys($labels) != $this->labels) {
throw new \InvalidArgumentException(sprintf('Label combination %s is not defined.', print_r($labels, true)));
}
$this->assertLabelsAreDefinedCorrectly($labels);
$this->values[serialize($labels)] = $value;
}

Expand All @@ -47,12 +25,12 @@ public function getSamples()
{
$metrics = array();
foreach ($this->values as $serializedLabels => $value) {
$labels = unserialize($serializedLabels);
$labelValues = unserialize($serializedLabels);
$metrics[] = new Sample(
array(
'name' => $this->getFullName(),
'labelNames' => $this->getLabelNames(),
'labelValues' => array_values($labels),
'labelValues' => $labelValues,
'value' => $value
)
);
Expand Down
22 changes: 6 additions & 16 deletions src/Prometheus/Histogram.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
namespace Prometheus;


class Histogram
class Histogram extends Metric
{
const TYPE = 'histogram';

private $namespace;
private $name;
private $help;
private $values = array();
private $labels;
private $buckets;

/**
Expand All @@ -23,10 +18,7 @@ class Histogram
*/
public function __construct($namespace, $name, $help, $labels = array(), $buckets = array())
{
$this->namespace = $namespace;
$this->name = $name;
$this->help = $help;
$this->labels = $labels;
parent::__construct($namespace, $name, $help, $labels);

if (0 == count($buckets)) {
throw new \InvalidArgumentException("Histogram must have at least one bucket.");
Expand All @@ -49,13 +41,12 @@ public function __construct($namespace, $name, $help, $labels = array(), $bucket

/**
* @param double $value e.g. 123
* @param array $labels e.g. ['controller' => 'status', 'action' => 'opcode']
* @param array $labels e.g. ['status', 'opcode']
*/
public function observe($value, $labels = array())
{
if (array_keys($labels) != $this->labels) {
throw new \InvalidArgumentException(sprintf('Label %s is not defined.', $labels));
}
$this->assertLabelsAreDefinedCorrectly($labels);

if (!isset($this->values[serialize($labels)])) {
$this->values[serialize($labels)] = array(
'sum' => 0,
Expand Down Expand Up @@ -83,8 +74,7 @@ public function getSamples()
{
$samples = array();
foreach ($this->values as $serializedLabels => $value) {
$labels = unserialize($serializedLabels);
$labelValues = array_values($labels);
$labelValues = unserialize($serializedLabels);
foreach ($value['buckets'] as $bucket => $bucketCounter) {
$samples[] = new Sample(
array(
Expand Down
30 changes: 30 additions & 0 deletions src/Prometheus/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@

class Metric
{
protected $labels;
protected $namespace;
protected $name;
protected $help;
protected $values = array();

/**
* @param string $namespace
* @param string $name
* @param string $help
* @param array $labels
*/
public function __construct($namespace, $name, $help, $labels = array())
{
$this->namespace = $namespace;
$this->name = $name;
$this->help = $help;
$this->labels = $labels;
}

public static function metricName($namespace, $name)
{
return ($namespace ? $namespace . '_' : '') . $name;
Expand All @@ -17,4 +37,14 @@ public static function metricIdentifier($namespace, $name, $labels)
}
return self::metricName($namespace, $name) . '_' . implode('_', $labels);
}

/**
* @param $labels
*/
protected function assertLabelsAreDefinedCorrectly($labels)
{
if (count($labels) != count($this->labels)) {
throw new \InvalidArgumentException(sprintf('Labels are not defined correctly: ', print_r($labels, true)));
}
}
}
10 changes: 5 additions & 5 deletions tests/Test/Prometheus/CounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class CounterTest extends PHPUnit_Framework_TestCase
public function itShouldIncreaseWithLabels()
{
$gauge = new Counter('test', 'some_metric', 'this is for testing', array('foo', 'bar'));
$gauge->increase(array('foo' => 'lalal', 'bar' => 'lululu'));
$gauge->increase(array('foo' => 'lalal', 'bar' => 'lululu'));
$gauge->increase(array('foo' => 'lalal', 'bar' => 'lululu'));
$gauge->increase(array('lalal', 'lululu'));
$gauge->increase(array('lalal', 'lululu'));
$gauge->increase(array('lalal', 'lululu'));
$this->assertThat(
$gauge->getSamples(),
$this->equalTo(
Expand Down Expand Up @@ -68,8 +68,8 @@ public function itShouldIncreaseWithoutLabelWhenNoLabelsAreDefined()
public function itShouldIncreaseTheCounterByAnArbitraryInteger()
{
$gauge = new Counter('test', 'some_metric', 'this is for testing', array('foo', 'bar'));
$gauge->increase(array('foo' => 'lalal', 'bar' => 'lululu'));
$gauge->increaseBy(123, array('foo' => 'lalal', 'bar' => 'lululu'));
$gauge->increase(array('lalal', 'lululu'));
$gauge->increaseBy(123, array('lalal', 'lululu'));
$this->assertThat(
$gauge->getSamples(),
$this->equalTo(
Expand Down
2 changes: 1 addition & 1 deletion tests/Test/Prometheus/GaugeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GaugeTest extends PHPUnit_Framework_TestCase
public function itShouldAllowSetWithLabels()
{
$gauge = new Gauge('test', 'some_metric', 'this is for testing', array('foo', 'bar'));
$gauge->set(123, array('foo' => 'lalal', 'bar' => 'lululu'));
$gauge->set(123, array('lalal', 'lululu'));
$this->assertThat(
$gauge->getSamples(),
$this->equalTo(
Expand Down
4 changes: 2 additions & 2 deletions tests/Test/Prometheus/HistogramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class HistogramTest extends PHPUnit_Framework_TestCase
public function itShouldObserveWithLabels()
{
$gauge = new Histogram('test', 'some_metric', 'this is for testing', array('foo', 'bar'), array(100, 200, 300));
$gauge->observe(123, array('foo' => 'lalal', 'bar' => 'lululu'));
$gauge->observe(245, array('foo' => 'lalal', 'bar' => 'lululu'));
$gauge->observe(123, array('lalal', 'lululu'));
$gauge->observe(245, array('lalal', 'lululu'));
$this->assertThat(
$gauge->getSamples(),
$this->equalTo(
Expand Down
14 changes: 7 additions & 7 deletions tests/Test/Prometheus/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function itShouldSaveGaugesInRedis()
{
$client = new Registry($this->newRedisAdapter());
$metric = $client->registerGauge('test', 'some_metric', 'this is for testing', array('foo', 'bar'));
$metric->set(14, array('foo' => 'lalal', 'bar' => 'lululu'));
$client->getGauge('test', 'some_metric', array('foo', 'bar'))->set(34, array('foo' => 'lalal', 'bar' => 'lululu'));
$metric->set(14, array('lalal', 'lululu'));
$client->getGauge('test', 'some_metric', array('foo', 'bar'))->set(34, array('lalal', 'lululu'));

$g = $client->registerGauge('test', 'some_metric', 'this is for testing', array('foo'));
$g->set(32, array('foo' => 'lalal'));
$g->set(35, array('foo' => 'lalab'));
$g->set(32, array('lalal'));
$g->set(35, array('lalab'));

$client->flush();

Expand Down Expand Up @@ -79,9 +79,9 @@ public function itShouldSaveHistogramsInRedis()
{
$client = new Registry($this->newRedisAdapter());
$metric = $client->registerHistogram('test', 'some_metric', 'this is for testing', array('foo', 'bar'), array(0.1, 1, 5, 10));
$metric->observe(2, array('foo' => 'lalal', 'bar' => 'lululu'));
$client->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(13, array('foo' => 'lalal', 'bar' => 'lululu'));
$client->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(7.1, array('foo' => 'lalal', 'bar' => 'lululu'));
$metric->observe(2, array('lalal', 'lululu'));
$client->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(13, array('lalal', 'lululu'));
$client->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(7.1, array('lalal', 'lululu'));
$client->flush();

$client = new Registry($this->newRedisAdapter());
Expand Down

0 comments on commit 8b362ac

Please sign in to comment.