push monitor to influxdb

This commit is contained in:
dongwei 2019-01-31 15:06:30 +08:00
parent 38f034aa34
commit 8a180e0df7

View File

@ -1,4 +1,5 @@
<?php
namespace JsonRpc\Middleware;
use Closure;
@ -13,48 +14,48 @@ use InfluxDB\Point;
*/
class TunnelMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Pre-Middleware Action
$response = $next($request);
// Post-Middleware Action
return $response;
}
/**
* @param \Illuminate\Http\Request $request
* @param \Closure $response
*/
public function terminate($request, $response)
{
//过滤tool返回结果
if ($response instanceof JsonResponse)
{
$content = $response->getOriginalContent();
$status = isset($content['error']) ? $content['error']['code'] : 200;
$client = new \InfluxDB\Client('127.0.0.1', '8086');
$database = $client->selectDB('rpc_monitor');
$points = array(
new Point(
'monitor',
0.64,
['app' =>env('APP_NAME'), 'status' => $status],
['content' => $request->getContent()]
)
);
$result = $database->writePoints($points, Database::PRECISION_SECONDS);
app('rpc.logger')->info('rpc tunnel ctx ' [$result]);
}
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Pre-Middleware Action
$response = $next($request);
// Post-Middleware Action
return $response;
}
/**
* @param \Illuminate\Http\Request $request
* @param \Closure $response
*/
public function terminate($request, $response)
{
//过滤tool返回结果
if ($response instanceof JsonResponse) {
if (app()->environment('dev', 'production')) {
$content = $response->getOriginalContent();
$status = isset($content['error']) ? $content['error']['code'] : 200;
$client = new \InfluxDB\Client("10.0.1.67");
$database = $client->selectDB('rpc_monitor');
$points = array(
new Point(
'monitor',
null,
['app' => env('APP_NAME'), 'status' => $status, 'env' => app()->environment()],
['content' => $request->getContent()]
)
);
$database->writePoints($points, Database::PRECISION_SECONDS);
}
}
}
}