AI Gateway 是一个高性能的反向代理服务,专为AI API设计,支持流式响应、详细监控和日志分析。
客户端 → AI Gateway → AI服务
↓
Logstash → Elasticsearch → Kibana
# 克隆项目
git clone <repository-url>
cd aigateway
# 编译
go build -o aigateway
# 运行
./aigateway
编辑 config.yaml 配置代理目标和日志设置:
proxy:
target_url: "http://localhost:3001"
logging:
level: "info"
output: "both" # console, file, logstash, both
logstash:
enabled: true
host: "192.168.0.142"
port: 28779
使用Docker Compose部署完整的ELK Stack:
# 启动ELK Stack
docker-compose -f docker-compose-elk.yml up -d
# 检查服务状态
docker-compose -f docker-compose-elk.yml ps
服务地址:
# 服务器配置
server:
host: "0.0.0.0"
port: 8082
read_timeout: "30s"
write_timeout: "30s"
# 代理配置
proxy:
target_url: "http://localhost:3001"
request_timeout: "30s"
keep_alive: true
# 监控配置
monitor:
enabled: true
detailed_logs: true
filter_rules:
- name: "special_token"
parameter: "token"
type: "prefix"
value: "special_"
action: "save_to_file"
# 日志配置
logging:
level: "info"
output: "both"
format: "json"
# Logstash集成
logstash:
enabled: true
host: "192.168.0.142"
port: 28779
protocol: "tcp"
buffer_size: 1000
# 详细请求日志
request_logging:
enabled: true
include_headers: true
include_body: true
include_response: true
max_body_size: 10240
sensitive_headers:
- "authorization"
- "x-api-key"
LOG_ELK_HOST: Logstash主机地址LOG_ELK_PORT: Logstash端口PROXY_TARGET: 代理目标URLLOG_LEVEL: 日志级别request_log): 详细的请求/响应信息stream_event): 流式传输的详细事件proxy_error): 代理过程中的错误system_error): 系统级错误metrics): 性能指标和统计信息{
"timestamp": "2025-01-09T12:00:00Z",
"level": "info",
"service": "aigateway",
"module": "proxy",
"message": "POST /auto/v1/chat/completions 200",
"trace_id": "abc123",
"data": {
"type": "request_log",
"request": {
"id": "abc123",
"method": "POST",
"path": "/auto/v1/chat/completions",
"client_ip": "192.168.1.100",
"duration": "1.5s",
"body_size": 256
},
"response": {
"status_code": 200,
"is_stream": true,
"stream_chunks": 15,
"stream_bytes": 1457
}
}
}
input {
tcp {
port => 28779
codec => json_lines
type => "aigateway"
}
}
filter {
# 解析时间戳
date {
match => [ "timestamp", "ISO8601" ]
target => "@timestamp"
}
# 按日志类型分类处理
if [data][type] == "request_log" {
mutate {
add_field => { "log_category" => "request" }
add_field => { "request_method" => "%{[data][request][method]}" }
add_field => { "response_status" => "%{[data][response][status_code]}" }
}
}
}
output {
elasticsearch {
hosts => ["http://elasticsearch:9200"]
# 按天分片,按类型分索引
index => "aigateway-%{log_category}-%{+YYYY.MM.dd}"
}
}
Elasticsearch索引会自动按天创建:
aigateway-request-2025.01.09: 请求日志aigateway-stream_event-2025.01.09: 流式事件aigateway-proxy_error-2025.01.09: 代理错误aigateway-system_error-2025.01.09: 系统错误aigateway-metrics-2025.01.09: 指标数据# 查看所有流式请求
data.response.is_stream:true
# 查看错误请求
response_status:>=400
# 查看特定时间段的请求
@timestamp:[2025-01-09T00:00:00 TO 2025-01-09T23:59:59]
# 查看慢请求 (>1秒)
duration_ms:>1000
curl http://localhost:8082/health
curl http://localhost:8082/metrics
# 访问: http://localhost:8083
# 用户名: admin
# 密码: admin123
go build -o aigateway
# 创建测试后端
python3 test-stream.py
# 测试流式请求
curl -X POST http://localhost:8082/auto/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Hello"}],"stream":true}' \
--no-buffer
# 查看实时日志
tail -f logs/aigateway.log
# 检查Logstash连接
nc -zv 192.168.0.142 28779
调整Golang参数:
export GOGC=100
export GOMAXPROCS=4
Elasticsearch优化:
ES_JAVA_OPTS: "-Xms2g -Xmx2g"
Logstash优化:
pipeline.workers: 2
pipeline.batch.size: 500
Logstash连接失败
telnet 192.168.0.142 28779流式响应中断
内存使用过高
./logs/aigateway.logMIT License
欢迎提交Issue和Pull Request!
Content type
Image
Digest
sha256:b5c54b1ed…
Size
10 MB
Last updated
about 1 year ago
docker pull xiangsx/aigateway