#feature: update AI log analysis
This commit is contained in:
@@ -82,6 +82,7 @@ class SlsClient
|
||||
* @param int $offset 偏移量
|
||||
* @param int $limit 返回数量
|
||||
* @param string|null $logstore 可选的 logstore,不传则使用默认
|
||||
* @param int $maxRetries 最大重试次数
|
||||
* @return array{logs: array, count: int, complete: bool}
|
||||
*/
|
||||
public function getLogs(
|
||||
@@ -90,7 +91,8 @@ class SlsClient
|
||||
?string $query = null,
|
||||
int $offset = 0,
|
||||
int $limit = 100,
|
||||
?string $logstore = null
|
||||
?string $logstore = null,
|
||||
int $maxRetries = 3
|
||||
): array {
|
||||
$this->ensureConfigured();
|
||||
|
||||
@@ -106,26 +108,47 @@ class SlsClient
|
||||
false
|
||||
);
|
||||
|
||||
try {
|
||||
$response = $this->client->getLogs($request);
|
||||
$lastException = null;
|
||||
for ($attempt = 1; $attempt <= $maxRetries; $attempt++) {
|
||||
try {
|
||||
$response = $this->client->getLogs($request);
|
||||
|
||||
$logs = [];
|
||||
foreach ($response->getLogs() as $log) {
|
||||
$logs[] = $log->getContents();
|
||||
$logs = [];
|
||||
foreach ($response->getLogs() as $log) {
|
||||
$logs[] = $log->getContents();
|
||||
}
|
||||
|
||||
return [
|
||||
'logs' => $logs,
|
||||
'count' => $response->getCount(),
|
||||
'complete' => $response->isCompleted(),
|
||||
];
|
||||
} catch (Aliyun_Log_Exception $e) {
|
||||
$lastException = $e;
|
||||
$errorCode = $e->getErrorCode();
|
||||
|
||||
// 对于 5xx 错误或 RequestError,进行重试
|
||||
if (str_contains($errorCode, 'RequestError') || str_contains($e->getErrorMessage(), '50')) {
|
||||
if ($attempt < $maxRetries) {
|
||||
sleep(pow(2, $attempt)); // 指数退避: 2, 4, 8 秒
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 其他错误直接抛出
|
||||
throw new RuntimeException(
|
||||
"SLS 查询失败: [{$errorCode}] {$e->getErrorMessage()}",
|
||||
0,
|
||||
$e
|
||||
);
|
||||
}
|
||||
|
||||
return [
|
||||
'logs' => $logs,
|
||||
'count' => $response->getCount(),
|
||||
'complete' => $response->isCompleted(),
|
||||
];
|
||||
} catch (Aliyun_Log_Exception $e) {
|
||||
throw new RuntimeException(
|
||||
"SLS 查询失败: [{$e->getErrorCode()}] {$e->getErrorMessage()}",
|
||||
0,
|
||||
$e
|
||||
);
|
||||
}
|
||||
|
||||
throw new RuntimeException(
|
||||
"SLS 查询失败: [{$lastException->getErrorCode()}] {$lastException->getErrorMessage()}",
|
||||
0,
|
||||
$lastException
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user