Skip to content

Commit

Permalink
Add transport method to url
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Nóbrega committed Jan 28, 2020
1 parent 65662f5 commit 82218c8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
43 changes: 32 additions & 11 deletions src/Prometheus/PushGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,42 @@

class PushGateway
{
const ALLOWED_TRANSPORT_METHODS = [
'http',
'https',
];

/**
* @var string
*/
private $address;

/**
* @var string
*/
private $transport;

/**
* @var ClientInterface
*/
private $client;

/**
* PushGateway constructor.
* @param string $address host:port of the push gateway
* @param $address string host:port of the push gateway
* @param ClientInterface $client
* @param $transport string transport method of the push gateway
*/
public function __construct($address, ClientInterface $client = null)
public function __construct($address, ClientInterface $client = null, $transport = 'http')
{
$this->address = $address;
$this->client = $client ?? new Client();

if (!in_array($transport, self::ALLOWED_TRANSPORT_METHODS)) {
throw new \InvalidArgumentException(\sprintf('Invalid transport "%s"', $transport));
}

$this->transport = $transport;
}

/**
Expand All @@ -42,7 +59,7 @@ public function __construct($address, ClientInterface $client = null)
*/
public function push(CollectorRegistry $collectorRegistry, string $job, array $groupingKey = []): void
{
$this->doRequest($collectorRegistry, $job, $groupingKey, 'put');
$this->doRequest($collectorRegistry, $job, 'put', $groupingKey);
}

/**
Expand All @@ -55,7 +72,7 @@ public function push(CollectorRegistry $collectorRegistry, string $job, array $g
*/
public function pushAdd(CollectorRegistry $collectorRegistry, string $job, array $groupingKey = []): void
{
$this->doRequest($collectorRegistry, $job, $groupingKey, 'post');
$this->doRequest($collectorRegistry, $job, 'post', $groupingKey);
}

/**
Expand All @@ -67,7 +84,7 @@ public function pushAdd(CollectorRegistry $collectorRegistry, string $job, array
*/
public function delete(string $job, array $groupingKey = []): void
{
$this->doRequest(null, $job, $groupingKey, 'delete');
$this->doRequest(null, $job, 'delete', $groupingKey);
}

/**
Expand All @@ -77,13 +94,17 @@ public function delete(string $job, array $groupingKey = []): void
* @param string $method
* @throws GuzzleException
*/
private function doRequest(CollectorRegistry $collectorRegistry, string $job, array $groupingKey, $method): void
private function doRequest(CollectorRegistry $collectorRegistry, string $job, string $method, array $groupingKey = []): void
{
$url = "http://" . $this->address . "/metrics/job/" . $job;
if (!empty($groupingKey)) {
foreach ($groupingKey as $label => $value) {
$url .= "/" . $label . "/" . $value;
}
$url = \sprintf(
"%s://%s/metrics/job/%s",
$this->transport,
$this->address,
$job
);

foreach ($groupingKey as $label => $value) {
$url .= "/" . $label . "/" . $value;
}

$requestOptions = [
Expand Down
19 changes: 13 additions & 6 deletions tests/Test/BlackBoxPushGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,29 @@

class BlackBoxPushGatewayTest extends TestCase
{
public function transportProvider()
{
yield ['http'];
yield ['https'];
}

/**
* @dataProvider transportProvider
* @test
*/
public function pushGatewayShouldWork()
public function pushGatewayShouldWork(string $transport)
{
$adapter = new APC();
$registry = new CollectorRegistry($adapter);

$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy(6, ['blue']);

$pushGateway = new PushGateway('pushgateway:9091');
$pushGateway = new PushGateway('pushgateway:9091', $transport);
$pushGateway->push($registry, 'my_job', ['instance' => 'foo']);

$httpClient = new Client();
$metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents();
$client = new Client();
$metrics = $client->get($transport . "://pushgateway:9091/metrics")->getBody()->getContents();
$this->assertContains(
'# HELP test_some_counter it increases
# TYPE test_some_counter counter
Expand All @@ -35,8 +42,8 @@ public function pushGatewayShouldWork()

$pushGateway->delete('my_job', ['instance' => 'foo']);

$httpClient = new Client();
$metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents();
$client = new Client();
$metrics = $client->get($transport . "://pushgateway:9091/metrics")->getBody()->getContents();
$this->assertNotContains(
'# HELP test_some_counter it increases
# TYPE test_some_counter counter
Expand Down
2 changes: 1 addition & 1 deletion tests/Test/BlackBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BlackBoxTest extends TestCase
*/
private $adapter;

public function setUp()
public function setUp() : void
{
$this->adapter = getenv('ADAPTER');
$this->client = new Client(['base_uri' => 'http://nginx:80/']);
Expand Down

0 comments on commit 82218c8

Please sign in to comment.