Completed
Push — master ( b85423...005649 )
by Philip
05:50
created

SilexSetup::setupAndGetMetricsRoute()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 25
rs 8.439
cc 5
eloc 17
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
/**
22
 * Class SilexSetup
23
 * Setups Silex applications to measure:
24
 * - the time of each route
25
 * - the used memory of each route
26
 * - the amount of requests of each route
27
 * It also offers an function to be used for a Prometheus scrapable endpoint.
28
 * @package PHPProm\Integration
29
 */
30
class SilexSetup {
31
32
    /**
33
     * Sets up the Silex middlewares where the actual measurements happen.
34
     *
35
     * @param Application $app
36
     * the Silex application
37
     * @param AbstractStorage $storage
38
     * the storage for the measurements
39
     */
40
    protected function setupMiddleware(Application $app, AbstractStorage $storage) {
41
42
        $storage->addAvailableMetric('route_time', 'name', 'request times per route in seconds', 'gauge', 'Nan');
43
        $storage->addAvailableMetric('route_memory', 'name', 'request memory per route in bytes', 'gauge', 'Nan');
44
        $storage->addAvailableMetric('route_requests_total', 'name', 'total requests per route', 'counter', 0);
45
46
        $routeTime = new StopWatch($storage);
47
48
        $app->before(function() use ($routeTime) {
49
            $routeTime->start();
50
        }, Application::EARLY_EVENT);
51
52
        $app->finish(function(Request $request) use ($routeTime, $storage) {
53
            $route = $request->get('_route');
54
            $routeTime->stop('route_time', $route);
55
            $storage->storeMeasurement('route_memory', $route, memory_get_peak_usage(true));
56
            $storage->incrementMeasurement('route_requests_total', $route);
57
        });
58
59
    }
60
61
    /**
62
     * Sets up the Silex middlewares where the actual measurements happen
63
     * and returns a function to be used for a Prometheus scrapable endpoint.
64
     *
65
     * @param Application $app
66
     * the Silex application
67
     * @param AbstractStorage $storage
68
     * the storage for the measurements
69
     *
70
     * @return \Closure
71
     * the function to be used for a Prometheus scrapable endpoint
72
     */
73
    public function setupAndGetMetricsRoute(Application $app, AbstractStorage $storage) {
74
75
        $this->setupMiddleware($app, $storage);
76
77
        return function() use ($app, $storage) {
78
            $routes = [];
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...
79
            $supportedMethods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'];
80
            foreach ($app['routes']->all() as $route) {
81
                $path        = str_replace('/', '_', $route->getPath());
82
                $foundMethod = false;
83
                foreach ($route->getMethods() as $method) {
84
                    $routes[] = $method.$path;
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...
85
                    $foundMethod = true;
86
                }
87
                if (!$foundMethod) {
88
                    foreach ($supportedMethods as $supportedMethod) {
89
                        $routes[] = $supportedMethod.$path;
90
                    }
91
                }
92
            }
93
            $export   = new PrometheusExport();
94
            $response = $export->getExport($storage, $routes);
95
            return new Response($response, 200, ['Content-Type' => 'text/plain; version=0.0.4']);
96
        };
97
    }
98
99
}
100