netmaker/nm-proxy/metrics/metrics.go

57 lines
884 B
Go
Raw Normal View History

2022-11-28 13:25:40 +08:00
package metrics
2022-11-29 02:55:42 +08:00
import (
"encoding/json"
"os"
2022-11-29 02:55:42 +08:00
"sync"
"time"
2022-11-29 02:55:42 +08:00
)
2022-11-28 13:25:40 +08:00
/*
1. Create metrics packet--> packet with identifier to track latency, errors.
*/
2022-11-29 02:55:42 +08:00
type Metric struct {
LastRecordedLatency uint64
2022-11-29 02:55:42 +08:00
ConnectionStatus bool
2022-12-01 13:21:24 +08:00
TrafficSent float64
TrafficRecieved float64
2022-11-29 02:55:42 +08:00
}
type MetricsPayload struct {
MetricType MetricsUpdateType
Value interface{}
}
type MetricsUpdateType uint32
const (
LatencyUpdate MetricsUpdateType = 1
TrafficSentUpdate MetricsUpdateType = 2
TrafficRecievedUpdate MetricsUpdateType = 3
)
2022-12-01 13:09:43 +08:00
var MetricsMapLock = &sync.RWMutex{}
2022-11-29 02:55:42 +08:00
var MetricsMap = make(map[string]Metric)
func init() {
go func() {
for {
time.Sleep(1 * time.Minute)
PrintMetrics()
}
}()
}
func PrintMetrics() {
data, err := json.MarshalIndent(MetricsMap, "", " ")
if err != nil {
return
}
os.WriteFile("/tmp/metrics.json", data, 0755)
}