-
Notifications
You must be signed in to change notification settings - Fork 22
/
stats.go
42 lines (38 loc) · 927 Bytes
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"encoding/json"
"time"
)
type MonitoringPath struct {
Path string
Count float64
TotalDuration float64
AverageTime float64
}
func (t *myTransport) updateStats(parsedRequest ModifiedRequest, elapsed time.Duration) {
key := parsedRequest.RemoteAddr + "-" + parsedRequest.Path
t.statsMu.Lock()
defer t.statsMu.Unlock()
if val, ok := t.stats[key]; ok {
val.Count = val.Count + 1
val.TotalDuration += elapsed.Seconds()
val.AverageTime = val.TotalDuration / val.Count
t.stats[key] = val
} else {
var m MonitoringPath
m.Path = parsedRequest.Path
m.Count = 1
m.TotalDuration = elapsed.Seconds()
m.AverageTime = m.TotalDuration / m.Count
t.stats[key] = m
}
}
func (t *myTransport) getStats() ([]byte, error) {
t.statsMu.RLock()
defer t.statsMu.RUnlock()
b, err := json.MarshalIndent(t.stats, "", " ")
if err != nil {
return nil, err
}
return b, nil
}