Completed
Push — master ( 7b04fe...bd89c1 )
by Philip
02:01
created

SilexSetup::setupAndGetMetricsRoute()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 3
eloc 16
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\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');
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...
56
57
            $routesMemory = $storage->getMeasurements('memory', $routes);
58
            $response .= $export->getMetric('route_memory', 'name', $routesMemory, 'request memory per route in bytes', 'gauge');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
59
60
            $routesRequestsTotal = $storage->getMeasurements('requests_total', $routes, 0);
61
            $response .= $export->getMetric('route_requests_total', 'name', $routesRequestsTotal, 'total requests per route', 'counter');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
62
63
            return new Response($response, 200, ['Content-Type' => 'text/plain; version=0.0.4']);
64
        };
65
    }
66
67
}