Sign inSign up

xiangsx/aigateway

By xiangsx

Updated about 1 year ago

Image
0

328

xiangsx/aigateway repository overview

AI Gateway

AI Gateway 是一个高性能的反向代理服务,专为AI API设计,支持流式响应、详细监控和日志分析。

特性

  • 流式响应支持: 零拷贝透传,优化AI流式API响应
  • 详细监控: 请求统计、性能指标、错误追踪
  • 结构化日志: JSON格式日志,支持Logstash集成
  • ELK集成: 完整的日志收集、处理和可视化方案
  • Web管理界面: 实时监控面板和统计图表
  • 灵活配置: YAML配置文件,支持环境变量覆盖
  • 存储支持: 文件存储和Elasticsearch存储
  • 过滤规则: 自定义请求过滤和特殊处理

架构

客户端 → AI Gateway → AI服务
            ↓
       Logstash → Elasticsearch → Kibana

快速开始

1. 基础运行
# 克隆项目
git clone <repository-url>
cd aigateway

# 编译
go build -o aigateway

# 运行
./aigateway
2. 配置文件

编辑 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
3. ELK Stack 部署

使用Docker Compose部署完整的ELK Stack:

# 启动ELK Stack
docker-compose -f docker-compose-elk.yml up -d

# 检查服务状态
docker-compose -f docker-compose-elk.yml ps

服务地址:

配置说明

主配置文件 (config.yaml)
# 服务器配置
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: 代理目标URL
  • LOG_LEVEL: 日志级别

日志系统

日志类型
  1. 请求日志 (request_log): 详细的请求/响应信息
  2. 流式事件 (stream_event): 流式传输的详细事件
  3. 代理错误 (proxy_error): 代理过程中的错误
  4. 系统错误 (system_error): 系统级错误
  5. 指标日志 (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
    }
  }
}

Logstash配置

Pipeline配置 (logstash/pipeline/aigateway.conf)
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: 指标数据

Kibana可视化

内置仪表板
  1. 请求概览: 总请求数、成功率、错误率
  2. 性能监控: 响应时间趋势、延迟分布
  3. 流式分析: 流式请求统计、chunk分析
  4. 错误分析: 错误类型、错误趋势
  5. 地理分析: 客户端地理位置分布
常用查询
# 查看所有流式请求
data.response.is_stream:true

# 查看错误请求
response_status:>=400

# 查看特定时间段的请求
@timestamp:[2025-01-09T00:00:00 TO 2025-01-09T23:59:59]

# 查看慢请求 (>1秒)
duration_ms:>1000

API接口

健康检查
curl http://localhost:8082/health
实时指标
curl http://localhost:8082/metrics
Web管理界面
# 访问: 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

生产部署

系统要求
  • Go 1.21+
  • Docker & Docker Compose (for ELK)
  • 4GB+ RAM (推荐)
  • 10GB+ 磁盘空间
性能优化
  1. 调整Golang参数:

    export GOGC=100
    export GOMAXPROCS=4
    
  2. Elasticsearch优化:

    ES_JAVA_OPTS: "-Xms2g -Xmx2g"
    
  3. Logstash优化:

    pipeline.workers: 2
    pipeline.batch.size: 500
    
监控告警
  • 内存使用率 > 80%
  • 错误率 > 5%
  • 响应时间 > 5秒
  • Elasticsearch磁盘空间 < 10%

故障排除

常见问题
  1. Logstash连接失败

    • 检查网络连接: telnet 192.168.0.142 28779
    • 检查防火墙设置
    • 确认Logstash服务状态
  2. 流式响应中断

    • 检查代理目标健康状态
    • 增加请求超时时间
    • 检查网络稳定性
  3. 内存使用过高

    • 调整日志缓冲区大小
    • 减少详细日志记录
    • 优化Elasticsearch索引
日志位置
  • 应用日志: ./logs/aigateway.log
  • Elasticsearch日志: Docker容器内
  • Logstash日志: Docker容器内

许可证

MIT License

贡献

欢迎提交Issue和Pull Request!

Tag summary

Content type

Image

Digest

sha256:b5c54b1ed

Size

10 MB

Last updated

about 1 year ago

docker pull xiangsx/aigateway