<commit_before><?php

/*
 * This file is part of the PHPProm package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace PHPProm\Integration;

use PHPProm\PrometheusExport;
use PHPProm\StopWatch;
use PHPProm\Storage\StorageInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Silex\Application;

class SilexSetup {

    protected function setupMiddleware(Application $app, StorageInterface $storage) {

        $routeTime = new StopWatch($storage);

        $app->before(function() use ($routeTime) {
            $routeTime->start();
        }, Application::EARLY_EVENT);

        $app->finish(function(Request $request) use ($routeTime, $storage) {
            $route = $request->get('_route');
            $routeTime->stop('time', $route);
            $storage->storeMeasurement('memory', $route, memory_get_peak_usage(true));
        });

    }

    public function setupAndGetMetricsRoute(Application $app, StorageInterface $storage) {

        $this->setupMiddleware($app, $storage);

        return function() use ($app, $storage) {
            $routes = [];
            foreach ($app['routes']->all() as $route) {
                $path = str_replace('/', '_', $route->getPath());
                foreach ($route->getMethods() as $method) {
                    $routes[] = $method.$path;
                }
            }
            $export = new PrometheusExport();

            $routesTime = $storage->getMeasurements('time', $routes);
            $response = $export->getMetric('route_time', 'name', $routesTime, 'request times per route in seconds', 'gauge');

            $routesMemory = $storage->getMeasurements('memory', $routes);
            $response .= $export->getMetric('route_memory', 'name', $routesMemory, 'request memory per route in bytes', 'gauge');

            return new Response($response, 200, ['Content-Type' => 'text/plain; version=0.0.4']);
        };
    }

}<commit_msg>measuring the total requests per route<commit_after><?php

/*
 * This file is part of the PHPProm package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace PHPProm\Integration;

use PHPProm\PrometheusExport;
use PHPProm\StopWatch;
use PHPProm\Storage\StorageInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Silex\Application;

class SilexSetup {

    protected function setupMiddleware(Application $app, StorageInterface $storage) {

        $routeTime = new StopWatch($storage);

        $app->before(function() use ($routeTime) {
            $routeTime->start();
        }, Application::EARLY_EVENT);

        $app->finish(function(Request $request) use ($routeTime, $storage) {
            $route = $request->get('_route');
            $routeTime->stop('time', $route);
            $storage->storeMeasurement('memory', $route, memory_get_peak_usage(true));
            $storage->incrementMeasurement('requests_total', $route);
        });

    }

    public function setupAndGetMetricsRoute(Application $app, StorageInterface $storage) {

        $this->setupMiddleware($app, $storage);

        return function() use ($app, $storage) {
            $routes = [];
            foreach ($app['routes']->all() as $route) {
                $path = str_replace('/', '_', $route->getPath());
                foreach ($route->getMethods() as $method) {
                    $routes[] = $method.$path;
                }
            }
            $export = new PrometheusExport();

            $routesTime = $storage->getMeasurements('time', $routes);
            $response = $export->getMetric('route_time', 'name', $routesTime, 'request times per route in seconds', 'gauge');

            $routesMemory = $storage->getMeasurements('memory', $routes);
            $response .= $export->getMetric('route_memory', 'name', $routesMemory, 'request memory per route in bytes', 'gauge');

            $routesRequestsTotal = $storage->getMeasurements('requests_total', $routes, 0);
            $response .= $export->getMetric('route_requests_total', 'name', $routesRequestsTotal, 'total requests per route', 'counter');

            return new Response($response, 200, ['Content-Type' => 'text/plain; version=0.0.4']);
        };
    }

}
{'fraction_non_alphanumeric': 0.14511814534106107, 'fraction_numerical': 0.0028979045920642, 'mean_word_length': 2.935964912280702, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5093185', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1222, 'n_words': 296}