From 3c33519787ed2480f3b339cdcb84c49f7ddd91b2 Mon Sep 17 00:00:00 2001 From: Andrei Moldovan Date: Mon, 19 Oct 2020 20:08:33 +0100 Subject: [PATCH] Added support for HTTPS Pushgateway endpoints Signed-off-by: Andrei Moldovan --- README.md | 7 +++- examples/pushgateway.php | 2 +- php-fpm/Dockerfile | 7 ++++ php-fpm/docker-php-ext-apcu-cli.ini | 1 + php-fpm/www.conf | 6 +++ src/PrometheusPushGateway/PushGateway.php | 10 ++--- tests/Test/BlackBoxPushGatewayTest.php | 5 ++- .../PrometheusPushGateway/PushGatewayTest.php | 37 +++++++++++++++++++ 8 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 php-fpm/Dockerfile create mode 100644 php-fpm/docker-php-ext-apcu-cli.ini create mode 100644 php-fpm/www.conf diff --git a/README.md b/README.md index 88b7d2b..b0edf0d 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Let's assume you have that simple counter and want to send it to your PushGatewa ->inc(); // Now send it to the PushGateway: -$pushGateway = new \PrometheusPushGateway\PushGateway('192.168.59.100:9091'); +$pushGateway = new \PrometheusPushGateway\PushGateway('http://192.168.59.100:9091'); $pushGateway->push(\Prometheus\CollectorRegistry::getDefault(), 'my_job', ['instance' => 'foo']); ``` @@ -44,6 +44,11 @@ Just start the PushGateway by using docker-compose docker-compose up ``` +Use composer to grab all dependencies +``` +docker run --rm --interactive --tty --volume $PWD:/app composer install +``` + Execute the tests: ``` docker-compose run phpunit vendor/bin/phpunit tests/Test/ diff --git a/examples/pushgateway.php b/examples/pushgateway.php index e148cd0..a6ccf3e 100644 --- a/examples/pushgateway.php +++ b/examples/pushgateway.php @@ -22,5 +22,5 @@ $counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']); $counter->incBy(6, ['blue']); -$pushGateway = new PushGateway('192.168.59.100:9091'); +$pushGateway = new PushGateway('http://192.168.59.100:9091'); $pushGateway->push($registry, 'my_job', ['instance' => 'foo']); diff --git a/php-fpm/Dockerfile b/php-fpm/Dockerfile new file mode 100644 index 0000000..3a7421e --- /dev/null +++ b/php-fpm/Dockerfile @@ -0,0 +1,7 @@ +FROM php:7.2-fpm + +RUN pecl install redis-5.3.1 && docker-php-ext-enable redis +RUN pecl install apcu-5.1.19 && 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/php-fpm/www.conf b/php-fpm/www.conf new file mode 100644 index 0000000..23420b1 --- /dev/null +++ b/php-fpm/www.conf @@ -0,0 +1,6 @@ +[www] +user = www-data +group = www-data +listen = 127.0.0.1:9000 +pm = static +pm.max_children = 20 diff --git a/src/PrometheusPushGateway/PushGateway.php b/src/PrometheusPushGateway/PushGateway.php index 7a1eecb..8bca6b6 100644 --- a/src/PrometheusPushGateway/PushGateway.php +++ b/src/PrometheusPushGateway/PushGateway.php @@ -25,12 +25,12 @@ class PushGateway /** * PushGateway constructor. - * @param string $address host:port of the push gateway + * @param string $address (http|https)://host:port of the push gateway * @param ClientInterface $client */ public function __construct($address, ClientInterface $client = null) { - $this->address = $address; + $this->address = strpos($address, 'http') === false ? 'http://' . $address : $address; $this->client = $client ?? new Client(); } @@ -73,15 +73,15 @@ public function delete(string $job, array $groupingKey = []): void } /** - * @param CollectorRegistry $collectorRegistry + * @param CollectorRegistry|null $collectorRegistry * @param string $job * @param array $groupingKey * @param string $method * @throws GuzzleException */ - private function doRequest(CollectorRegistry $collectorRegistry, string $job, array $groupingKey, $method): void + private function doRequest(?CollectorRegistry $collectorRegistry, string $job, array $groupingKey, $method): void { - $url = "http://" . $this->address . "/metrics/job/" . $job; + $url = $this->address . "/metrics/job/" . $job; if (!empty($groupingKey)) { foreach ($groupingKey as $label => $value) { $url .= "/" . $label . "/" . $value; diff --git a/tests/Test/BlackBoxPushGatewayTest.php b/tests/Test/BlackBoxPushGatewayTest.php index 66d0bf2..6737aa9 100644 --- a/tests/Test/BlackBoxPushGatewayTest.php +++ b/tests/Test/BlackBoxPushGatewayTest.php @@ -26,18 +26,19 @@ public function pushGatewayShouldWork() $httpClient = new Client(); $metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents(); - $this->assertContains( + $this->assertStringContainsString( '# HELP test_some_counter it increases # TYPE test_some_counter counter test_some_counter{instance="foo",job="my_job",type="blue"} 6', $metrics ); + $pushGateway = new PushGateway('http://pushgateway:9091'); $pushGateway->delete('my_job', ['instance' => 'foo']); $httpClient = new Client(); $metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents(); - $this->assertNotContains( + $this->assertStringNotContainsString( '# HELP test_some_counter it increases # TYPE test_some_counter counter test_some_counter{instance="foo",job="my_job",type="blue"} 6', diff --git a/tests/Test/PrometheusPushGateway/PushGatewayTest.php b/tests/Test/PrometheusPushGateway/PushGatewayTest.php index 65c5468..076a629 100644 --- a/tests/Test/PrometheusPushGateway/PushGatewayTest.php +++ b/tests/Test/PrometheusPushGateway/PushGatewayTest.php @@ -76,4 +76,41 @@ public function clientGetsDefinedIfNotSpecified(): void $pushGateway = new PushGateway('http://foo.bar'); $pushGateway->push($mockedCollectorRegistry, 'foo'); } + + /** + * @test + * + * @dataProvider validAddressAndRequestsProvider + */ + public function validAddressShouldCreateValidRequests(string $address, string $scheme, string $host, int $port): void + { + $mockedCollectorRegistry = $this->createMock(CollectorRegistry::class); + $mockedCollectorRegistry->method('getMetricFamilySamples')->with()->willReturn([ + $this->createMock(MetricFamilySamples::class) + ]); + + $mockHandler = new MockHandler([ + new Response(200), + ]); + $handler = HandlerStack::create($mockHandler); + $client = new Client(['handler' => $handler]); + + $pushGateway = new PushGateway($address, $client); + $pushGateway->push($mockedCollectorRegistry, 'foo'); + + $uri = $mockHandler->getLastRequest()->getUri(); + $this->assertEquals($scheme, $uri->getScheme()); + $this->assertEquals($host, $uri->getHost()); + $this->assertEquals($port, $uri->getPort()); + $this->assertEquals('/metrics/job/foo', $uri->getPath()); + } + + public function validAddressAndRequestsProvider() + { + return [ + ['foo.bar:123', 'http', 'foo.bar', 123], + ['http://foo.bar:123', 'http', 'foo.bar', 123], + ['https://foo.bar:123', 'https', 'foo.bar', 123], + ]; + } }