package server import ( "net/http" "sort" "strings" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store" ) func init() { registerUserRoute("GET /api/v1/errors", func(s *Server) http.HandlerFunc { return s.errorsAPI }) } // svcWindow resolves range, service and host filters shared by the health routes. func (s *Server) svcWindow(w http.ResponseWriter, r *http.Request) (from, to int64, svc, host string, ok bool) { rg, ok := s.rangeOf(w, r) if !ok { return } q := r.URL.Query() now := s.opt.Store.Now().Unix() from, to = rg.Bounds(now) svc = q.Get("svc") host = q.Get("host") if host == "all" || host == s.opt.Store.Name() { host = "" } if len(host) > 253 || len(svc) > 128 { writeJSON(w, http.StatusBadRequest, map[string]string{"error": "bad host or svc"}) return 0, 0, "", "", false } return from, to, svc, host, true } type kv struct { Key string N uint32 } func topKV(m map[string]uint32, n int) []kv { out := make([]kv, 0, len(m)) for k, v := range m { out = append(out, kv{k, v}) } sort.Slice(out, func(a, b int) bool { if out[a].N != out[b].N { return out[a].N > out[b].N } return out[a].Key < out[b].Key }) if len(out) > n { out = out[:n] } return out } func errCount(c [store.NumCls]uint32) uint32 { var n uint32 for _, v := range c { n += v } return n } func (s *Server) errorsAPI(w http.ResponseWriter, r *http.Request) { from, to, svc, host, ok := s.svcWindow(w, r) if !ok { return } views := s.opt.Store.Services(from, to, svc, host) var tot [store.NumCls]uint32 var req, legit uint32 startH, endH := from/3600, to/3600 if endH-startH > 719 { startH = endH - 719 } nb := int(endH-startH) + 1 series := map[string][]uint32{} for _, n := range store.ClsNames { series[n] = make([]uint32, nb) } type routeOut struct { Svc string `json:"svc"` Route string `json:"route"` Class string `json:"class"` N uint32 `json:"n"` } var routes []routeOut broken := map[string]*struct { n uint32 last int64 ref string path string }{} type heat struct { alias string total uint32 byH map[int64]uint32 } var heats []heat for _, v := range views { req += v.Tot.Req legit += v.Tot.Legit h := heat{alias: v.Alias, byH: map[int64]uint32{}} for i := range tot { tot[i] += v.Tot.Cls[i] } for _, hr := range v.Hours { if hr.Hour < startH || hr.Hour > endH { continue } for i, n := range store.ClsNames { series[n][hr.Hour-startH] += hr.Cls[i] } if c := hr.Cls[store.ClsApp]; c > 0 { h.byH[hr.Hour] = c h.total += c } } heats = append(heats, h) for _, e := range topKV(v.ErrRoutes, 50) { cl, rt, _ := strings.Cut(e.Key, "|") routes = append(routes, routeOut{v.Alias, rt, cl, e.N}) } for k, n := range v.Broken { p, ref, _ := strings.Cut(k, "\x00") b := broken[k] if b == nil { b = &struct { n uint32 last int64 ref string path string }{ref: ref, path: p} broken[k] = b } b.n += n if t := v.BrokenLast[k]; t > b.last { b.last = t } } } sort.Slice(routes, func(a, b int) bool { return routes[a].N > routes[b].N }) if len(routes) > 50 { routes = routes[:50] } type brokenOut struct { Path string `json:"path"` Ref string `json:"ref"` N uint32 `json:"n"` Last int64 `json:"last"` } bl := make([]brokenOut, 0, len(broken)) for _, b := range broken { bl = append(bl, brokenOut{b.path, b.ref, b.n, b.last}) } sort.Slice(bl, func(a, b int) bool { if bl[a].N != bl[b].N { return bl[a].N > bl[b].N } return bl[a].Path < bl[b].Path }) if len(bl) > 50 { bl = bl[:50] } // heatmap: services with most app errors, last 168 hours of the window sort.Slice(heats, func(a, b int) bool { return heats[a].total > heats[b].total }) if len(heats) > 64 { heats = heats[:64] } hStart := startH if endH-hStart > 167 { hStart = endH - 167 } hours := make([]int64, 0, int(endH-hStart)+1) for h := hStart; h <= endH; h++ { hours = append(hours, h*3600) } svcs := make([]string, len(heats)) grid := make([][]uint32, len(heats)) for i, h := range heats { svcs[i] = h.alias grid[i] = make([]uint32, len(hours)) for j := range hours { grid[i][j] = h.byH[hStart+int64(j)] } } client := tot[store.ClsClientBroken] + tot[store.ClsClientAuth] + tot[store.ClsClientOther] classes := map[string]uint32{ "app": tot[store.ClsApp], "proxy": tot[store.ClsProxy], "blocked": tot[store.ClsBlocked], "attack": tot[store.ClsAttack], "bot": tot[store.ClsBot], "client": client, } for i, n := range store.ClsNames { if strings.HasPrefix(n, "client_") { classes[n] = tot[i] } } errs := errCount(tot) var rate float64 if legit > 0 { rate = float64(tot[store.ClsApp]+tot[store.ClsProxy]) / float64(legit) } writeJSON(w, 200, map[string]any{ "instance": s.opt.Store.Name(), "range": map[string]int64{"from": from, "to": to}, "requests": req, "errors": errs, "legit": legit, "app_error_rate": rate, "classes": classes, "timeline": map[string]any{"start": startH * 3600, "step": 3600, "series": series}, "heatmap": map[string]any{"svcs": svcs, "hours": hours, "err": grid}, "broken": bl, "top_routes": routes, }) }