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

Add transport method to url #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'];
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using yield here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data providers do not indeed need an array as a return, but an iterator can fit better for making them a little more readable.

/**
* @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', null, $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