-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_stat.go
159 lines (135 loc) · 2.95 KB
/
url_stat.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package pinpoint
import (
"sync"
"time"
)
const (
urlStatusSuccess = 1
urlStatusFail = 2
urlStatBucketVersion = 0
urlStatBucketSize = 8
urlStatCollectInterval = 30 * time.Second
)
var (
clock tickClock
urlSnapshot *urlStatSnapshot
urlSnapshotLock sync.Mutex
)
func (agent *agent) initUrlStat() {
clock = newTickClock(urlStatCollectInterval)
urlSnapshot = agent.newUrlStatSnapshot()
}
type urlStat struct {
entry *UrlStatEntry
endTime time.Time
elapsed int64
}
type urlStatSnapshot struct {
urlMap map[urlKey]*eachUrlStat
config *Config
count int
}
type urlKey struct {
url string
tick time.Time
}
type eachUrlStat struct {
url string
totalHistogram *urlStatHistogram
failedHistogram *urlStatHistogram
tickTime time.Time
}
type urlStatHistogram struct {
total int64
max int64
histogram []int32
}
type tickClock struct {
interval time.Duration
}
func (agent *agent) newUrlStatSnapshot() *urlStatSnapshot {
return &urlStatSnapshot{
urlMap: make(map[urlKey]*eachUrlStat, 0),
config: agent.config,
}
}
func (agent *agent) currentUrlStatSnapshot() *urlStatSnapshot {
urlSnapshotLock.Lock()
defer urlSnapshotLock.Unlock()
return urlSnapshot
}
func (agent *agent) takeUrlStatSnapshot() *urlStatSnapshot {
urlSnapshotLock.Lock()
defer urlSnapshotLock.Unlock()
oldSnapshot := urlSnapshot
urlSnapshot = agent.newUrlStatSnapshot()
return oldSnapshot
}
func (snapshot *urlStatSnapshot) add(us *urlStat) {
key := urlKey{us.entry.Url, clock.tick(us.endTime)}
e, ok := snapshot.urlMap[key]
if !ok {
if snapshot.count >= snapshot.config.urlStatLimitSize {
return
}
e = newEachUrlStat(us.entry.Url, key.tick)
snapshot.urlMap[key] = e
snapshot.count++
}
e.totalHistogram.add(us.elapsed)
if urlStatStatus(us.entry.Status) == urlStatusFail {
e.failedHistogram.add(us.elapsed)
}
}
func newEachUrlStat(url string, tick time.Time) *eachUrlStat {
return &eachUrlStat{
url: url,
totalHistogram: newStatHistogram(),
failedHistogram: newStatHistogram(),
tickTime: tick,
}
}
func newStatHistogram() *urlStatHistogram {
return &urlStatHistogram{
histogram: make([]int32, urlStatBucketSize),
}
}
func (hg *urlStatHistogram) add(elapsed int64) {
hg.total += elapsed
if hg.max < elapsed {
hg.max = elapsed
}
hg.histogram[getBucket(elapsed)]++
}
func getBucket(elapsed int64) int {
if elapsed < 100 {
return 0
} else if elapsed < 300 {
return 1
} else if elapsed < 500 {
return 2
} else if elapsed < 1000 {
return 3
} else if elapsed < 3000 {
return 4
} else if elapsed < 5000 {
return 5
} else if elapsed < 8000 {
return 6
} else {
return 7
}
}
func newTickClock(interval time.Duration) tickClock {
return tickClock{interval}
}
func (t tickClock) tick(tm time.Time) time.Time {
return tm.Truncate(t.interval)
}
func urlStatStatus(status int) int {
if status/100 < 4 {
return urlStatusSuccess
} else {
return urlStatusFail
}
}