Files

182 lines
5.2 KiB
Go

// ops_handler.go Issue#6·P2:系统健康总览(GET /ops/system/overview)——Dashboard
// 系统健康卡数据源(D10 元信息层,gateway 视角探测):
// - gateway 本进程 uptime(装配时刻起算);
// - consumer 独立进程无直接探针,以 submit 消费组 lag 间接观测(<10k=up);
// - mysql sql.DB.Ping 计时;
// - redis go-redis Ping 计时(nil=未启用降级 → down,诚实标注);
// - kafka LagReader.Total(与 consumer 同源 lag)。
//
// 角色门:admin/ops(基础设施信息不放大,group/customer/service 403——与
// /billing/orders 平台门同口径;service 属业务角色不见运维面)。
package v1
import (
"context"
"fmt"
"math"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"go-sms/internal/pkg/cache"
"go-sms/internal/pkg/response"
)
// HealthDeps 系统健康探测依赖(nil 字段=该子系统探针缺失,状态 unknown)。
// Lag 为函数字段便于测试注入(生产闭包包 LagReader;ctx 来自请求)。
type HealthDeps struct {
DB *gorm.DB
Redis *cache.Client
Lag func(ctx context.Context) (int64, error)
StartTime time.Time
HTTPAddr string
RedisMode string
}
// subsystemDTO 子系统状态(up/degraded/down/unknown 三态+unknown,拒绝全绿作弊)。
type subsystemDTO struct {
Name string `json:"name"`
Status string `json:"status"`
Detail string `json:"detail"`
LatencyMs float64 `json:"latency_ms"`
}
type opsHandler struct{ hd *HealthDeps }
func registerOpsRoutes(protected *gin.RouterGroup, deps Deps) {
if deps.Health == nil {
return
}
h := &opsHandler{hd: deps.Health}
protected.GET("/ops/system/overview", h.overview)
}
// opsAdminGate admin/ops 角色门(与 /billing/orders 平台门同口径)。
func opsAdminGate(c *gin.Context) bool {
claims := statsClaims(c)
if claims == nil {
return true // 无 claims 仅测试场景,放行
}
for _, r := range claims.Roles {
if r == "admin" || r == "ops" {
return true
}
}
return false
}
// overview GET /ops/system/overview:五子系统快照(单次请求全量探测,
// 上位机 10s 轮询;各探针失败不互相影响,缺失=unknown)。
func (h *opsHandler) overview(c *gin.Context) {
if !opsAdminGate(c) {
c.JSON(http.StatusForbidden, gin.H{"code": "ISC-403", "msg": "仅管理员可查看系统健康"})
return
}
hd := h.hd
ctx, cancel := context.WithTimeout(c.Request.Context(), 3*time.Second)
defer cancel()
subs := make([]subsystemDTO, 0, 5)
// gateway:本进程
up := time.Since(hd.StartTime).Round(time.Second)
subs = append(subs, subsystemDTO{
Name: "gateway", Status: "up",
Detail: fmt.Sprintf("UP %s · %s", opsHumane(up), opsOr(hd.HTTPAddr, ":8080")),
})
// lag 采样(consumer/kafka 共源;失败=双 unknown,链路抖动不判死)。
var lag int64 = -1
var lagErr error
if hd.Lag != nil {
lag, lagErr = hd.Lag(ctx)
}
lagStatus := func() string {
switch {
case hd.Lag == nil || lagErr != nil:
return "unknown"
case lag >= 10000:
return "degraded"
default:
return "up"
}
}
lagDetail := func(prefix string) string {
if hd.Lag == nil {
return prefix + "探针未装配"
}
if lagErr != nil {
return prefix + "采样失败"
}
return fmt.Sprintf("%slag %s", prefix, opsThousand(lag))
}
subs = append(subs, subsystemDTO{Name: "consumer", Status: lagStatus(), Detail: lagDetail("kafka ")})
subs = append(subs, subsystemDTO{Name: "kafka", Status: lagStatus(), Detail: lagDetail("")})
// mysql
if hd.DB != nil {
t0 := time.Now()
if sdb, err := hd.DB.DB(); err == nil {
if perr := sdb.PingContext(ctx); perr != nil {
subs = append(subs, subsystemDTO{Name: "mysql", Status: "down", Detail: perr.Error()})
} else {
subs = append(subs, subsystemDTO{Name: "mysql", Status: "up",
Detail: "OK", LatencyMs: opsMs(time.Since(t0))})
}
} else {
subs = append(subs, subsystemDTO{Name: "mysql", Status: "unknown", Detail: "句柄不可用"})
}
} else {
subs = append(subs, subsystemDTO{Name: "mysql", Status: "unknown", Detail: "未装配"})
}
// redis(nil=降级未启用,诚实标 down 而非假绿)
if hd.Redis == nil {
subs = append(subs, subsystemDTO{Name: "redis", Status: "down", Detail: "未启用(降级态)"})
} else {
t0 := time.Now()
if perr := hd.Redis.Ping(ctx).Err(); perr != nil {
subs = append(subs, subsystemDTO{Name: "redis", Status: "down", Detail: perr.Error()})
} else {
subs = append(subs, subsystemDTO{Name: "redis", Status: "up",
Detail: opsOr(hd.RedisMode, "single"), LatencyMs: opsMs(time.Since(t0))})
}
}
response.Success(c, gin.H{"uptime_s": int64(time.Since(hd.StartTime).Seconds()), "subsystems": subs})
}
func opsOr(s, def string) string {
if strings.TrimSpace(s) == "" {
return def
}
return s
}
func opsMs(d time.Duration) float64 { return math.Round(float64(d.Microseconds())/10) / 100 }
func opsThousand(v int64) string {
s := fmt.Sprintf("%d", v)
if len(s) <= 3 {
return s
}
var b []byte
for i, r := range []byte(s) {
if i > 0 && (len(s)-i)%3 == 0 {
b = append(b, ',')
}
b = append(b, r)
}
return string(b)
}
func opsHumane(d time.Duration) string {
if d < time.Hour {
return d.Round(time.Minute).String()
}
days := int(d.Hours()) / 24
return fmt.Sprintf("%dd%dh", days, int(d.Hours())%24)
}