add monitor switch & catch exception log to lumen.

This commit is contained in:
dongwei 2019-03-14 16:58:11 +08:00
parent d12d0486b9
commit 60bc5943ff

View File

@ -6,6 +6,7 @@ use Closure;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use InfluxDB\Database; use InfluxDB\Database;
use InfluxDB\Exception;
use InfluxDB\Point; use InfluxDB\Point;
/** /**
@ -39,7 +40,7 @@ class TunnelMiddleware
{ {
//过滤tool返回结果 //过滤tool返回结果
if ($response instanceof JsonResponse) { if ($response instanceof JsonResponse) {
if (app()->environment('develop')) { if (app()->environment('develop', 'production') && env('RPC_MONITOR_SWITCH') == 1) {
$content = $response->getOriginalContent(); $content = $response->getOriginalContent();
$status = isset($content['error']) ? $content['error']['code'] : 200; $status = isset($content['error']) ? $content['error']['code'] : 200;
$client = new \InfluxDB\Client("influxdb-svc", 8086, '', '', false, false, 1, 1); $client = new \InfluxDB\Client("influxdb-svc", 8086, '', '', false, false, 1, 1);
@ -52,6 +53,11 @@ class TunnelMiddleware
['status_value' => $status == 200 ? $status : -$status] ['status_value' => $status == 200 ? $status : -$status]
) )
); );
try {
$database->writePoints($points, Database::PRECISION_SECONDS);
} catch (Exception $exception) {
app('log')->error('influxdb-write-wrong', ['code' => $exception->getCode(),'message' => $exception->getMessage()]);
}
} }
} }