Skip to content

Commit

Permalink
Introduce a value object to hold the metric name
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSuisse committed May 25, 2019
1 parent db024b9 commit 773cecd
Show file tree
Hide file tree
Showing 26 changed files with 374 additions and 307 deletions.
3 changes: 2 additions & 1 deletion examples/pushgateway.php
Expand Up @@ -9,6 +9,7 @@
use Prometheus\PushGateway\PSR18Pusher;
use Prometheus\Registry\CollectorRegistry;
use Prometheus\Storage\RedisStore;
use Prometheus\Value\MetricName;

$adapter = $_GET['adapter'] ?? '';

Expand All @@ -24,7 +25,7 @@

$registry = new CollectorRegistry($adapter);

$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter = $registry->registerCounter(MetricName::fromNamespacedName('test', 'some_counter'), 'it increases', ['type']);
$counter->incBy(6, ['blue']);

$pusher = new PSR18Pusher('192.168.59.100:9091', Psr18ClientDiscovery::find(), Psr17FactoryDiscovery::findRequestFactory(), Psr17FactoryDiscovery::findStreamFactory());
Expand Down
3 changes: 2 additions & 1 deletion examples/some_counter.php
Expand Up @@ -5,6 +5,7 @@
require __DIR__ . '/../vendor/autoload.php';

use Prometheus\Registry\CollectorRegistry;
use Prometheus\Value\MetricName;

$adapter = $_GET['adapter'];

Expand All @@ -19,7 +20,7 @@
}
$registry = new CollectorRegistry($adapter);

$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter = $registry->registerCounter(MetricName::fromNamespacedName('test', 'some_counter'), 'it increases', ['type']);
$counter->incBy((int) $_GET['c'], ['blue']);

echo "OK\n";
3 changes: 2 additions & 1 deletion examples/some_gauge.php
Expand Up @@ -5,6 +5,7 @@
require __DIR__ . '/../vendor/autoload.php';

use Prometheus\Registry\CollectorRegistry;
use Prometheus\Value\MetricName;

error_log('c=' . $_GET['c']);

Expand All @@ -21,7 +22,7 @@
}
$registry = new CollectorRegistry($adapter);

$gauge = $registry->registerGauge('test', 'some_gauge', 'it sets', ['type']);
$gauge = $registry->registerGauge(MetricName::fromNamespacedName('test', 'some_gauge'), 'it sets', ['type']);
$gauge->set((float) $_GET['c'], ['blue']);

echo "OK\n";
3 changes: 2 additions & 1 deletion examples/some_histogram.php
Expand Up @@ -5,6 +5,7 @@
require __DIR__ . '/../vendor/autoload.php';

use Prometheus\Registry\CollectorRegistry;
use Prometheus\Value\MetricName;

error_log('c=' . $_GET['c']);

Expand All @@ -21,7 +22,7 @@
}
$registry = new CollectorRegistry($adapter);

$histogram = $registry->registerHistogram('test', 'some_histogram', 'it observes', ['type'], [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9]);
$histogram = $registry->registerHistogram(MetricName::fromNamespacedName('test', 'some_histogram'), 'it observes', ['type'], [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9]);
$histogram->observe((float) $_GET['c'], ['blue']);

echo "OK\n";
7 changes: 4 additions & 3 deletions src/Prometheus/Counter.php
Expand Up @@ -6,6 +6,7 @@

use Prometheus\Storage\CounterStorage;
use Prometheus\Storage\StorageCommand;
use Prometheus\Value\MetricName;

class Counter extends Metric
{
Expand All @@ -17,9 +18,9 @@ class Counter extends Metric
/**
* @inheritdoc
*/
public function __construct(CounterStorage $storage, string $namespace, string $name, string $help, array $labels = [])
public function __construct(CounterStorage $storage, MetricName $name, string $help, array $labels = [])
{
parent::__construct($namespace, $name, $help, $labels);
parent::__construct($name, $help, $labels);
$this->storage = $storage;
}

Expand All @@ -45,8 +46,8 @@ public function incBy(int $count, array $labels = []) : void
$this->assertLabelsAreDefinedCorrectly($labels);

$this->storage->updateCounter(
$this->getName(),
[
'name' => $this->getName(),
'help' => $this->getHelp(),
'type' => $this->getType(),
'labelNames' => $this->getLabelNames(),
Expand Down
11 changes: 7 additions & 4 deletions src/Prometheus/Gauge.php
Expand Up @@ -6,6 +6,7 @@

use Prometheus\Storage\GaugeStorage;
use Prometheus\Storage\StorageCommand;
use Prometheus\Value\MetricName;

class Gauge extends Metric
{
Expand All @@ -17,9 +18,9 @@ class Gauge extends Metric
/**
* @inheritdoc
*/
public function __construct(GaugeStorage $storage, string $namespace, string $name, string $help, array $labels = [])
public function __construct(GaugeStorage $storage, MetricName $name, string $help, array $labels = [])
{
parent::__construct($namespace, $name, $help, $labels);
parent::__construct($name, $help, $labels);
$this->storage = $storage;
}

Expand All @@ -32,8 +33,9 @@ public function set(float $value, array $labels = []) : void
$this->assertLabelsAreDefinedCorrectly($labels);

$this->storage->updateGauge(
$this->getName(),
[
'name' => $this->getName(),
'name' => $this->getName()->toString(),
'help' => $this->getHelp(),
'type' => $this->getType(),
'labelNames' => $this->getLabelNames(),
Expand Down Expand Up @@ -65,8 +67,9 @@ public function incBy(float $value, array $labels = []) : void
$this->assertLabelsAreDefinedCorrectly($labels);

$this->storage->updateGauge(
$this->getName(),
[
'name' => $this->getName(),
'name' => $this->getName()->toString(),
'help' => $this->getHelp(),
'type' => $this->getType(),
'labelNames' => $this->getLabelNames(),
Expand Down
7 changes: 4 additions & 3 deletions src/Prometheus/Histogram.php
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use Prometheus\Storage\HistogramStorage;
use Prometheus\Value\MetricName;
use function count;

class Histogram extends Metric
Expand All @@ -22,9 +23,9 @@ class Histogram extends Metric
* @param string[] $labels
* @param float[] $buckets
*/
public function __construct(HistogramStorage $storage, string $namespace, string $name, string $help, array $labels = [], ?array $buckets = null)
public function __construct(HistogramStorage $storage, MetricName $name, string $help, array $labels = [], ?array $buckets = null)
{
parent::__construct($namespace, $name, $help, $labels);
parent::__construct($name, $help, $labels);
$this->storage = $storage;

if ($buckets === null) {
Expand Down Expand Up @@ -85,9 +86,9 @@ public function observe(float $value, array $labels = []) : void
$this->assertLabelsAreDefinedCorrectly($labels);

$this->storage->updateHistogram(
$this->getName(),
[
'value' => $value,
'name' => $this->getName(),
'help' => $this->getHelp(),
'type' => $this->getType(),
'labelNames' => $this->getLabelNames(),
Expand Down
15 changes: 6 additions & 9 deletions src/Prometheus/Metric.php
Expand Up @@ -5,6 +5,7 @@
namespace Prometheus;

use InvalidArgumentException;
use Prometheus\Value\MetricName;
use function count;
use function preg_match;
use function print_r;
Expand All @@ -14,8 +15,8 @@ abstract class Metric
{
public const RE_METRIC_LABEL_NAME = '/^[a-zA-Z_:][a-zA-Z0-9_:]*$/';

/** @var string */
protected $name;
/** @var MetricName */
private $name;
/** @var string */
protected $help;
/** @var string[] */
Expand All @@ -24,13 +25,9 @@ abstract class Metric
/**
* @param string[] $labels
*/
public function __construct(string $namespace, string $name, string $help, array $labels = [])
public function __construct(MetricName $name, string $help, array $labels = [])
{
$metricName = ($namespace ? $namespace . '_' : '') . $name;
if (! preg_match(self::RE_METRIC_LABEL_NAME, $metricName)) {
throw new InvalidArgumentException("Invalid metric name: '" . $metricName . "'");
}
$this->name = $metricName;
$this->name = $name;
$this->help = $help;
foreach ($labels as $label) {
if (! preg_match(self::RE_METRIC_LABEL_NAME, $label)) {
Expand All @@ -42,7 +39,7 @@ public function __construct(string $namespace, string $name, string $help, array

abstract public function getType() : string;

public function getName() : string
public function getName() : MetricName
{
return $this->name;
}
Expand Down

0 comments on commit 773cecd

Please sign in to comment.