Skip to content

Commit 09e7574

Browse files
authored
Merge pull request #2 from fourth/https_support
Added support for HTTPS Pushgateway endpoints
2 parents 310f9eb + 3c33519 commit 09e7574

File tree

8 files changed

+66
-9
lines changed

8 files changed

+66
-9
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Let's assume you have that simple counter and want to send it to your PushGatewa
2424
->inc();
2525

2626
// Now send it to the PushGateway:
27-
$pushGateway = new \PrometheusPushGateway\PushGateway('192.168.59.100:9091');
27+
$pushGateway = new \PrometheusPushGateway\PushGateway('http://192.168.59.100:9091');
2828
$pushGateway->push(\Prometheus\CollectorRegistry::getDefault(), 'my_job', ['instance' => 'foo']);
2929
```
3030

@@ -44,6 +44,11 @@ Just start the PushGateway by using docker-compose
4444
docker-compose up
4545
```
4646

47+
Use composer to grab all dependencies
48+
```
49+
docker run --rm --interactive --tty --volume $PWD:/app composer install
50+
```
51+
4752
Execute the tests:
4853
```
4954
docker-compose run phpunit vendor/bin/phpunit tests/Test/

examples/pushgateway.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
2323
$counter->incBy(6, ['blue']);
2424

25-
$pushGateway = new PushGateway('192.168.59.100:9091');
25+
$pushGateway = new PushGateway('http://192.168.59.100:9091');
2626
$pushGateway->push($registry, 'my_job', ['instance' => 'foo']);

php-fpm/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM php:7.2-fpm
2+
3+
RUN pecl install redis-5.3.1 && docker-php-ext-enable redis
4+
RUN pecl install apcu-5.1.19 && docker-php-ext-enable apcu
5+
6+
COPY www.conf /usr/local/etc/php-fpm.d/
7+
COPY docker-php-ext-apcu-cli.ini /usr/local/etc/php/conf.d/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
apc.enable_cli = On

php-fpm/www.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[www]
2+
user = www-data
3+
group = www-data
4+
listen = 127.0.0.1:9000
5+
pm = static
6+
pm.max_children = 20

src/PrometheusPushGateway/PushGateway.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ class PushGateway
2525

2626
/**
2727
* PushGateway constructor.
28-
* @param string $address host:port of the push gateway
28+
* @param string $address (http|https)://host:port of the push gateway
2929
* @param ClientInterface $client
3030
*/
3131
public function __construct($address, ClientInterface $client = null)
3232
{
33-
$this->address = $address;
33+
$this->address = strpos($address, 'http') === false ? 'http://' . $address : $address;
3434
$this->client = $client ?? new Client();
3535
}
3636

@@ -73,15 +73,15 @@ public function delete(string $job, array $groupingKey = []): void
7373
}
7474

7575
/**
76-
* @param CollectorRegistry $collectorRegistry
76+
* @param CollectorRegistry|null $collectorRegistry
7777
* @param string $job
7878
* @param array $groupingKey
7979
* @param string $method
8080
* @throws GuzzleException
8181
*/
82-
private function doRequest(CollectorRegistry $collectorRegistry, string $job, array $groupingKey, $method): void
82+
private function doRequest(?CollectorRegistry $collectorRegistry, string $job, array $groupingKey, $method): void
8383
{
84-
$url = "http://" . $this->address . "/metrics/job/" . $job;
84+
$url = $this->address . "/metrics/job/" . $job;
8585
if (!empty($groupingKey)) {
8686
foreach ($groupingKey as $label => $value) {
8787
$url .= "/" . $label . "/" . $value;

tests/Test/BlackBoxPushGatewayTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ public function pushGatewayShouldWork()
2626

2727
$httpClient = new Client();
2828
$metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents();
29-
$this->assertContains(
29+
$this->assertStringContainsString(
3030
'# HELP test_some_counter it increases
3131
# TYPE test_some_counter counter
3232
test_some_counter{instance="foo",job="my_job",type="blue"} 6',
3333
$metrics
3434
);
3535

36+
$pushGateway = new PushGateway('http://pushgateway:9091');
3637
$pushGateway->delete('my_job', ['instance' => 'foo']);
3738

3839
$httpClient = new Client();
3940
$metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents();
40-
$this->assertNotContains(
41+
$this->assertStringNotContainsString(
4142
'# HELP test_some_counter it increases
4243
# TYPE test_some_counter counter
4344
test_some_counter{instance="foo",job="my_job",type="blue"} 6',

tests/Test/PrometheusPushGateway/PushGatewayTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,41 @@ public function clientGetsDefinedIfNotSpecified(): void
7676
$pushGateway = new PushGateway('http://foo.bar');
7777
$pushGateway->push($mockedCollectorRegistry, 'foo');
7878
}
79+
80+
/**
81+
* @test
82+
*
83+
* @dataProvider validAddressAndRequestsProvider
84+
*/
85+
public function validAddressShouldCreateValidRequests(string $address, string $scheme, string $host, int $port): void
86+
{
87+
$mockedCollectorRegistry = $this->createMock(CollectorRegistry::class);
88+
$mockedCollectorRegistry->method('getMetricFamilySamples')->with()->willReturn([
89+
$this->createMock(MetricFamilySamples::class)
90+
]);
91+
92+
$mockHandler = new MockHandler([
93+
new Response(200),
94+
]);
95+
$handler = HandlerStack::create($mockHandler);
96+
$client = new Client(['handler' => $handler]);
97+
98+
$pushGateway = new PushGateway($address, $client);
99+
$pushGateway->push($mockedCollectorRegistry, 'foo');
100+
101+
$uri = $mockHandler->getLastRequest()->getUri();
102+
$this->assertEquals($scheme, $uri->getScheme());
103+
$this->assertEquals($host, $uri->getHost());
104+
$this->assertEquals($port, $uri->getPort());
105+
$this->assertEquals('/metrics/job/foo', $uri->getPath());
106+
}
107+
108+
public function validAddressAndRequestsProvider()
109+
{
110+
return [
111+
['foo.bar:123', 'http', 'foo.bar', 123],
112+
['http://foo.bar:123', 'http', 'foo.bar', 123],
113+
['https://foo.bar:123', 'https', 'foo.bar', 123],
114+
];
115+
}
79116
}

0 commit comments

Comments
 (0)