From 82218c82477d57c66a6d192dfec447d155081474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20N=C3=B3brega?= Date: Tue, 15 Oct 2019 17:57:20 -0500 Subject: [PATCH 1/3] Add transport method to url --- src/Prometheus/PushGateway.php | 43 +++++++++++++++++++------- tests/Test/BlackBoxPushGatewayTest.php | 19 ++++++++---- tests/Test/BlackBoxTest.php | 2 +- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/Prometheus/PushGateway.php b/src/Prometheus/PushGateway.php index 8b99982..15414af 100644 --- a/src/Prometheus/PushGateway.php +++ b/src/Prometheus/PushGateway.php @@ -11,11 +11,21 @@ class PushGateway { + const ALLOWED_TRANSPORT_METHODS = [ + 'http', + 'https', + ]; + /** * @var string */ private $address; + /** + * @var string + */ + private $transport; + /** * @var ClientInterface */ @@ -23,13 +33,20 @@ class PushGateway /** * PushGateway constructor. - * @param string $address host:port of the push gateway + * @param $address string host:port of the push gateway * @param ClientInterface $client + * @param $transport string transport method of the push gateway */ - public function __construct($address, ClientInterface $client = null) + public function __construct($address, ClientInterface $client = null, $transport = 'http') { $this->address = $address; $this->client = $client ?? new Client(); + + if (!in_array($transport, self::ALLOWED_TRANSPORT_METHODS)) { + throw new \InvalidArgumentException(\sprintf('Invalid transport "%s"', $transport)); + } + + $this->transport = $transport; } /** @@ -42,7 +59,7 @@ public function __construct($address, ClientInterface $client = null) */ public function push(CollectorRegistry $collectorRegistry, string $job, array $groupingKey = []): void { - $this->doRequest($collectorRegistry, $job, $groupingKey, 'put'); + $this->doRequest($collectorRegistry, $job, 'put', $groupingKey); } /** @@ -55,7 +72,7 @@ public function push(CollectorRegistry $collectorRegistry, string $job, array $g */ public function pushAdd(CollectorRegistry $collectorRegistry, string $job, array $groupingKey = []): void { - $this->doRequest($collectorRegistry, $job, $groupingKey, 'post'); + $this->doRequest($collectorRegistry, $job, 'post', $groupingKey); } /** @@ -67,7 +84,7 @@ public function pushAdd(CollectorRegistry $collectorRegistry, string $job, array */ public function delete(string $job, array $groupingKey = []): void { - $this->doRequest(null, $job, $groupingKey, 'delete'); + $this->doRequest(null, $job, 'delete', $groupingKey); } /** @@ -77,13 +94,17 @@ public function delete(string $job, array $groupingKey = []): void * @param string $method * @throws GuzzleException */ - private function doRequest(CollectorRegistry $collectorRegistry, string $job, array $groupingKey, $method): void + private function doRequest(CollectorRegistry $collectorRegistry, string $job, string $method, array $groupingKey = []): void { - $url = "http://" . $this->address . "/metrics/job/" . $job; - if (!empty($groupingKey)) { - foreach ($groupingKey as $label => $value) { - $url .= "/" . $label . "/" . $value; - } + $url = \sprintf( + "%s://%s/metrics/job/%s", + $this->transport, + $this->address, + $job + ); + + foreach ($groupingKey as $label => $value) { + $url .= "/" . $label . "/" . $value; } $requestOptions = [ diff --git a/tests/Test/BlackBoxPushGatewayTest.php b/tests/Test/BlackBoxPushGatewayTest.php index b3ad8ac..8230b1e 100644 --- a/tests/Test/BlackBoxPushGatewayTest.php +++ b/tests/Test/BlackBoxPushGatewayTest.php @@ -10,10 +10,17 @@ class BlackBoxPushGatewayTest extends TestCase { + public function transportProvider() + { + yield ['http']; + yield ['https']; + } + /** + * @dataProvider transportProvider * @test */ - public function pushGatewayShouldWork() + public function pushGatewayShouldWork(string $transport) { $adapter = new APC(); $registry = new CollectorRegistry($adapter); @@ -21,11 +28,11 @@ public function pushGatewayShouldWork() $counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']); $counter->incBy(6, ['blue']); - $pushGateway = new PushGateway('pushgateway:9091'); + $pushGateway = new PushGateway('pushgateway:9091', $transport); $pushGateway->push($registry, 'my_job', ['instance' => 'foo']); - $httpClient = new Client(); - $metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents(); + $client = new Client(); + $metrics = $client->get($transport . "://pushgateway:9091/metrics")->getBody()->getContents(); $this->assertContains( '# HELP test_some_counter it increases # TYPE test_some_counter counter @@ -35,8 +42,8 @@ public function pushGatewayShouldWork() $pushGateway->delete('my_job', ['instance' => 'foo']); - $httpClient = new Client(); - $metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents(); + $client = new Client(); + $metrics = $client->get($transport . "://pushgateway:9091/metrics")->getBody()->getContents(); $this->assertNotContains( '# HELP test_some_counter it increases # TYPE test_some_counter counter diff --git a/tests/Test/BlackBoxTest.php b/tests/Test/BlackBoxTest.php index b4d8a9e..f208aed 100644 --- a/tests/Test/BlackBoxTest.php +++ b/tests/Test/BlackBoxTest.php @@ -18,7 +18,7 @@ class BlackBoxTest extends TestCase */ private $adapter; - public function setUp() + public function setUp() : void { $this->adapter = getenv('ADAPTER'); $this->client = new Client(['base_uri' => 'http://nginx:80/']); From 44bde8a46d728fe0ac6d9fce36b83286f506baba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20N=C3=B3brega?= Date: Tue, 28 Jan 2020 16:29:42 -0600 Subject: [PATCH 2/3] Fix unit test instance signature --- tests/Test/BlackBoxPushGatewayTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Test/BlackBoxPushGatewayTest.php b/tests/Test/BlackBoxPushGatewayTest.php index 8230b1e..961f803 100644 --- a/tests/Test/BlackBoxPushGatewayTest.php +++ b/tests/Test/BlackBoxPushGatewayTest.php @@ -28,7 +28,7 @@ public function pushGatewayShouldWork(string $transport) $counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']); $counter->incBy(6, ['blue']); - $pushGateway = new PushGateway('pushgateway:9091', $transport); + $pushGateway = new PushGateway('pushgateway:9091', null, $transport); $pushGateway->push($registry, 'my_job', ['instance' => 'foo']); $client = new Client(); From 8826c217a716fa1e457f1af20d0afce15daba49e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20N=C3=B3brega?= Date: Tue, 28 Jan 2020 16:32:48 -0600 Subject: [PATCH 3/3] Fix return type declaration --- tests/Test/BlackBoxTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Test/BlackBoxTest.php b/tests/Test/BlackBoxTest.php index f208aed..1d12de8 100644 --- a/tests/Test/BlackBoxTest.php +++ b/tests/Test/BlackBoxTest.php @@ -18,7 +18,7 @@ class BlackBoxTest extends TestCase */ private $adapter; - public function setUp() : void + public function setUp(): void { $this->adapter = getenv('ADAPTER'); $this->client = new Client(['base_uri' => 'http://nginx:80/']);