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