Skip to content
Permalink
Browse files

Introduce a value object to hold the metric name

  • Loading branch information...
LeSuisse committed May 25, 2019
1 parent db024b9 commit 773cecd111472b3930edfaf6bfaa00a7c0f147da
@@ -9,6 +9,7 @@
use Prometheus\PushGateway\PSR18Pusher;
use Prometheus\Registry\CollectorRegistry;
use Prometheus\Storage\RedisStore;
use Prometheus\Value\MetricName;
$adapter = $_GET['adapter'] ?? '';
@@ -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());
@@ -5,6 +5,7 @@
require __DIR__ . '/../vendor/autoload.php';
use Prometheus\Registry\CollectorRegistry;
use Prometheus\Value\MetricName;
$adapter = $_GET['adapter'];
@@ -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";
@@ -5,6 +5,7 @@
require __DIR__ . '/../vendor/autoload.php';
use Prometheus\Registry\CollectorRegistry;
use Prometheus\Value\MetricName;
error_log('c=' . $_GET['c']);
@@ -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";
@@ -5,6 +5,7 @@
require __DIR__ . '/../vendor/autoload.php';
use Prometheus\Registry\CollectorRegistry;
use Prometheus\Value\MetricName;
error_log('c=' . $_GET['c']);
@@ -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";
@@ -6,6 +6,7 @@
use Prometheus\Storage\CounterStorage;
use Prometheus\Storage\StorageCommand;
use Prometheus\Value\MetricName;
class Counter extends Metric
{
@@ -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;
}
@@ -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(),
@@ -6,6 +6,7 @@
use Prometheus\Storage\GaugeStorage;
use Prometheus\Storage\StorageCommand;
use Prometheus\Value\MetricName;
class Gauge extends Metric
{
@@ -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;
}
@@ -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(),
@@ -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(),
@@ -6,6 +6,7 @@
use InvalidArgumentException;
use Prometheus\Storage\HistogramStorage;
use Prometheus\Value\MetricName;
use function count;
class Histogram extends Metric
@@ -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) {
@@ -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(),
@@ -5,6 +5,7 @@
namespace Prometheus;
use InvalidArgumentException;
use Prometheus\Value\MetricName;
use function count;
use function preg_match;
use function print_r;
@@ -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[] */
@@ -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)) {
@@ -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;
}

0 comments on commit 773cecd

Please sign in to comment.
You can’t perform that action at this time.