第七章:可观测性¶
本章介绍 API 网关的可观测性配置。
访问日志¶
Kong 日志配置¶
# kong.conf
proxy_access_log = /dev/stdout
proxy_error_log = /dev/stderr
admin_access_log = /dev/stdout
admin_error_log = /dev/stderr
log_level = notice
APISIX 日志配置¶
# HTTP 日志
curl -i -X PUT http://localhost:9180/apisix/admin/routes/1 \
-H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
-d '{
"uri": "/api/*",
"plugins": {
"http-logger": {
"uri": "http://log-server/logs",
"batch_max_size": 100,
"max_retry_count": 3
}
},
"upstream_id": "1"
}'
# Kafka 日志
curl -i -X PUT http://localhost:9180/apisix/admin/routes/1 \
-H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
-d '{
"uri": "/api/*",
"plugins": {
"kafka-logger": {
"broker_list": {
"kafka:9092": 9092
},
"kafka_topic": "access-logs",
"key": "remote_addr"
}
},
"upstream_id": "1"
}'
指标收集¶
Prometheus 集成¶
# Kong
curl -i -X POST http://localhost:8001/plugins \
-d "name=prometheus"
# 访问指标
curl http://localhost:8001/metrics
# APISIX
# config.yaml
plugin_attr:
prometheus:
enable_exporter: true
exporter_addr:
ip: "0.0.0.0"
port: 9091
# 访问指标
curl http://localhost:9091/apisix/prometheus/metrics
Prometheus 配置¶
# prometheus.yml
scrape_configs:
- job_name: 'kong'
static_configs:
- targets: ['kong:8001']
- job_name: 'apisix'
static_configs:
- targets: ['apisix:9091']
分布式追踪¶
Jaeger 集成¶
# APISIX
curl -i -X PUT http://localhost:9180/apisix/admin/routes/1 \
-H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
-d '{
"uri": "/api/*",
"plugins": {
"opentelemetry": {
"sampler": {
"name": "always_on"
},
"exporter": {
"address": "otel-collector:4317"
}
}
},
"upstream_id": "1"
}'
SkyWalking 集成¶
# APISIX
curl -i -X PUT http://localhost:9180/apisix/admin/routes/1 \
-H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
-d '{
"uri": "/api/*",
"plugins": {
"skywalking": {
"sample_ratio": 1,
"service_name": "APISIX-Gateway",
"service_instance_name": "apisix-instance-1",
"endpoint_addr": "skywalking-oap:11800"
}
},
"upstream_id": "1"
}'
健康检查¶
# Kong
curl http://localhost:8001/status
# APISIX
curl http://localhost:9180/apisix/status \
-H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1"
Grafana 仪表板¶
导入仪表板¶
关键指标¶
| 指标 | 说明 |
|---|---|
| Request Rate | 请求速率 |
| Latency | 响应延迟 |
| Error Rate | 错误率 |
| Upstream Latency | 上游延迟 |
| Bandwidth | 带宽使用 |
小结¶
可观测性要点:
- 访问日志:HTTP、Kafka
- 指标收集:Prometheus
- 分布式追踪:Jaeger、SkyWalking
- 健康检查:状态监控
下一章我们将学习生产实践。