Skip to content
This repository was archived by the owner on Oct 22, 2019. It is now read-only.
Closed
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
26 changes: 17 additions & 9 deletions src/Prometheus/PushGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@


use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;

class PushGateway
{
/** @var \Psr\Http\Message\UriInterface */
private $address;
/** @var string */
private $hostPort;

/**
* PushGateway constructor.
* @param $address string host:port of the push gateway
*/
public function __construct($address)
{
$this->address = $address;
$this->address = Uri::fromParts(array_merge(['scheme' => 'http'], parse_url($address)));
$this->hostPort = $this->address->getHost() . ':' . $this->address->getPort();
}

/**
Expand Down Expand Up @@ -62,28 +67,31 @@ public function delete($job, $groupingKey = null)
*/
private function doRequest(CollectorRegistry $collectorRegistry, $job, $groupingKey, $method)
{
$url = "http://" . $this->address . "/metrics/job/" . $job;
$path = '/metrics/job/' . $job;
if (!empty($groupingKey)) {
foreach ($groupingKey as $label => $value) {
$url .= "/" . $label . "/" . $value;
$path .= '/' . $label . '/' . $value;
}
}

$url = $this->address->withPath($path);

$client = new Client();
$requestOptions = array(
'headers' => array(
'Content-Type' => RenderTextFormat::MIME_TYPE
),
$requestOptions = [
'headers' => [
'Content-Type' => RenderTextFormat::MIME_TYPE,
],
'connect_timeout' => 10,
'timeout' => 20,
);
];
if ($method != 'delete') {
$renderer = new RenderTextFormat();
$requestOptions['body'] = $renderer->render($collectorRegistry->getMetricFamilySamples());
}
$response = $client->request($method, $url, $requestOptions);
$statusCode = $response->getStatusCode();
if ($statusCode != 202) {
$msg = "Unexpected status code " . $statusCode . " received from pushgateway " . $this->address . ": " . $response->getBody();
$msg = 'Unexpected status code ' . $statusCode . ' received from pushgateway ' . $this->hostPort . ': ' . $response->getBody();
throw new \RuntimeException($msg);
}
}
Expand Down