1313
1414class PushGateway
1515{
16+ const HTTP_PUT = "PUT " ;
17+ const HTTP_POST = "POST " ;
18+ const HTTP_DELETE = "DELETE " ;
1619 /**
1720 * @var string
1821 */
@@ -26,9 +29,9 @@ class PushGateway
2629 /**
2730 * PushGateway constructor.
2831 * @param string $address (http|https)://host:port of the push gateway
29- * @param ClientInterface $client
32+ * @param ClientInterface|null $client
3033 */
31- public function __construct ($ address , ClientInterface $ client = null )
34+ public function __construct (string $ address , ? ClientInterface $ client = null )
3235 {
3336 $ this ->address = strpos ($ address , 'http ' ) === false ? 'http:// ' . $ address : $ address ;
3437 $ this ->client = $ client ?? new Client ();
@@ -39,50 +42,50 @@ public function __construct($address, ClientInterface $client = null)
3942 * Uses HTTP PUT.
4043 * @param CollectorRegistry $collectorRegistry
4144 * @param string $job
42- * @param array $groupingKey
45+ * @param array<string> $groupingKey
4346 * @throws GuzzleException
4447 */
4548 public function push (CollectorRegistry $ collectorRegistry , string $ job , array $ groupingKey = []): void
4649 {
47- $ this ->doRequest ($ collectorRegistry , $ job , $ groupingKey , ' put ' );
50+ $ this ->doRequest ($ collectorRegistry , $ job , $ groupingKey , self :: HTTP_PUT );
4851 }
4952
5053 /**
5154 * Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name and job.
5255 * Uses HTTP POST.
5356 * @param CollectorRegistry $collectorRegistry
54- * @param $job
55- * @param $groupingKey
57+ * @param string $job
58+ * @param array<string> $groupingKey
5659 * @throws GuzzleException
5760 */
5861 public function pushAdd (CollectorRegistry $ collectorRegistry , string $ job , array $ groupingKey = []): void
5962 {
60- $ this ->doRequest ($ collectorRegistry , $ job , $ groupingKey , ' post ' );
63+ $ this ->doRequest ($ collectorRegistry , $ job , $ groupingKey , self :: HTTP_POST );
6164 }
6265
6366 /**
6467 * Deletes metrics from the Push Gateway.
6568 * Uses HTTP POST.
6669 * @param string $job
67- * @param array $groupingKey
70+ * @param array<string> $groupingKey
6871 * @throws GuzzleException
6972 */
7073 public function delete (string $ job , array $ groupingKey = []): void
7174 {
72- $ this ->doRequest (null , $ job , $ groupingKey , ' delete ' );
75+ $ this ->doRequest (null , $ job , $ groupingKey , self :: HTTP_DELETE );
7376 }
7477
7578 /**
7679 * @param CollectorRegistry|null $collectorRegistry
7780 * @param string $job
78- * @param array $groupingKey
81+ * @param array<string> $groupingKey
7982 * @param string $method
8083 * @throws GuzzleException
8184 */
82- private function doRequest (?CollectorRegistry $ collectorRegistry , string $ job , array $ groupingKey , $ method ): void
85+ private function doRequest (?CollectorRegistry $ collectorRegistry , string $ job , array $ groupingKey , string $ method ): void
8386 {
8487 $ url = $ this ->address . "/metrics/job/ " . $ job ;
85- if (! empty ( $ groupingKey) ) {
88+ if ($ groupingKey !== [] ) {
8689 foreach ($ groupingKey as $ label => $ value ) {
8790 $ url .= "/ " . $ label . "/ " . $ value ;
8891 }
@@ -96,13 +99,13 @@ private function doRequest(?CollectorRegistry $collectorRegistry, string $job, a
9699 'timeout ' => 20 ,
97100 ];
98101
99- if ($ method != ' delete ' ) {
102+ if ($ method !== self :: HTTP_DELETE && $ collectorRegistry !== null ) {
100103 $ renderer = new RenderTextFormat ();
101104 $ requestOptions ['body ' ] = $ renderer ->render ($ collectorRegistry ->getMetricFamilySamples ());
102105 }
103106 $ response = $ this ->client ->request ($ method , $ url , $ requestOptions );
104107 $ statusCode = $ response ->getStatusCode ();
105- if (!in_array ($ statusCode , [200 , 202 ])) {
108+ if (!in_array ($ statusCode , [200 , 202 ], true )) {
106109 $ msg = "Unexpected status code "
107110 . $ statusCode
108111 . " received from push gateway "
0 commit comments