Completed
Push — master ( 3c7a07...1bc614 )
by Philip
01:56
created

SilexSetup::setupAndGetMetricsRoute()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 11
nc 1
nop 2
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();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
57
            $response = $export->getExport($storage, $routes);
58
            return new Response($response, 200, ['Content-Type' => 'text/plain; version=0.0.4']);
59
        };
60
    }
61
62
}