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()
}
go navi.R.AddTrace(navi.RoadTrace{
go navi.R.Metrics.AddTrace(navi.RoadTrace{
Region: region.ID,
Location: location.ID,
Destination: dest.ID,

View File

@ -6,8 +6,6 @@ import (
"path/filepath"
"strings"
"github.com/spf13/viper"
"github.com/pelletier/go-toml/v2"
)
@ -16,7 +14,12 @@ var R *RoadApp
func ReadInConfig(root string) error {
instance := &RoadApp{
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 {

View File

@ -2,6 +2,14 @@ package navi
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 {
Region string `json:"region"`
Location string `json:"location"`
@ -17,8 +25,28 @@ type RoadTraceError struct {
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)
// 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") {
v.Traces = v.Traces[1:]
}

View File

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

View File

@ -5,6 +5,10 @@ import (
"github.com/gofiber/fiber/v2"
)
func getTraces(c *fiber.Ctx) error {
return c.JSON(navi.R.Traces)
func getTraffic(c *fiber.Ctx) error {
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.Get("/metadata", getMetadata)
cgi.Get("/traffic", getTraffic)
cgi.Get("/traces", getTraces)
cgi.Get("/stats", getStats)
cgi.Get("/regions", getRegions)