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..961f803 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', null, $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..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() + public function setUp(): void { $this->adapter = getenv('ADAPTER'); $this->client = new Client(['base_uri' => 'http://nginx:80/']);