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
110 lines (98 loc) · 3.29 KB
/
PushGateway.php
File metadata and controls
110 lines (98 loc) · 3.29 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
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Prometheus;
use GuzzleHttp\Client;
class PushGateway
{
/**
* @var Client $client
*/
private $client;
/**
* PushGateway constructor.
* @param Client $client client with configured push gateway base_uri param, example uri: http://pushgateway.com/metrics/job/
*/
public function __construct(Client $client)
{
$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 string $job
* @param array|null $groupingKey
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function push(CollectorRegistry $collectorRegistry, $job, $groupingKey = null)
{
$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 string $job
* @param array|null $groupingKey
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function pushAdd(CollectorRegistry $collectorRegistry, $job, $groupingKey = null)
{
$this->doRequest('post', $job, $groupingKey, $collectorRegistry);
}
/**
* Deletes metrics from the Pushgateway.
* Uses HTTP POST.
* @param string $job
* @param array|null $groupingKey
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function delete($job, $groupingKey = null)
{
$this->doRequest('delete', $job, $groupingKey);
}
/**
* @param string $method
* @param string $job
* @param array|null $groupingKey
* @param CollectorRegistry|null $collectorRegistry
* @throws \GuzzleHttp\Exception\GuzzleException
*/
private function doRequest($method, $job, $groupingKey, CollectorRegistry $collectorRegistry = null)
{
$url = $job;
if (!empty($groupingKey)) {
foreach ($groupingKey as $label => $value) {
$url .= '/' . $label . '/' . $value;
}
}
$requestOptions = array(
'headers' => array(
'Content-Type' => RenderTextFormat::MIME_TYPE
),
'connect_timeout' => 10,
'timeout' => 20,
);
if ($method !== 'delete') {
if ($collectorRegistry === null) {
throw new \RuntimeException('CollectorRegistry not set');
}
$renderer = new RenderTextFormat();
$requestOptions['body'] = $renderer->render($collectorRegistry->getMetricFamilySamples());
}
$response = $this->client->request($method, $url, $requestOptions);
$statusCode = $response->getStatusCode();
if ($statusCode !== 202) {
$msg = "Unexpected status code {$statusCode} received from pushgateway body: {$response->getBody()}";
throw new \RuntimeException($msg);
}
}
}