Home > PHP Framework > Laravel > body text

Code example for connecting Laravel to Prometheus

不言
Release: 2019-03-02 14:09:57
forward
3538 people have browsed it

The content of this article is about the code example of connecting Laravel to Prometheus. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Increase the Counter counter on the original basis:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use traumferienwohnungen\PrometheusExporter\Middleware\AbstractResponseTimeMiddleware;

class PrometheusMonitor extends AbstractResponseTimeMiddleware
{
    protected function getRouteNames()
    {
        $routeNames = [];
        foreach (\Route::getRoutes() as $route){
            $routeNames[] = '/'.ltrim($route->uri(), '/');
        }
        return $routeNames;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (defined('LARAVEL_START')){
            $start = LARAVEL_START;
        } elseif (defined('LUMEN_START')){
            $start = LUMEN_START;
        } else {
            $start = microtime(true);
        }
        $this->request = $request;

        /** @var \Illuminate\Http\Response $response */
        $response = $next($request);

        $route_name = $this->getRouteName();
        $method = $request->getMethod();
        $status = $response->getStatusCode();

        $duration = microtime(true) - $start;
        $duration_milliseconds = $duration * 1000.0;
        $this->countRequest($route_name, $method, $status, $duration_milliseconds);

        $this->initRequestMetrics($method, $status);

        return $response;
    }

    public function getRouteName(){
        return request()->getRequestUri();
    }


    public function initRequestMetrics($method, $status)
    {
        $namespace = config('prometheus_exporter.namespace_http_server');

        $labelNames = $this->getRequestCounterLabelNames();

        $name = 'request_wuc';
        $help = 'http_requests count';
        $counter = $this->registry->getOrRegisterCounter(
            $namespace, $name, $help, $labelNames
        );

        $counter->incBy(1, [$this->getRouteName(), $method, $status]);
    }
}
Copy after login

The above is the detailed content of Code example for connecting Laravel to Prometheus. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!