Skip to content

Commit

Permalink
don't disrupt response on exception
Browse files Browse the repository at this point in the history
Signed-off-by: ahmadali shafiee <mail@ahmadalli.net>
  • Loading branch information
ahmadalli committed May 16, 2020
1 parent 4dfb62f commit a59d0e6
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/Middleware/RequestPerRoute.php
@@ -1,6 +1,8 @@
<?php

namespace Triadev\PrometheusExporter\Middleware;

use Exception;
use Illuminate\Http\Request;
use Closure;
use Illuminate\Http\Response;
Expand All @@ -11,7 +13,7 @@ class RequestPerRoute
{
/** @var PrometheusExporterContract */
private $prometheusExporter;

/**
* RequestPerRoute constructor.
* @param PrometheusExporterContract $prometheusExporter
Expand All @@ -20,35 +22,38 @@ public function __construct(PrometheusExporterContract $prometheusExporter)
{
$this->prometheusExporter = $prometheusExporter;
}

/**
* Handle an incoming request.
*
* @param Request $request
* @param \Closure $next
* @param Request $request
* @param \Closure $next
* @return mixed
*
* @throws MetricsRegistrationException
*/
public function handle(Request $request, Closure $next)
{
$start = microtime(true);

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

$durationMilliseconds = (microtime(true) - $start) * 1000.0;

$path = $request->path();
$method = $request->getMethod();
$status = $response->getStatusCode();

$this->requestCountMetric($path, $method, $status);
$this->requestLatencyMetric($path, $method, $status, $durationMilliseconds);

try {
$durationMilliseconds = (microtime(true) - $start) * 1000.0;

$path = $request->path();
$method = $request->getMethod();
$status = $response->getStatusCode();

$this->requestCountMetric($path, $method, $status);
$this->requestLatencyMetric($path, $method, $status, $durationMilliseconds);
} catch (Exception $e) {
report($e);
}

return $response;
}

/**
* @param string $routeName
* @param string $method
Expand All @@ -74,7 +79,7 @@ private function requestCountMetric(string $routeName, string $method, int $stat
]
);
}

/**
* @param string $routeName
* @param string $method
Expand All @@ -86,11 +91,11 @@ private function requestCountMetric(string $routeName, string $method, int $stat
private function requestLatencyMetric(string $routeName, string $method, int $status, int $duration)
{
$bucketsPerRoute = null;

if ($bucketsPerRouteConfig = config('prometheus-exporter.buckets_per_route')) {
$bucketsPerRoute = array_get($bucketsPerRouteConfig, $routeName);
}

$this->prometheusExporter->setHistogram(
'requests_latency_milliseconds',
'duration of requests',
Expand Down

0 comments on commit a59d0e6

Please sign in to comment.