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\Integration; |
13
|
|
|
|
14
|
|
|
use PHPProm\PrometheusExport; |
15
|
|
|
use PHPProm\StopWatch; |
16
|
|
|
use PHPProm\Storage\AbstractStorage; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Silex\Application; |
20
|
|
|
|
21
|
|
|
class SilexSetup { |
22
|
|
|
|
23
|
|
|
protected function setupMiddleware(Application $app, AbstractStorage $storage) { |
24
|
|
|
|
25
|
|
|
$storage->addAvailableMetric('time', 'route_time', 'name', 'request times per route in seconds', 'gauge', 'Nan'); |
26
|
|
|
$storage->addAvailableMetric('memory', 'route_memory', 'name', 'request memory per route in bytes', 'gauge', 'Nan'); |
27
|
|
|
$storage->addAvailableMetric('requests_total', 'route_requests_total', 'name', 'total requests per route', 'counter', 0); |
28
|
|
|
|
29
|
|
|
$routeTime = new StopWatch($storage); |
30
|
|
|
|
31
|
|
|
$app->before(function() use ($routeTime) { |
32
|
|
|
$routeTime->start(); |
33
|
|
|
}, Application::EARLY_EVENT); |
34
|
|
|
|
35
|
|
|
$app->finish(function(Request $request) use ($routeTime, $storage) { |
36
|
|
|
$route = $request->get('_route'); |
37
|
|
|
$routeTime->stop('time', $route); |
38
|
|
|
$storage->storeMeasurement('memory', $route, memory_get_peak_usage(true)); |
39
|
|
|
$storage->incrementMeasurement('requests_total', $route); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setupAndGetMetricsRoute(Application $app, AbstractStorage $storage) { |
45
|
|
|
|
46
|
|
|
$this->setupMiddleware($app, $storage); |
47
|
|
|
|
48
|
|
|
return function() use ($app, $storage) { |
49
|
|
|
$routes = []; |
50
|
|
|
foreach ($app['routes']->all() as $route) { |
51
|
|
|
$path = str_replace('/', '_', $route->getPath()); |
52
|
|
|
foreach ($route->getMethods() as $method) { |
53
|
|
|
$routes[] = $method.$path; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
$export = new PrometheusExport(); |
|
|
|
|
57
|
|
|
$response = $export->getExport($storage, $routes); |
58
|
|
|
return new Response($response, 200, ['Content-Type' => 'text/plain; version=0.0.4']); |
59
|
|
|
}; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.