More detailed metrics
All checks were successful
release-nightly / build-docker (push) Successful in 1m52s

This commit is contained in:
LittleSheep 2024-01-31 13:17:52 +08:00
parent 09c4800143
commit 7b544f370d
6 changed files with 45 additions and 9 deletions

View File

@ -121,7 +121,7 @@ func makeResponse(c *fiber.Ctx, region *navi.Region, location *navi.Location, de
message = err.Error() message = err.Error()
} }
go navi.R.AddTrace(navi.RoadTrace{ go navi.R.Metrics.AddTrace(navi.RoadTrace{
Region: region.ID, Region: region.ID,
Location: location.ID, Location: location.ID,
Destination: dest.ID, Destination: dest.ID,

View File

@ -6,8 +6,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/spf13/viper"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
) )
@ -16,7 +14,12 @@ var R *RoadApp
func ReadInConfig(root string) error { func ReadInConfig(root string) error {
instance := &RoadApp{ instance := &RoadApp{
Regions: make([]*Region, 0), Regions: make([]*Region, 0),
Traces: make([]RoadTrace, 0, viper.GetInt("performance.traces_limit")), Metrics: &RoadMetrics{
Traces: make([]RoadTrace, 0),
Traffic: make(map[string]int64),
TrafficFrom: make(map[string]int64),
TotalTraffic: 0,
},
} }
if err := filepath.Walk(root, func(fp string, info os.FileInfo, _ error) error { if err := filepath.Walk(root, func(fp string, info os.FileInfo, _ error) error {

View File

@ -2,6 +2,14 @@ package navi
import "github.com/spf13/viper" import "github.com/spf13/viper"
type RoadMetrics struct {
Traces []RoadTrace `json:"-"`
Traffic map[string]int64 `json:"traffic"`
TrafficFrom map[string]int64 `json:"traffic_from"`
TotalTraffic int64 `json:"total_traffic"`
}
type RoadTrace struct { type RoadTrace struct {
Region string `json:"region"` Region string `json:"region"`
Location string `json:"location"` Location string `json:"location"`
@ -17,8 +25,28 @@ type RoadTraceError struct {
Message string `json:"message"` Message string `json:"message"`
} }
func (v *RoadApp) AddTrace(trace RoadTrace) { func (v *RoadMetrics) AddTrace(trace RoadTrace) {
v.TotalTraffic++
if _, ok := v.Traffic[trace.Region]; !ok {
v.Traffic[trace.Region] = 0
} else {
v.Traffic[trace.Region]++
}
if _, ok := v.TrafficFrom[trace.IpAddress]; !ok {
v.TrafficFrom[trace.IpAddress] = 0
} else {
v.TrafficFrom[trace.IpAddress]++
}
v.Traces = append(v.Traces, trace) v.Traces = append(v.Traces, trace)
// Garbage recycle
if len(v.Traffic) > viper.GetInt("performance.traces_limit") {
v.Traffic = make(map[string]int64)
}
if len(v.TrafficFrom) > viper.GetInt("performance.traces_limit") {
v.TrafficFrom = make(map[string]int64)
}
if len(v.Traces) > viper.GetInt("performance.traces_limit") { if len(v.Traces) > viper.GetInt("performance.traces_limit") {
v.Traces = v.Traces[1:] v.Traces = v.Traces[1:]
} }

View File

@ -10,8 +10,8 @@ import (
) )
type RoadApp struct { type RoadApp struct {
Regions []*Region `json:"regions"` Regions []*Region `json:"regions"`
Traces []RoadTrace `json:"traces"` Metrics *RoadMetrics `json:"metrics"`
} }
func (v *RoadApp) Forward(c *fiber.Ctx, dest *Destination) error { func (v *RoadApp) Forward(c *fiber.Ctx, dest *Destination) error {

View File

@ -5,6 +5,10 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func getTraces(c *fiber.Ctx) error { func getTraffic(c *fiber.Ctx) error {
return c.JSON(navi.R.Traces) return c.JSON(navi.R.Metrics)
}
func getTraces(c *fiber.Ctx) error {
return c.JSON(navi.R.Metrics.Traces)
} }

View File

@ -45,6 +45,7 @@ func InitSideload() *fiber.App {
cgi := app.Group("/cgi").Name("CGI") cgi := app.Group("/cgi").Name("CGI")
{ {
cgi.Get("/metadata", getMetadata) cgi.Get("/metadata", getMetadata)
cgi.Get("/traffic", getTraffic)
cgi.Get("/traces", getTraces) cgi.Get("/traces", getTraces)
cgi.Get("/stats", getStats) cgi.Get("/stats", getStats)
cgi.Get("/regions", getRegions) cgi.Get("/regions", getRegions)