Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Refactor PushGateway client to support http client configuration #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/pushgateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
69 changes: 44 additions & 25 deletions src/Prometheus/PushGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,101 @@

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
),
'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);
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Test/BlackBoxPushGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down