♻️ Refactored tracing system to cost less memory
This commit is contained in:
@ -16,9 +16,7 @@ func ReadInConfig(root string) error {
|
||||
instance := &RoadApp{
|
||||
Regions: make([]*Region, 0),
|
||||
Metrics: &RoadMetrics{
|
||||
Traces: make([]RoadTrace, 0),
|
||||
Traffic: make(map[string]int64),
|
||||
TrafficFrom: make(map[string]int64),
|
||||
TotalTraffic: 0,
|
||||
StartupAt: time.Now(),
|
||||
},
|
||||
|
20
pkg/navi/logging.go
Normal file
20
pkg/navi/logging.go
Normal file
@ -0,0 +1,20 @@
|
||||
package navi
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
var accessLogger *log.Logger
|
||||
|
||||
func InitializeLogging() {
|
||||
accessLogger = log.New(&lumberjack.Logger{
|
||||
Filename: viper.GetString("logging.access"),
|
||||
MaxSize: 10,
|
||||
MaxBackups: 3,
|
||||
MaxAge: 30,
|
||||
Compress: true,
|
||||
}, "", 0)
|
||||
}
|
@ -1,15 +1,16 @@
|
||||
package navi
|
||||
|
||||
import (
|
||||
"github.com/spf13/viper"
|
||||
"bufio"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"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"`
|
||||
StartupAt time.Time `json:"startup_at"`
|
||||
}
|
||||
@ -42,22 +43,28 @@ func (v *RoadMetrics) AddTrace(trace RoadTrace) {
|
||||
} 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") {
|
||||
clear(v.Traffic)
|
||||
}
|
||||
if len(v.TrafficFrom) > viper.GetInt("performance.traces_limit") {
|
||||
clear(v.TrafficFrom)
|
||||
}
|
||||
if len(v.Traces) > viper.GetInt("performance.traces_limit") {
|
||||
clear(v.Traces)
|
||||
}
|
||||
raw, _ := jsoniter.Marshal(trace)
|
||||
accessLogger.Println(string(raw))
|
||||
}
|
||||
|
||||
func (v *RoadMetrics) ReadTrace() []RoadTrace {
|
||||
fp := viper.GetString("logging.access")
|
||||
file, err := os.Open(fp)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var out []RoadTrace
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
var entry RoadTrace
|
||||
if err := jsoniter.Unmarshal([]byte(line), &entry); err == nil {
|
||||
out = append(out, entry)
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
Reference in New Issue
Block a user