diff --git a/examples/pushgateway.php b/examples/pushgateway.php index 984035d..024bf21 100644 --- a/examples/pushgateway.php +++ b/examples/pushgateway.php @@ -20,5 +20,6 @@ $counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']); $counter->incBy(6, ['blue']); -$pushGateway = new \Prometheus\PushGateway('192.168.59.100:9091'); +$client = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.59.100:9091/metrics/job']); +$pushGateway = new \Prometheus\PushGateway($client); $pushGateway->push($registry, 'my_job', array('instance'=>'foo')); diff --git a/src/Prometheus/PushGateway.php b/src/Prometheus/PushGateway.php index a4a3fd2..95c654f 100644 --- a/src/Prometheus/PushGateway.php +++ b/src/Prometheus/PushGateway.php @@ -8,67 +8,83 @@ class PushGateway { - private $address; + /** + * @var Client $client + */ + private $client; /** * PushGateway constructor. - * @param $address string host:port of the push gateway + * @param Client $client client with configured push gateway base_uri param, example uri: http://pushgateway.com/metrics/job/ */ - public function __construct($address) + public function __construct(Client $client) { - $this->address = $address; + $this->client = $client; + } + + /** + * @param string $address + * @return PushGateway + */ + public static function makeClientWithAddress($address) + { + $client = new Client(['base_uri' => "http://$address/metrics/job/"]); + return new PushGateway($client); } /** * Pushes all metrics in a Collector, replacing all those with the same job. * Uses HTTP PUT. * @param CollectorRegistry $collectorRegistry - * @param $job - * @param $groupingKey + * @param string $job + * @param array|null $groupingKey + * @throws \GuzzleHttp\Exception\GuzzleException */ public function push(CollectorRegistry $collectorRegistry, $job, $groupingKey = null) { - $this->doRequest($collectorRegistry, $job, $groupingKey, 'put'); + $this->doRequest('put', $job, $groupingKey, $collectorRegistry); } /** * 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 + * @param string $job + * @param array|null $groupingKey + * @throws \GuzzleHttp\Exception\GuzzleException */ public function pushAdd(CollectorRegistry $collectorRegistry, $job, $groupingKey = null) { - $this->doRequest($collectorRegistry, $job, $groupingKey, 'post'); + $this->doRequest('post', $job, $groupingKey, $collectorRegistry); } /** * Deletes metrics from the Pushgateway. * Uses HTTP POST. - * @param $job - * @param $groupingKey + * @param string $job + * @param array|null $groupingKey + * @throws \GuzzleHttp\Exception\GuzzleException */ public function delete($job, $groupingKey = null) { - $this->doRequest(null, $job, $groupingKey, 'delete'); + $this->doRequest('delete', $job, $groupingKey); } /** - * @param CollectorRegistry $collectorRegistry - * @param $job - * @param $groupingKey - * @param $method + * @param string $method + * @param string $job + * @param array|null $groupingKey + * @param CollectorRegistry|null $collectorRegistry + * @throws \GuzzleHttp\Exception\GuzzleException */ - private function doRequest(CollectorRegistry $collectorRegistry, $job, $groupingKey, $method) + private function doRequest($method, $job, $groupingKey, CollectorRegistry $collectorRegistry = null) { - $url = "http://" . $this->address . "/metrics/job/" . $job; + $url = $job; if (!empty($groupingKey)) { foreach ($groupingKey as $label => $value) { - $url .= "/" . $label . "/" . $value; + $url .= '/' . $label . '/' . $value; } } - $client = new Client(); $requestOptions = array( 'headers' => array( 'Content-Type' => RenderTextFormat::MIME_TYPE @@ -76,14 +92,17 @@ private function doRequest(CollectorRegistry $collectorRegistry, $job, $grouping 'connect_timeout' => 10, 'timeout' => 20, ); - if ($method != 'delete') { + if ($method !== 'delete') { + if ($collectorRegistry === null) { + throw new \RuntimeException('CollectorRegistry not set'); + } $renderer = new RenderTextFormat(); $requestOptions['body'] = $renderer->render($collectorRegistry->getMetricFamilySamples()); } - $response = $client->request($method, $url, $requestOptions); + $response = $this->client->request($method, $url, $requestOptions); $statusCode = $response->getStatusCode(); - if ($statusCode != 202) { - $msg = "Unexpected status code " . $statusCode . " received from pushgateway " . $this->address . ": " . $response->getBody(); + if ($statusCode !== 202) { + $msg = "Unexpected status code {$statusCode} received from pushgateway body: {$response->getBody()}"; throw new \RuntimeException($msg); } } diff --git a/tests/Test/BlackBoxPushGatewayTest.php b/tests/Test/BlackBoxPushGatewayTest.php index 26490c6..3246b70 100644 --- a/tests/Test/BlackBoxPushGatewayTest.php +++ b/tests/Test/BlackBoxPushGatewayTest.php @@ -21,7 +21,8 @@ public function pushGatewayShouldWork() $counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']); $counter->incBy(6, ['blue']); - $pushGateway = new PushGateway('pushgateway:9091'); + $client = new Client(['base_uri' => 'http://pushgateway:9091/metrics/job/']); + $pushGateway = new PushGateway($client); $pushGateway->push($registry, 'my_job', array('instance' => 'foo')); $httpClient = new Client();