1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Krenor\Prometheus; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Psr7\Utils; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use Krenor\Prometheus\Renderer\TextRenderer; |
9
|
|
|
|
10
|
|
|
class PushGateway |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* PushGateway constructor. |
14
|
|
|
* |
15
|
|
|
* @param Client $client |
16
|
|
|
* @param CollectorRegistry $registry |
17
|
|
|
*/ |
18
|
3 |
|
public function __construct(protected Client $client, protected CollectorRegistry $registry) |
19
|
|
|
{ |
20
|
|
|
// |
21
|
3 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $job |
25
|
|
|
* @param string|null $instance |
26
|
|
|
* |
27
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
28
|
|
|
* |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
1 |
|
public function add(string $job, ?string $instance = null): bool |
32
|
|
|
{ |
33
|
1 |
|
return $this->request('POST', ...func_get_args()); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $job |
38
|
|
|
* @param string|null $instance |
39
|
|
|
* |
40
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
1 |
|
public function replace(string $job, ?string $instance = null): bool |
45
|
|
|
{ |
46
|
1 |
|
return $this->request('PUT', ...func_get_args()); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $job |
51
|
|
|
* @param string|null $instance |
52
|
|
|
* |
53
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
1 |
|
public function delete(string $job, ?string $instance = null): bool |
58
|
|
|
{ |
59
|
1 |
|
return $this->request('DELETE', ...func_get_args()); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $method |
64
|
|
|
* @param string $job |
65
|
|
|
* @param string|null $instance |
66
|
|
|
* |
67
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
3 |
|
protected function request(string $method, string $job, ?string $instance = null): bool |
72
|
|
|
{ |
73
|
3 |
|
$request = new Request($method, $instance === null ? "job/{$job}" : "job/{$job}/instance/{$instance}"); |
74
|
3 |
|
$options = [ |
75
|
|
|
'headers' => [ |
76
|
|
|
'Content-Type' => TextRenderer::CONTENT_TYPE, |
77
|
|
|
], |
78
|
|
|
]; |
79
|
|
|
|
80
|
3 |
|
if ($method !== 'DELETE') { |
81
|
2 |
|
$options['body'] = Utils::streamFor( |
82
|
2 |
|
(new TextRenderer) |
83
|
2 |
|
->render($this->registry->collect()) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this |
88
|
3 |
|
->client |
89
|
3 |
|
->send($request, $options) |
90
|
3 |
|
->getStatusCode() === 200; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|