forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
210 lines (183 loc) · 4.55 KB
/
metrics.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package stat
import (
"os"
"sync"
"time"
"github.com/tal-tech/go-zero/core/executors"
"github.com/tal-tech/go-zero/core/logx"
)
var (
LogInterval = time.Minute
writerLock sync.Mutex
reportWriter Writer = nil
)
type (
Writer interface {
Write(report *StatReport) error
}
StatReport struct {
Name string `json:"name"`
Timestamp int64 `json:"tm"`
Pid int `json:"pid"`
ReqsPerSecond float32 `json:"qps"`
Drops int `json:"drops"`
Average float32 `json:"avg"`
Median float32 `json:"med"`
Top90th float32 `json:"t90"`
Top99th float32 `json:"t99"`
Top99p9th float32 `json:"t99p9"`
}
Metrics struct {
executor *executors.PeriodicalExecutor
container *metricsContainer
}
)
func SetReportWriter(writer Writer) {
writerLock.Lock()
reportWriter = writer
writerLock.Unlock()
}
func NewMetrics(name string) *Metrics {
container := &metricsContainer{
name: name,
pid: os.Getpid(),
}
return &Metrics{
executor: executors.NewPeriodicalExecutor(LogInterval, container),
container: container,
}
}
func (m *Metrics) Add(task Task) {
m.executor.Add(task)
}
func (m *Metrics) AddDrop() {
m.executor.Add(Task{
Drop: true,
})
}
func (m *Metrics) SetName(name string) {
m.executor.Sync(func() {
m.container.name = name
})
}
type (
tasksDurationPair struct {
tasks []Task
duration time.Duration
drops int
}
metricsContainer struct {
name string
pid int
tasks []Task
duration time.Duration
drops int
}
)
func (c *metricsContainer) AddTask(v interface{}) bool {
if task, ok := v.(Task); ok {
if task.Drop {
c.drops++
} else {
c.tasks = append(c.tasks, task)
c.duration += task.Duration
}
}
return false
}
func (c *metricsContainer) Execute(v interface{}) {
pair := v.(tasksDurationPair)
tasks := pair.tasks
duration := pair.duration
drops := pair.drops
size := len(tasks)
report := &StatReport{
Name: c.name,
Timestamp: time.Now().Unix(),
Pid: c.pid,
ReqsPerSecond: float32(size) / float32(LogInterval/time.Second),
Drops: drops,
}
if size > 0 {
report.Average = float32(duration/time.Millisecond) / float32(size)
fiftyPercent := size >> 1
if fiftyPercent > 0 {
top50pTasks := topK(tasks, fiftyPercent)
medianTask := top50pTasks[0]
report.Median = float32(medianTask.Duration) / float32(time.Millisecond)
tenPercent := fiftyPercent / 5
if tenPercent > 0 {
top10pTasks := topK(tasks, tenPercent)
task90th := top10pTasks[0]
report.Top90th = float32(task90th.Duration) / float32(time.Millisecond)
onePercent := tenPercent / 10
if onePercent > 0 {
top1pTasks := topK(top10pTasks, onePercent)
task99th := top1pTasks[0]
report.Top99th = float32(task99th.Duration) / float32(time.Millisecond)
pointOnePercent := onePercent / 10
if pointOnePercent > 0 {
topPointOneTasks := topK(top1pTasks, pointOnePercent)
task99Point9th := topPointOneTasks[0]
report.Top99p9th = float32(task99Point9th.Duration) / float32(time.Millisecond)
} else {
report.Top99p9th = getTopDuration(top1pTasks)
}
} else {
mostDuration := getTopDuration(top10pTasks)
report.Top99th = mostDuration
report.Top99p9th = mostDuration
}
} else {
mostDuration := getTopDuration(tasks)
report.Top90th = mostDuration
report.Top99th = mostDuration
report.Top99p9th = mostDuration
}
} else {
mostDuration := getTopDuration(tasks)
report.Median = mostDuration
report.Top90th = mostDuration
report.Top99th = mostDuration
report.Top99p9th = mostDuration
}
}
log(report)
}
func (c *metricsContainer) RemoveAll() interface{} {
tasks := c.tasks
duration := c.duration
drops := c.drops
c.tasks = nil
c.duration = 0
c.drops = 0
return tasksDurationPair{
tasks: tasks,
duration: duration,
drops: drops,
}
}
func getTopDuration(tasks []Task) float32 {
top := topK(tasks, 1)
if len(top) < 1 {
return 0
} else {
return float32(top[0].Duration) / float32(time.Millisecond)
}
}
func log(report *StatReport) {
writeReport(report)
logx.Statf("(%s) - qps: %.1f/s, drops: %d, avg time: %.1fms, med: %.1fms, "+
"90th: %.1fms, 99th: %.1fms, 99.9th: %.1fms",
report.Name, report.ReqsPerSecond, report.Drops, report.Average, report.Median,
report.Top90th, report.Top99th, report.Top99p9th)
}
func writeReport(report *StatReport) {
writerLock.Lock()
defer writerLock.Unlock()
if reportWriter != nil {
if err := reportWriter.Write(report); err != nil {
logx.Error(err)
}
}
}