Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for HTTPS Pushgateway endpoints #2

Merged
merged 1 commit into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Let's assume you have that simple counter and want to send it to your PushGatewa
->inc();

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

Expand All @@ -44,6 +44,11 @@ Just start the PushGateway by using docker-compose
docker-compose up
```

Use composer to grab all dependencies
```
docker run --rm --interactive --tty --volume $PWD:/app composer install
```

Execute the tests:
```
docker-compose run phpunit vendor/bin/phpunit tests/Test/
Expand Down
2 changes: 1 addition & 1 deletion examples/pushgateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy(6, ['blue']);

$pushGateway = new PushGateway('192.168.59.100:9091');
$pushGateway = new PushGateway('http://192.168.59.100:9091');
$pushGateway->push($registry, 'my_job', ['instance' => 'foo']);
7 changes: 7 additions & 0 deletions php-fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM php:7.2-fpm

RUN pecl install redis-5.3.1 && docker-php-ext-enable redis
RUN pecl install apcu-5.1.19 && docker-php-ext-enable apcu

COPY www.conf /usr/local/etc/php-fpm.d/
COPY docker-php-ext-apcu-cli.ini /usr/local/etc/php/conf.d/
1 change: 1 addition & 0 deletions php-fpm/docker-php-ext-apcu-cli.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apc.enable_cli = On
6 changes: 6 additions & 0 deletions php-fpm/www.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
pm = static
pm.max_children = 20
10 changes: 5 additions & 5 deletions src/PrometheusPushGateway/PushGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class PushGateway

/**
* PushGateway constructor.
* @param string $address host:port of the push gateway
* @param string $address (http|https)://host:port of the push gateway
* @param ClientInterface $client
*/
public function __construct($address, ClientInterface $client = null)
{
$this->address = $address;
$this->address = strpos($address, 'http') === false ? 'http://' . $address : $address;
LKaemmerling marked this conversation as resolved.
Show resolved Hide resolved
$this->client = $client ?? new Client();
}

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

/**
* @param CollectorRegistry $collectorRegistry
* @param CollectorRegistry|null $collectorRegistry
* @param string $job
* @param array $groupingKey
* @param string $method
* @throws GuzzleException
*/
private function doRequest(CollectorRegistry $collectorRegistry, string $job, array $groupingKey, $method): void
private function doRequest(?CollectorRegistry $collectorRegistry, string $job, array $groupingKey, $method): void
LKaemmerling marked this conversation as resolved.
Show resolved Hide resolved
{
$url = "http://" . $this->address . "/metrics/job/" . $job;
$url = $this->address . "/metrics/job/" . $job;
if (!empty($groupingKey)) {
foreach ($groupingKey as $label => $value) {
$url .= "/" . $label . "/" . $value;
Expand Down
5 changes: 3 additions & 2 deletions tests/Test/BlackBoxPushGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ public function pushGatewayShouldWork()

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

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

$httpClient = new Client();
$metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents();
$this->assertNotContains(
$this->assertStringNotContainsString(
'# HELP test_some_counter it increases
# TYPE test_some_counter counter
test_some_counter{instance="foo",job="my_job",type="blue"} 6',
Expand Down
37 changes: 37 additions & 0 deletions tests/Test/PrometheusPushGateway/PushGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,41 @@ public function clientGetsDefinedIfNotSpecified(): void
$pushGateway = new PushGateway('http://foo.bar');
$pushGateway->push($mockedCollectorRegistry, 'foo');
}

/**
* @test
*
* @dataProvider validAddressAndRequestsProvider
*/
public function validAddressShouldCreateValidRequests(string $address, string $scheme, string $host, int $port): void
{
$mockedCollectorRegistry = $this->createMock(CollectorRegistry::class);
$mockedCollectorRegistry->method('getMetricFamilySamples')->with()->willReturn([
$this->createMock(MetricFamilySamples::class)
]);

$mockHandler = new MockHandler([
new Response(200),
]);
$handler = HandlerStack::create($mockHandler);
$client = new Client(['handler' => $handler]);

$pushGateway = new PushGateway($address, $client);
$pushGateway->push($mockedCollectorRegistry, 'foo');

$uri = $mockHandler->getLastRequest()->getUri();
$this->assertEquals($scheme, $uri->getScheme());
$this->assertEquals($host, $uri->getHost());
$this->assertEquals($port, $uri->getPort());
$this->assertEquals('/metrics/job/foo', $uri->getPath());
}

public function validAddressAndRequestsProvider()
{
return [
['foo.bar:123', 'http', 'foo.bar', 123],
['http://foo.bar:123', 'http', 'foo.bar', 123],
['https://foo.bar:123', 'https', 'foo.bar', 123],
];
}
}