Ejemplo n.º 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;
 }
    /**
     * @test
     */
    public function pushGatewayShouldWork()
    {
        $adapter = new APC();
        $registry = new CollectorRegistry($adapter);
        $counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
        $counter->incBy(6, ['blue']);
        $pushGateway = new PushGateway('pushgateway:9091');
        $pushGateway->push($registry, 'my_job', array('instance' => 'foo'));
        $httpClient = new Client();
        $metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents();
        $this->assertContains('# HELP test_some_counter it increases
# TYPE test_some_counter counter
test_some_counter{instance="foo",job="my_job",type="blue"} 6', $metrics);
        $pushGateway->delete('my_job', array('instance' => 'foo'));
        $httpClient = new Client();
        $metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents();
        $this->assertNotContains('# HELP test_some_counter it increases
# TYPE test_some_counter counter
test_some_counter{instance="foo",job="my_job",type="blue"} 6', $metrics);
    }
Ejemplo n.º 3
0
 /**
  * @param CollectorRegistry $collectorRegistry
  * @param $job
  * @param $groupingKey
  * @param $method
  */
 private function doRequest(CollectorRegistry $collectorRegistry, $job, $groupingKey, $method)
 {
     $url = "http://" . $this->address . "/metrics/job/" . $job;
     if (!empty($groupingKey)) {
         foreach ($groupingKey as $label => $value) {
             $url .= "/" . $label . "/" . $value;
         }
     }
     $client = new Client();
     $requestOptions = array('headers' => array('Content-Type' => RenderTextFormat::MIME_TYPE), 'connect_timeout' => 10, 'timeout' => 20);
     if ($method != 'delete') {
         $renderer = new RenderTextFormat();
         $requestOptions['body'] = $renderer->render($collectorRegistry->getMetricFamilySamples());
     }
     $response = $client->request($method, $url, $requestOptions);
     $statusCode = $response->getStatusCode();
     if ($statusCode != 202) {
         $msg = "Unexpected status code " . $statusCode . " received from pushgateway " . $this->address . ": " . $response->getBody();
         throw new \RuntimeException($msg);
     }
 }
Ejemplo n.º 4
0
<?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);
$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy($_GET['c'], ['blue']);
echo "OK\n";
Ejemplo n.º 5
0
<?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\MetricNotFoundException
  */
 public function itShouldThrowAnExceptionWhenGettingANonExistentMetric()
 {
     $registry = new CollectorRegistry($this->adapter);
     $registry->getGauge("not_here", "go_away");
 }
Ejemplo n.º 7
0
<?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);
$histogram = $registry->registerHistogram('test', 'some_histogram', 'it observes', ['type'], [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9]);
$histogram->observe($_GET['c'], ['blue']);
echo "OK\n";
Ejemplo n.º 8
0
<?php

require __DIR__ . '/../vendor/autoload.php';
use Prometheus\CollectorRegistry;
use Prometheus\RenderTextFormat;
use Prometheus\Storage\Redis;
$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);
$renderer = new RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples());
header('Content-type: ' . RenderTextFormat::MIME_TYPE);
echo $result;
Ejemplo n.º 9
0
<?php

require __DIR__ . '/../vendor/autoload.php';
use Prometheus\CollectorRegistry;
$adapter = new Prometheus\Storage\APC();
$registry = new CollectorRegistry($adapter);
$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy(6, ['blue']);
$pushGateway = new \Prometheus\PushGateway('192.168.59.100:9091');
$pushGateway->push($registry, 'my_job', array('instance' => 'foo'));