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\StorageInterface; |
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, StorageInterface $storage) { |
24
|
|
|
|
25
|
|
|
$routeTime = new StopWatch($storage); |
26
|
|
|
|
27
|
|
|
$app->before(function() use ($routeTime) { |
28
|
|
|
$routeTime->start(); |
29
|
|
|
}, Application::EARLY_EVENT); |
30
|
|
|
|
31
|
|
|
$app->finish(function(Request $request) use ($routeTime, $storage) { |
32
|
|
|
$route = $request->get('_route'); |
33
|
|
|
$routeTime->stop('time', $route); |
34
|
|
|
$storage->storeMeasurement('memory', $route, memory_get_peak_usage(true)); |
35
|
|
|
$storage->incrementMeasurement('requests_total', $route); |
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setupAndGetMetricsRoute(Application $app, StorageInterface $storage) { |
41
|
|
|
|
42
|
|
|
$this->setupMiddleware($app, $storage); |
43
|
|
|
|
44
|
|
|
return function() use ($app, $storage) { |
45
|
|
|
$routes = []; |
46
|
|
|
foreach ($app['routes']->all() as $route) { |
47
|
|
|
$path = str_replace('/', '_', $route->getPath()); |
48
|
|
|
foreach ($route->getMethods() as $method) { |
49
|
|
|
$routes[] = $method.$path; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
$export = new PrometheusExport(); |
53
|
|
|
|
54
|
|
|
$routesTime = $storage->getMeasurements('time', $routes); |
55
|
|
|
$response = $export->getMetric('route_time', 'name', $routesTime, 'request times per route in seconds', 'gauge'); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$routesMemory = $storage->getMeasurements('memory', $routes); |
58
|
|
|
$response .= $export->getMetric('route_memory', 'name', $routesMemory, 'request memory per route in bytes', 'gauge'); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$routesRequestsTotal = $storage->getMeasurements('requests_total', $routes, 0); |
61
|
|
|
$response .= $export->getMetric('route_requests_total', 'name', $routesRequestsTotal, 'total requests per route', 'counter'); |
|
|
|
|
62
|
|
|
|
63
|
|
|
return new Response($response, 200, ['Content-Type' => 'text/plain; version=0.0.4']); |
64
|
|
|
}; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
} |
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.