Skip to content
This repository was archived by the owner on Oct 22, 2019. It is now read-only.

Commit 4668eeb

Browse files
kswzrNoelDavies
authored andcommitted
Add support for prometheus/pushgateway:>=0.10 (#17)
* allow status 202 in case of downwards compatibility * add allowance of status 200 for prometheus/pushgateway >=0.10.0 * fix grouping key, because of not matching declerations * simplify if to add more status code easier
1 parent 63966cd commit 4668eeb

2 files changed

Lines changed: 96 additions & 8 deletions

File tree

src/Prometheus/PushGateway.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Prometheus;
55

66
use GuzzleHttp\Client;
7+
use GuzzleHttp\ClientInterface;
78
use GuzzleHttp\Exception\GuzzleException;
89
use RuntimeException;
910

@@ -14,13 +15,20 @@ class PushGateway
1415
*/
1516
private $address;
1617

18+
/**
19+
* @var ClientInterface
20+
*/
21+
private $client;
22+
1723
/**
1824
* PushGateway constructor.
19-
* @param $address string host:port of the push gateway
25+
* @param string $address host:port of the push gateway
26+
* @param ClientInterface $client
2027
*/
21-
public function __construct($address)
28+
public function __construct($address, ClientInterface $client = null)
2229
{
2330
$this->address = $address;
31+
$this->client = $client ?? new Client();
2432
}
2533

2634
/**
@@ -31,7 +39,7 @@ public function __construct($address)
3139
* @param array $groupingKey
3240
* @throws GuzzleException
3341
*/
34-
public function push(CollectorRegistry $collectorRegistry, string $job, array $groupingKey = null): void
42+
public function push(CollectorRegistry $collectorRegistry, string $job, array $groupingKey = []): void
3543
{
3644
$this->doRequest($collectorRegistry, $job, $groupingKey, 'put');
3745
}
@@ -44,7 +52,7 @@ public function push(CollectorRegistry $collectorRegistry, string $job, array $g
4452
* @param $groupingKey
4553
* @throws GuzzleException
4654
*/
47-
public function pushAdd(CollectorRegistry $collectorRegistry, string $job, array $groupingKey = null): void
55+
public function pushAdd(CollectorRegistry $collectorRegistry, string $job, array $groupingKey = []): void
4856
{
4957
$this->doRequest($collectorRegistry, $job, $groupingKey, 'post');
5058
}
@@ -56,7 +64,7 @@ public function pushAdd(CollectorRegistry $collectorRegistry, string $job, array
5664
* @param array $groupingKey
5765
* @throws GuzzleException
5866
*/
59-
public function delete(string $job, array $groupingKey = null): void
67+
public function delete(string $job, array $groupingKey = []): void
6068
{
6169
$this->doRequest(null, $job, $groupingKey, 'delete');
6270
}
@@ -76,21 +84,22 @@ private function doRequest(CollectorRegistry $collectorRegistry, string $job, ar
7684
$url .= "/" . $label . "/" . $value;
7785
}
7886
}
79-
$client = new Client();
87+
8088
$requestOptions = [
8189
'headers' => [
8290
'Content-Type' => RenderTextFormat::MIME_TYPE,
8391
],
8492
'connect_timeout' => 10,
8593
'timeout' => 20,
8694
];
95+
8796
if ($method != 'delete') {
8897
$renderer = new RenderTextFormat();
8998
$requestOptions['body'] = $renderer->render($collectorRegistry->getMetricFamilySamples());
9099
}
91-
$response = $client->request($method, $url, $requestOptions);
100+
$response = $this->client->request($method, $url, $requestOptions);
92101
$statusCode = $response->getStatusCode();
93-
if ($statusCode != 202) {
102+
if (!in_array($statusCode, [200, 202])) {
94103
$msg = "Unexpected status code "
95104
. $statusCode
96105
. " received from push gateway "
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Test\Prometheus;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Handler\MockHandler;
7+
use GuzzleHttp\HandlerStack;
8+
use GuzzleHttp\Psr7\Response;
9+
use PHPUnit\Framework\TestCase;
10+
use Prometheus\CollectorRegistry;
11+
use Prometheus\MetricFamilySamples;
12+
use Prometheus\PushGateway;
13+
14+
class PushGatewayTest extends TestCase
15+
{
16+
/**
17+
* @test
18+
*
19+
* @doesNotPerformAssertions
20+
*/
21+
public function validResponseShouldNotThrowException(): void
22+
{
23+
$mockedCollectorRegistry = $this->createMock(CollectorRegistry::class);
24+
$mockedCollectorRegistry->method('getMetricFamilySamples')->with()->willReturn([
25+
$this->createMock(MetricFamilySamples::class)
26+
]);
27+
28+
$mockHandler = new MockHandler([
29+
new Response(200),
30+
new Response(202),
31+
]);
32+
$handler = HandlerStack::create($mockHandler);
33+
$client = new Client(['handler' => $handler]);
34+
35+
$pushGateway = new PushGateway('http://foo.bar', $client);
36+
$pushGateway->push($mockedCollectorRegistry, 'foo');
37+
}
38+
39+
/**
40+
* @test
41+
*
42+
* @doesNotPerformAnyAssertions
43+
*/
44+
public function invalidResponseShouldThrowRuntimeException(): void
45+
{
46+
$this->expectException(\RuntimeException::class);
47+
48+
$mockedCollectorRegistry = $this->createMock(CollectorRegistry::class);
49+
$mockedCollectorRegistry->method('getMetricFamilySamples')->with()->willReturn([
50+
$this->createMock(MetricFamilySamples::class)
51+
]);
52+
53+
$mockHandler = new MockHandler([
54+
new Response(201),
55+
new Response(300),
56+
]);
57+
$handler = HandlerStack::create($mockHandler);
58+
$client = new Client(['handler' => $handler]);
59+
60+
$pushGateway = new PushGateway('http://foo.bar', $client);
61+
$pushGateway->push($mockedCollectorRegistry, 'foo');
62+
}
63+
64+
/**
65+
* @test
66+
*/
67+
public function clientGetsDefinedIfNotSpecified(): void
68+
{
69+
$this->expectException(\RuntimeException::class);
70+
71+
$mockedCollectorRegistry = $this->createMock(CollectorRegistry::class);
72+
$mockedCollectorRegistry->method('getMetricFamilySamples')->with()->willReturn([
73+
$this->createMock(MetricFamilySamples::class)
74+
]);
75+
76+
$pushGateway = new PushGateway('http://foo.bar');
77+
$pushGateway->push($mockedCollectorRegistry, 'foo');
78+
}
79+
}

0 commit comments

Comments
 (0)