Skip to content

Commit 4112b45

Browse files
committed
Initial commit
0 parents  commit 4112b45

File tree

7 files changed

+215
-0
lines changed

7 files changed

+215
-0
lines changed

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2014-2016 Philip Lehmann-Böhm [email protected]
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "philiplb/phpprom",
3+
"type": "library",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Philip Lehmann-Boehm",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"autoload": {
12+
"psr-4": { "PHPProm\\": "src/PHPProm" }
13+
},
14+
"extra": {
15+
"branch-alias": {
16+
"dev-master": "0.1.x-dev"
17+
}
18+
}
19+
}

src/PHPProm/PrometheusExport.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHPProm package.
5+
*
6+
* (c) Philip Lehmann-Böhm <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace PHPProm;
13+
14+
class PrometheusExport {
15+
16+
protected function getHeader($type, $metric, $label) {
17+
$header = '';
18+
if ($label !== null) {
19+
$header .= '# '.$type.' '.$metric.' '.$label."\n";
20+
}
21+
return $header;
22+
}
23+
24+
public function getMetric($metric, $label, array $labelsToValues, $help = null, $type = null) {
25+
$result = $this->getHeader('HELP', $metric, $help);
26+
$result .= $this->getHeader('TYPE', $metric, $type);
27+
$result .= implode("\n", array_map(function ($value, $labelValue) use ($metric, $label) {
28+
return $metric.'{'.$label.'="'.$labelValue.'"} '.$value;
29+
}, $labelsToValues, array_keys($labelsToValues)));
30+
return $result;
31+
}
32+
33+
}

src/PHPProm/SilexSetup.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHPProm package.
5+
*
6+
* (c) Philip Lehmann-Böhm <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace PHPProm;
13+
14+
use PHPProm\Storage\AbstractStorage;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Silex\Application;
18+
19+
class SilexSetup {
20+
21+
protected function setupMiddleware(Application $app, AbstractStorage $storage) {
22+
23+
$routeTime = new StopWatch($storage);
24+
25+
$app->before(function (Request $request, Application $app) use ($routeTime) {
26+
$routeTime->start();
27+
}, Application::EARLY_EVENT);
28+
29+
$app->finish(function (Request $request, Response $response) use ($routeTime) {
30+
$route = $request->get('_route');
31+
$routeTime->stop($route);
32+
});
33+
34+
}
35+
36+
public function setupAndGetMetricsRoute(Application $app, AbstractStorage $storage) {
37+
38+
$this->setupMiddleware($app, $storage);
39+
40+
return function() use ($app, $storage) {
41+
$routes = [];
42+
foreach ($app['routes']->all() as $route) {
43+
$path = str_replace('/', '_', $route->getPath());
44+
foreach ($route->getMethods() as $method) {
45+
$routes[] = $method.$path;
46+
}
47+
}
48+
$measurements = $storage->getMeasurements($routes);
49+
50+
$export = new PrometheusExport();
51+
$response = $export->getMetric('route', 'name', $measurements, 'request times', 'gauge');
52+
53+
return new Response($response, 200, ['Content-Type' => 'text/plain; version=0.0.4']);
54+
};
55+
}
56+
57+
}

src/PHPProm/StopWatch.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHPProm package.
5+
*
6+
* (c) Philip Lehmann-Böhm <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace PHPProm;
13+
14+
use PHPProm\Storage\AbstractStorage;
15+
16+
class StopWatch {
17+
18+
protected $storage;
19+
20+
protected $start;
21+
22+
public function __construct(AbstractStorage $storage) {
23+
$this->storage = $storage;
24+
}
25+
26+
public function start() {
27+
$this->start = microtime(true);
28+
}
29+
30+
public function stop($key) {
31+
$time = microtime(true) - $this->start;
32+
$this->storage->storeMeasurement($time, $key);
33+
}
34+
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHPProm package.
5+
*
6+
* (c) Philip Lehmann-Böhm <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace PHPProm\Storage;
13+
14+
abstract class AbstractStorage {
15+
16+
abstract public function storeMeasurement($value, $key);
17+
18+
abstract public function getMeasurements(array $keys);
19+
20+
}

src/PHPProm/Storage/RedisStorage.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHPProm package.
5+
*
6+
* (c) Philip Lehmann-Böhm <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace PHPProm\Storage;
13+
14+
class RedisStorage extends AbstractStorage {
15+
16+
protected $redis;
17+
18+
protected $prefix;
19+
20+
public function __construct($host, $password = null, $port = 6379, $dbIndex = null, $prefix = 'RoutePerformanceExporter:') {
21+
$this->redis = new \Redis();
22+
$this->redis->connect($host, $port);
23+
if ($password !== null) {
24+
$this->redis->auth($password);
25+
}
26+
if ($dbIndex !== null) {
27+
$this->redis->select($dbIndex);
28+
}
29+
$this->redis->setOption(\Redis::OPT_PREFIX, $prefix);
30+
$this->prefix = $prefix;
31+
}
32+
33+
public function storeMeasurement($value, $key) {
34+
$this->redis->set($key, $value);
35+
}
36+
37+
public function getMeasurements(array $keys) {
38+
$measurements = [];
39+
foreach ($this->redis->mget($keys) as $i => $value) {
40+
$measurements[$keys[$i]] = $value !== false ? (float)$value : null;
41+
}
42+
return $measurements;
43+
}
44+
}

0 commit comments

Comments
 (0)