You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.9 KiB
130 lines
3.9 KiB
<?php
|
|
|
|
namespace App\Commons;
|
|
use Hyperf\Guzzle\ClientFactory;
|
|
|
|
class Log
|
|
{
|
|
/**
|
|
* @var \Hyperf\Guzzle\ClientFactory
|
|
*/
|
|
private $clientFactory;
|
|
|
|
public function __construct(ClientFactory $clientFactory)
|
|
{
|
|
$this->clientFactory = $clientFactory;
|
|
}
|
|
|
|
public function getClient()
|
|
{
|
|
// $options 等同于 GuzzleHttp\Client 构造函数的 $config 参数
|
|
$options = [
|
|
'timeout' => 2.0,
|
|
];
|
|
// $client 为协程化的 GuzzleHttp\Client 对象
|
|
$client = $this->clientFactory->create($options);
|
|
|
|
return $client;
|
|
}
|
|
|
|
public function event($labels=null,$datas){
|
|
|
|
// co(function () use ($labels,$datas){
|
|
|
|
// $client = $this->getClient();
|
|
// $kv = [];
|
|
// foreach ($datas as $key => $value) {
|
|
// $kv[] = $key."=".$value;
|
|
// }
|
|
// $pushLabels = [];
|
|
|
|
// $event_name = 'event_'.env('APP_ENV');
|
|
// if(!empty($labels)) $pushLabels[$event_name] = $labels;
|
|
|
|
// /*
|
|
// * data format:
|
|
// curl -v -H "Content-Type: application/json" -XPOST -s "http://39.96.12.39:3100/loki/api/v1/push" --data-raw \
|
|
// '{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1596274538882028800", "fizzbuzz" ] ] }]}'
|
|
// */
|
|
// $ts = $this->getMsecTime() . '000000';
|
|
// $datas = implode("&",$kv);
|
|
// $values = [[$ts,$datas]];
|
|
// $app_name = env('APP_NAME').'_'.env('APP_ENV');
|
|
|
|
// $pushLabels['app']= $app_name;
|
|
// $pushDatas = [
|
|
// 'streams'=>[
|
|
// [
|
|
// 'stream'=>$pushLabels,
|
|
// 'values'=>$values,
|
|
// ]
|
|
// ]
|
|
// ];
|
|
// $client->post(
|
|
// env('LOG_HOST','http://39.96.12.39:3100').'/loki/api/v1/push',
|
|
// [
|
|
// 'headers'=>[
|
|
// 'Content-Type'=>'application/json'
|
|
// ],
|
|
// 'body' => json_encode($pushDatas)
|
|
// ]
|
|
// );
|
|
// //var_dump(json_encode($pushDatas) );
|
|
// });
|
|
}
|
|
|
|
public function eventInTask($labels=null,$datas){
|
|
|
|
// $client = $this->getClient();
|
|
// $kv = [];
|
|
// foreach ($datas as $key => $value) {
|
|
// $kv[] = $key."=".$value;
|
|
// }
|
|
// $pushLabels = [];
|
|
|
|
// $event_name = 'event_'.env('APP_ENV');
|
|
// if(!empty($labels)) $pushLabels[$event_name] = $labels;
|
|
|
|
// /*
|
|
// * data format:
|
|
// curl -v -H "Content-Type: application/json" -XPOST -s "http://39.96.12.39:3100/loki/api/v1/push" --data-raw \
|
|
// '{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1596274538882028800", "fizzbuzz" ] ] }]}'
|
|
// */
|
|
// $ts = $this->getMsecTime() . '000000';
|
|
// $datas = implode("&",$kv);
|
|
// $values = [[$ts,$datas]];
|
|
// $app_name = env('APP_NAME').'_'.env('APP_ENV');
|
|
|
|
// $pushLabels['app']= $app_name;
|
|
// $pushDatas = [
|
|
// 'streams'=>[
|
|
// [
|
|
// 'stream'=>$pushLabels,
|
|
// 'values'=>$values,
|
|
// ]
|
|
// ]
|
|
// ];
|
|
// $client->post(
|
|
// env('LOG_HOST','http://39.96.12.39:3100').'/loki/api/v1/push',
|
|
// [
|
|
// 'headers'=>[
|
|
// 'Content-Type'=>'application/json'
|
|
// ],
|
|
// 'body' => json_encode($pushDatas)
|
|
// ]
|
|
// );
|
|
//var_dump(json_encode($pushDatas) );
|
|
}
|
|
|
|
public function push($datas){
|
|
$this->event(null,$datas);
|
|
}
|
|
|
|
public function getMsecTime()
|
|
{
|
|
list($msec, $sec) = explode(' ', microtime());
|
|
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
|
|
return $msectime;
|
|
}
|
|
|
|
}
|