Skip to content

Commit

Permalink
Merge pull request endclothing#28 from Jimdo/pushgateway
Browse files Browse the repository at this point in the history
Add PushGateway implementation
  • Loading branch information
bracki authored Oct 11, 2016
2 parents 43fc3bd + 88fb86b commit 40259be
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 6 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,11 @@ composer install

Just start the nginx, fpm & Redis setup with docker-compose:
```
composer require guzzlehttp/guzzle=~6.0
docker-compose up
```
Pick the adapter you want to test.

```
ADAPTER=redis vendor/bin/phpunit tests/Test/BlackBoxTest.php
ADAPTER=apc vendor/bin/phpunit tests/Test/BlackBoxTest.php
docker-compose run phpunit env ADAPTER=apc vendor/bin/phpunit tests/Test/
docker-compose run phpunit env ADAPTER=redis vendor/bin/phpunit tests/Test/
```
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
}
],
"require": {
"php": ">=5.6.3"
"php": ">=5.6.3",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
"phpunit/phpunit": "4.1.0"
},
"suggest": {
"guzzlehttp/guzzle": "~6.0 for running the blackbox tests.",
"ext-redis": "Required if using Redis.",
"ext-apc": "Required if using APCu."
},
Expand Down
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,27 @@ php-fpm:
- .:/var/www/html
links:
- redis
- pushgateway
environment:
- REDIS_HOST=redis

redis:
image: redis
ports:
- 6379:6379

pushgateway:
image: prom/pushgateway
ports:
- 9091:9091

phpunit:
build: php-fpm/
volumes:
- .:/var/www/html
links:
- redis
- pushgateway
- nginx
environment:
- REDIS_HOST=redis
13 changes: 13 additions & 0 deletions examples/pushgateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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'));
1 change: 1 addition & 0 deletions php-fpm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ RUN pecl install redis-2.2.8 && docker-php-ext-enable redis
RUN pecl install apcu-4.0.11 && docker-php-ext-enable apcu

COPY www.conf /usr/local/etc/php-fpm.d/
COPY docker-php-ext-apcu-cli.ini /usr/local/etc/php/conf.d/
1 change: 1 addition & 0 deletions php-fpm/docker-php-ext-apcu-cli.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apc.enable_cli = On
91 changes: 91 additions & 0 deletions src/Prometheus/PushGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php


namespace Prometheus;


use GuzzleHttp\Client;

class PushGateway
{
private $address;

/**
* PushGateway constructor.
* @param $address string host:port of the push gateway
*/
public function __construct($address)
{
$this->address = $address;
}

/**
* Pushes all metrics in a Collector, replacing all those with the same job.
* Uses HTTP PUT.
* @param CollectorRegistry $collectorRegistry
* @param $job
* @param $groupingKey
*/
public function push(CollectorRegistry $collectorRegistry, $job, $groupingKey = null)
{
$this->doRequest($collectorRegistry, $job, $groupingKey, 'put');
}

/**
* Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name and job.
* Uses HTTP POST.
* @param CollectorRegistry $collectorRegistry
* @param $job
* @param $groupingKey
*/
public function pushAdd(CollectorRegistry $collectorRegistry, $job, $groupingKey = null)
{
$this->doRequest($collectorRegistry, $job, $groupingKey, 'post');
}

/**
* Deletes metrics from the Pushgateway.
* Uses HTTP POST.
* @param $job
* @param $groupingKey
*/
public function delete($job, $groupingKey = null)
{
$this->doRequest(null, $job, $groupingKey, 'delete');
}

/**
* @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);
}
}

}
47 changes: 47 additions & 0 deletions tests/Test/BlackBoxPushGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace Test;

use GuzzleHttp\Client;
use PHPUnit_Framework_TestCase;

use Prometheus\CollectorRegistry;
use Prometheus\PushGateway;
use Prometheus\Storage\APC;

class BlackBoxPushGatewayTest extends PHPUnit_Framework_TestCase
{
/**
* @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
);
}
}
2 changes: 1 addition & 1 deletion tests/Test/BlackBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BlackBoxTest extends PHPUnit_Framework_TestCase
public function setUp()
{
$this->adapter = getenv('ADAPTER');
$this->client = new Client(['base_uri' => 'http://192.168.59.100:8080/']);
$this->client = new Client(['base_uri' => 'http://nginx:80/']);
$this->client->get('/examples/flush_adapter.php?adapter=' . $this->adapter);
}

Expand Down

0 comments on commit 40259be

Please sign in to comment.