This commit is contained in:
2025-04-17 10:14:01 +08:00
parent 88dc849bd5
commit c954218088
6 changed files with 125 additions and 59 deletions
+30 -18
View File
@@ -5,12 +5,17 @@ use app\model\RequestTracks;
class DataTracker
{
// 不需要埋点的路径
protected $excludePaths = [
'/user/login'
];
public function process($request, \Closure $next)
{
// 前置处理
if($request->path() != 'user/login'){
return $request;
if ($this->shouldSkipTracking($request)) {
return $next($request);
}
// 前置处理
$this->logRequestData($request);
$response = $next($request);
@@ -21,11 +26,31 @@ class DataTracker
return $response;
}
protected function shouldSkipTracking($request)
{
$currentPath = $request->path();
$eventTracking = config('const.event_tracking');
// 检查是否在排除列表中
// foreach ($this->excludePaths as $path) {
// if (str_starts_with($currentPath, $path)) {
// return true;
// }
// }
if (in_array($currentPath, $eventTracking)) {
return false;
}
return true;
}
protected function logRequestData($request)
{
$trackData = [
'app_id' => 'crm/api',
'user_id' => $request->user['id'],
'user_id' => $request->user['id']??0,
'ip' => $request->getRealIp(),
'method' => $request->method(),
'path' => $request->path(),
@@ -38,20 +63,7 @@ class DataTracker
$requestTrack = RequestTracks::create($trackData);
}
protected function sendToQueue($data)
{
// 使用Redis队列
try {
redis()->lpush('track_queue', json_encode($data));
} catch (\Exception $e) {
// 失败降级处理
file_put_contents(
runtime_path('logs/track_fallback.log'),
json_encode($data) . PHP_EOL,
FILE_APPEND
);
}
}
protected function generateTraceId()
{