forked from endclothing/prometheus_client_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request endclothing#28 from Jimdo/pushgateway
Add PushGateway implementation
- Loading branch information
Showing
9 changed files
with
175 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Prometheus\CollectorRegistry; | ||
|
||
$adapter = new Prometheus\Storage\APC(); | ||
$registry = new CollectorRegistry($adapter); | ||
|
||
$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']); | ||
$counter->incBy(6, ['blue']); | ||
|
||
$pushGateway = new \Prometheus\PushGateway('192.168.59.100:9091'); | ||
$pushGateway->push($registry, 'my_job', array('instance'=>'foo')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
apc.enable_cli = On |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
|
||
namespace Prometheus; | ||
|
||
|
||
use GuzzleHttp\Client; | ||
|
||
class PushGateway | ||
{ | ||
private $address; | ||
|
||
/** | ||
* PushGateway constructor. | ||
* @param $address string host:port of the push gateway | ||
*/ | ||
public function __construct($address) | ||
{ | ||
$this->address = $address; | ||
} | ||
|
||
/** | ||
* Pushes all metrics in a Collector, replacing all those with the same job. | ||
* Uses HTTP PUT. | ||
* @param CollectorRegistry $collectorRegistry | ||
* @param $job | ||
* @param $groupingKey | ||
*/ | ||
public function push(CollectorRegistry $collectorRegistry, $job, $groupingKey = null) | ||
{ | ||
$this->doRequest($collectorRegistry, $job, $groupingKey, 'put'); | ||
} | ||
|
||
/** | ||
* Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name and job. | ||
* Uses HTTP POST. | ||
* @param CollectorRegistry $collectorRegistry | ||
* @param $job | ||
* @param $groupingKey | ||
*/ | ||
public function pushAdd(CollectorRegistry $collectorRegistry, $job, $groupingKey = null) | ||
{ | ||
$this->doRequest($collectorRegistry, $job, $groupingKey, 'post'); | ||
} | ||
|
||
/** | ||
* Deletes metrics from the Pushgateway. | ||
* Uses HTTP POST. | ||
* @param $job | ||
* @param $groupingKey | ||
*/ | ||
public function delete($job, $groupingKey = null) | ||
{ | ||
$this->doRequest(null, $job, $groupingKey, 'delete'); | ||
} | ||
|
||
/** | ||
* @param CollectorRegistry $collectorRegistry | ||
* @param $job | ||
* @param $groupingKey | ||
* @param $method | ||
*/ | ||
private function doRequest(CollectorRegistry $collectorRegistry, $job, $groupingKey, $method) | ||
{ | ||
$url = "http://" . $this->address . "/metrics/job/" . $job; | ||
if (!empty($groupingKey)) { | ||
foreach ($groupingKey as $label => $value) { | ||
$url .= "/" . $label . "/" . $value; | ||
} | ||
} | ||
$client = new Client(); | ||
$requestOptions = array( | ||
'headers' => array( | ||
'Content-Type' => RenderTextFormat::MIME_TYPE | ||
), | ||
'connect_timeout' => 10, | ||
'timeout' => 20, | ||
); | ||
if ($method != 'delete') { | ||
$renderer = new RenderTextFormat(); | ||
$requestOptions['body'] = $renderer->render($collectorRegistry->getMetricFamilySamples()); | ||
} | ||
$response = $client->request($method, $url, $requestOptions); | ||
$statusCode = $response->getStatusCode(); | ||
if ($statusCode != 202) { | ||
$msg = "Unexpected status code " . $statusCode . " received from pushgateway " . $this->address . ": " . $response->getBody(); | ||
throw new \RuntimeException($msg); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
namespace Test; | ||
|
||
use GuzzleHttp\Client; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
use Prometheus\CollectorRegistry; | ||
use Prometheus\PushGateway; | ||
use Prometheus\Storage\APC; | ||
|
||
class BlackBoxPushGatewayTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function pushGatewayShouldWork() | ||
{ | ||
$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->push($registry, 'my_job', array('instance' => 'foo')); | ||
|
||
$httpClient = new Client(); | ||
$metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents(); | ||
$this->assertContains( | ||
'# HELP test_some_counter it increases | ||
# TYPE test_some_counter counter | ||
test_some_counter{instance="foo",job="my_job",type="blue"} 6', | ||
$metrics | ||
); | ||
|
||
$pushGateway->delete('my_job', array('instance' => 'foo')); | ||
|
||
$httpClient = new Client(); | ||
$metrics = $httpClient->get("http://pushgateway:9091/metrics")->getBody()->getContents(); | ||
$this->assertNotContains( | ||
'# HELP test_some_counter it increases | ||
# TYPE test_some_counter counter | ||
test_some_counter{instance="foo",job="my_job",type="blue"} 6', | ||
$metrics | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters