diff --git a/README.md b/README.md index d5e1ff0..a8bb52a 100644 --- a/README.md +++ b/README.md @@ -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/ ``` diff --git a/composer.json b/composer.json index 121d587..3c49f70 100644 --- a/composer.json +++ b/composer.json @@ -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." }, diff --git a/docker-compose.yml b/docker-compose.yml index 00dab20..43447ab 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,7 @@ php-fpm: - .:/var/www/html links: - redis + - pushgateway environment: - REDIS_HOST=redis @@ -18,3 +19,19 @@ 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 diff --git a/examples/pushgateway.php b/examples/pushgateway.php new file mode 100644 index 0000000..e92baae --- /dev/null +++ b/examples/pushgateway.php @@ -0,0 +1,13 @@ +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')); diff --git a/php-fpm/Dockerfile b/php-fpm/Dockerfile index 0fbe56c..fc34512 100644 --- a/php-fpm/Dockerfile +++ b/php-fpm/Dockerfile @@ -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/ diff --git a/php-fpm/docker-php-ext-apcu-cli.ini b/php-fpm/docker-php-ext-apcu-cli.ini new file mode 100644 index 0000000..c376be0 --- /dev/null +++ b/php-fpm/docker-php-ext-apcu-cli.ini @@ -0,0 +1 @@ +apc.enable_cli = On diff --git a/src/Prometheus/PushGateway.php b/src/Prometheus/PushGateway.php new file mode 100644 index 0000000..a4a3fd2 --- /dev/null +++ b/src/Prometheus/PushGateway.php @@ -0,0 +1,91 @@ +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); + } + } + +} diff --git a/tests/Test/BlackBoxPushGatewayTest.php b/tests/Test/BlackBoxPushGatewayTest.php new file mode 100644 index 0000000..26490c6 --- /dev/null +++ b/tests/Test/BlackBoxPushGatewayTest.php @@ -0,0 +1,47 @@ +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 + ); + } +} diff --git a/tests/Test/BlackBoxTest.php b/tests/Test/BlackBoxTest.php index daff9dd..d57f3a3 100644 --- a/tests/Test/BlackBoxTest.php +++ b/tests/Test/BlackBoxTest.php @@ -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); }