2022-11-28 13:25:40 +08:00
|
|
|
package metrics
|
|
|
|
|
2022-11-29 02:55:42 +08:00
|
|
|
import (
|
2022-11-29 22:20:23 +08:00
|
|
|
"encoding/json"
|
|
|
|
"os"
|
2022-11-29 02:55:42 +08:00
|
|
|
"sync"
|
2022-11-29 22:20:23 +08:00
|
|
|
"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 {
|
2022-11-29 22:20:23 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-30 21:31:08 +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)
|
2022-11-29 22:20:23 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|