feat: 集成阿里云日志服务(SLS)并增强日志记录详情
- 添加 SLS 日志上报模块(sls-logger.js) - 支持批量上报(每10条或5秒间隔) - 环境变量缺失时静默降级 - 自动重试失败的日志 - 新增日志信息提取器(log-extractor.js) - 提取 Token 使用统计(input_tokens, output_tokens) - 提取用户标识信息(user_id, session_id, ip) - 提取请求参数(temperature, max_tokens, stream) - 提取消息摘要(message_count, role_distribution, tool_names) - 增强所有 API 端点的日志记录 - /v1/chat/completions - /v1/responses - /v1/messages - /v1/messages/count_tokens - 修复日志字段序列化问题 - 扁平化嵌套对象字段,避免 [object Object] - 数组字段转换为逗号分隔字符串 - 添加阿里云环境变量配置到 docker-compose.yml - ALIYUN_ACCESS_KEY_ID - ALIYUN_ACCESS_KEY_SECRET - ALIYUN_SLS_ENDPOINT - ALIYUN_SLS_PROJECT - ALIYUN_SLS_LOGSTORE - 修改认证配置为自动刷新 Token 机制 - 使用 DROID_REFRESH_KEY 替代固定的 FACTORY_API_KEY - 实现每6小时自动刷新(Token有效期8小时) - Token 持久化到 auth.json
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
/**
|
||||
* 阿里云日志服务(SLS)日志模块
|
||||
*
|
||||
*
|
||||
* 功能:
|
||||
* - 将 API 请求/响应日志上报到阿里云 SLS
|
||||
* - 批量上报,减少 API 调用
|
||||
* - 环境变量缺失时静默降级
|
||||
*/
|
||||
|
||||
import aliyunLog from 'aliyun-log';
|
||||
const { Client, PutLogsRequest, LogItem, LogContent } = aliyunLog;
|
||||
import ALSClient from 'aliyun-log';
|
||||
|
||||
// SLS 配置
|
||||
const SLS_CONFIG = {
|
||||
@@ -35,7 +34,7 @@ function initClient() {
|
||||
}
|
||||
|
||||
try {
|
||||
client = new Client({
|
||||
client = new ALSClient({
|
||||
accessKeyId: SLS_CONFIG.accessKeyId,
|
||||
accessKeySecret: SLS_CONFIG.accessKeySecret,
|
||||
endpoint: SLS_CONFIG.endpoint
|
||||
@@ -55,28 +54,14 @@ async function flushLogs() {
|
||||
const logsToSend = logQueue.splice(0, BATCH_SIZE);
|
||||
|
||||
try {
|
||||
const logItems = logsToSend.map(log => {
|
||||
const contents = Object.entries(log).map(([key, value]) => {
|
||||
return new LogContent({
|
||||
key,
|
||||
value: String(value ?? '')
|
||||
});
|
||||
});
|
||||
return new LogItem({
|
||||
time: Math.floor(Date.now() / 1000),
|
||||
contents
|
||||
});
|
||||
});
|
||||
const logs = logsToSend.map(log => ({
|
||||
timestamp: Math.floor(Date.now() / 1000),
|
||||
content: Object.fromEntries(
|
||||
Object.entries(log).map(([key, value]) => [key, String(value ?? '')])
|
||||
)
|
||||
}));
|
||||
|
||||
const request = new PutLogsRequest({
|
||||
projectName: SLS_CONFIG.project,
|
||||
logStoreName: SLS_CONFIG.logstore,
|
||||
logGroup: {
|
||||
logs: logItems
|
||||
}
|
||||
});
|
||||
|
||||
await client.putLogs(request);
|
||||
await client.postLogStoreLogs(SLS_CONFIG.project, SLS_CONFIG.logstore, { logs });
|
||||
console.log(`[SLS] 成功上报 ${logsToSend.length} 条日志`);
|
||||
} catch (error) {
|
||||
console.error('[SLS] 日志上报失败:', error.message);
|
||||
|
||||
Reference in New Issue
Block a user