This repository was archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathPushGateway.php
More file actions
99 lines (86 loc) · 2.87 KB
/
PushGateway.php
File metadata and controls
99 lines (86 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Prometheus;
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 = Uri::fromParts(array_merge(['scheme' => 'http'], parse_url($address)));
$this->hostPort = $this->address->getHost() . ':' . $this->address->getPort();
}
/**
* Pushes all metrics in a Collector, replacing all those with the same job.
* Uses HTTP PUT.
* @param CollectorRegistry $collectorRegistry
* @param $job
* @param $groupingKey
*/
public function push(CollectorRegistry $collectorRegistry, $job, $groupingKey = null)
{
$this->doRequest($collectorRegistry, $job, $groupingKey, 'put');
}
/**
* 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
*/
public function pushAdd(CollectorRegistry $collectorRegistry, $job, $groupingKey = null)
{
$this->doRequest($collectorRegistry, $job, $groupingKey, 'post');
}
/**
* Deletes metrics from the Pushgateway.
* Uses HTTP POST.
* @param $job
* @param $groupingKey
*/
public function delete($job, $groupingKey = null)
{
$this->doRequest(null, $job, $groupingKey, 'delete');
}
/**
* @param CollectorRegistry $collectorRegistry
* @param $job
* @param $groupingKey
* @param $method
*/
private function doRequest(CollectorRegistry $collectorRegistry, $job, $groupingKey, $method)
{
$path = '/metrics/job/' . $job;
if (!empty($groupingKey)) {
foreach ($groupingKey as $label => $value) {
$path .= '/' . $label . '/' . $value;
}
}
$url = $this->address->withPath($path);
$client = new Client();
$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->hostPort . ': ' . $response->getBody();
throw new \RuntimeException($msg);
}
}
}