示例#1
0
 /**
  * @param string $variable
  * @return \Prometheus\Gauge
  */
 private function getOrRegisterGaugeForVariable($variable)
 {
     try {
         $gauge = $this->collectorRegistry->getGauge($this->namespace, $variable);
     } catch (MetricNotFoundException $e) {
         $gauge = $this->collectorRegistry->registerGauge($this->namespace, $variable, '', array_keys($this->tags));
     }
     return $gauge;
 }
<?php

require __DIR__ . '/../vendor/autoload.php';
use Prometheus\CollectorRegistry;
use Prometheus\Storage\Redis;
error_log('c=' . $_GET['c']);
$adapter = $_GET['adapter'];
if ($adapter == 'redis') {
    Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
    $adapter = new Prometheus\Storage\Redis();
}
if ($adapter == 'apc') {
    $adapter = new Prometheus\Storage\APC();
}
$registry = new CollectorRegistry($adapter);
$gauge = $registry->registerGauge('test', 'some_gauge', 'it sets', ['type']);
$gauge->set($_GET['c'], ['blue']);
echo "OK\n";
 /**
  * @test
  * @expectedException \Prometheus\Exception\MetricsRegistrationException
  */
 public function itShouldForbidRegisteringTheSameGaugeWithDifferentLabels()
 {
     $registry = new CollectorRegistry($this->adapter);
     $registry->registerGauge('foo', 'metric', 'help', array("foo", "bar"));
     $registry->registerGauge('foo', 'metric', 'help', array("spam", "eggs"));
 }