-
Notifications
You must be signed in to change notification settings - Fork 18
/
schedule.go
345 lines (302 loc) · 10.6 KB
/
schedule.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2020 tree xie
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package schedule
import (
"context"
"strconv"
"time"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog"
"github.com/vicanso/go-performance"
"github.com/vicanso/tiny-site/cs"
"github.com/vicanso/tiny-site/email"
"github.com/vicanso/tiny-site/helper"
"github.com/vicanso/tiny-site/log"
"github.com/vicanso/tiny-site/request"
routerconcurrency "github.com/vicanso/tiny-site/router_concurrency"
"github.com/vicanso/tiny-site/service"
"github.com/vicanso/tiny-site/util"
)
type (
taskFn func() error
statsTaskFn func() map[string]interface{}
)
const logCategory = "schedule"
func init() {
c := cron.New()
_, _ = c.AddFunc("@every 1m", redisPing)
_, _ = c.AddFunc("@every 1m", entPing)
_, _ = c.AddFunc("@every 1m", influxdbPing)
_, _ = c.AddFunc("@every 1m", configRefresh)
_, _ = c.AddFunc("@every 1m", redisStats)
_, _ = c.AddFunc("@every 1m", entStats)
_, _ = c.AddFunc("@every 1m", influxdbStats)
_, _ = c.AddFunc("@every 30s", cpuUsageStats)
_, _ = c.AddFunc("@every 1m", performanceStats)
_, _ = c.AddFunc("@every 1m", httpInstanceStats)
_, _ = c.AddFunc("@every 1m", routerConcurrencyStats)
// 如果是开发环境,则不执行定时任务
if util.IsDevelopment() {
return
}
c.Start()
}
func doTask(desc string, fn taskFn) {
startedAt := time.Now()
err := fn()
use := time.Since(startedAt)
if err != nil {
log.Error(context.Background()).
Str("category", logCategory).
Dur("use", use).
Err(err).
Msg(desc + " fail")
email.AlarmError(context.Background(), desc+" fail, "+err.Error())
} else {
log.Info(context.Background()).
Str("category", logCategory).
Dur("use", use).
Msg(desc + " success")
}
}
func doStatsTask(desc string, fn statsTaskFn) {
startedAt := time.Now()
stats := fn()
log.Info(context.Background()).
Str("category", logCategory).
Dur("use", time.Since(startedAt)).
Dict("stats", zerolog.Dict().Fields(stats)).
Msg("")
}
func redisPing() {
doTask("redis ping", helper.RedisPing)
}
func configRefresh() {
configSrv := new(service.ConfigurationSrv)
doTask("config refresh", configSrv.Refresh)
}
func redisStats() {
doStatsTask("redis stats", func() map[string]interface{} {
// 统计中除了redis数据库的统计,还有当前实例的统计指标,因此所有实例都会写入统计
stats := helper.RedisStats()
helper.GetInfluxDB().Write(cs.MeasurementRedisStats, nil, stats)
return stats
})
}
func entPing() {
doTask("ent ping", helper.EntPing)
}
// entStats ent的性能统计
func entStats() {
doStatsTask("ent stats", func() map[string]interface{} {
stats := helper.EntGetStats()
helper.GetInfluxDB().Write(cs.MeasurementEntStats, nil, stats)
return stats
})
}
// cpuUsageStats cpu使用率
func cpuUsageStats() {
doTask("update cpu usage", func() error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
return service.UpdateCPUUsage(ctx)
})
}
// influxdbStats influxdb统计
func influxdbStats() {
doStatsTask("influxdb stats", func() map[string]interface{} {
db := helper.GetInfluxDB()
writeCount := db.GetAndResetWriteCount()
writingCount := db.GetWritingCount()
fields := map[string]interface{}{
cs.FieldProcessing: writingCount,
cs.FieldCount: writeCount,
}
db.Write(cs.MeasurementInfluxdbStats, nil, fields)
return fields
})
}
// prevMemFrees 上一次 memory objects 释放的数量
var prevMemFrees uint64
// 上一次memory objects的申请数量
var prevMemMallocs uint64
// prevNumGC 上一次 gc 的次数
var prevNumGC uint32
// prevPauseTotal 上一次 pause 的总时长
var prevPauseTotal time.Duration
// 上一次的 io counter记录
var prevIOCountersStat = &performance.IOCountersStat{}
// 上一次context切换记录
var prevNumCtxSwitchesStat = &performance.NumCtxSwitchesStat{}
// 上一次的page faults记录
var prevPageFaultsStat = &performance.PageFaultsStat{}
const mb = 1024 * 1024
// performanceStats 系统性能
func performanceStats() {
doStatsTask("performance stats", func() map[string]interface{} {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
data := service.GetPerformance(ctx)
fields := map[string]interface{}{
cs.FieldGoMaxProcs: data.GoMaxProcs,
cs.FieldThreadCount: int(data.ThreadCount),
cs.FieldRoutineCount: data.RoutineCount,
cs.FieldProcessing: int(data.Concurrency),
// CPU使用率相关
cs.FieldCPUUsage: int(data.CPUUsage),
cs.FieldCPUUser: data.CPUUser,
cs.FieldCPUIdle: data.CPUIdle,
cs.FieldCPUNice: data.CPUNice,
cs.FieldCPUIowait: data.CPUIowait,
cs.FieldCPUIrq: data.CPUIrq,
cs.FieldCPUSoftirq: data.CPUSoftirq,
cs.FieldCPUSteal: data.CPUSteal,
cs.FieldCPUGuest: data.CPUGuest,
cs.FieldCPUGuestNice: data.CPUGuestNice,
// 内存使用相关
cs.FieldMemAlloc: data.MemAlloc,
cs.FieldMemTotalAlloc: data.MemTotalAlloc,
cs.FieldMemSys: data.MemSys,
cs.FieldMemLookups: data.MemLookups,
cs.FieldMemMallocs: int(data.MemMallocs - prevMemMallocs),
cs.FieldMemFrees: int(data.MemFrees - prevMemFrees),
cs.FieldMemHeapAlloc: data.MemHeapAlloc,
cs.FieldMemHeapSys: data.MemHeapSys,
cs.FieldMemHeapIdle: data.MemHeapIdle,
cs.FieldMemHeapInuse: data.MemHeapInuse,
cs.FieldMemHeapReleased: data.MemHeapReleased,
cs.FieldMemHeapObjects: data.MemHeapObjects,
cs.FieldMemStackInuse: data.MemStackInuse,
cs.FieldMemStackSys: data.MemStackSys,
cs.FieldMemMSpanInuse: data.MemMSpanInuse,
cs.FieldMemMSpanSys: data.MemMSpanSys,
cs.FieldMemMCacheInuse: data.MemMCacheInuse,
cs.FieldMemMCacheSys: data.MemMCacheSys,
cs.FieldMemBuckHashSys: data.MemBuckHashSys,
cs.FieldMemGCSys: data.MemGCSys,
cs.FieldMemOtherSys: data.MemOtherSys,
cs.FieldNumGC: int(data.NumGC - prevNumGC),
cs.FieldPauseMS: int((data.PauseTotalNs - prevPauseTotal).Milliseconds()),
cs.FieldConnProcessing: int(data.HTTPServerConnStats.ConnProcessing),
cs.FieldConnAlive: int(data.HTTPServerConnStats.ConnAlive),
cs.FieldConnCreatedCount: int(data.HTTPServerConnStats.ConnCreatedCount),
}
// io 相关统计
if data.IOCountersStat != nil {
readCount := data.IOCountersStat.ReadCount - prevIOCountersStat.ReadCount
writeCount := data.IOCountersStat.WriteCount - prevIOCountersStat.WriteCount
readBytes := data.IOCountersStat.ReadBytes - prevIOCountersStat.ReadBytes
writeBytes := data.IOCountersStat.WriteBytes - prevIOCountersStat.WriteBytes
prevIOCountersStat = data.IOCountersStat
fields[cs.FieldIOReadCount] = readCount
fields[cs.FieldIOWriteCount] = writeCount
fields[cs.FieldIOReadMBytes] = readBytes / mb
fields[cs.FieldIOWriteMBytes] = writeBytes / mb
}
// 网络相关
if data.ConnStat != nil {
count := make(map[string]string)
for k, v := range data.ConnStat.Status {
// 如果该状态下对应的连接大于0,则记录此连接数
if v > 0 {
fields[cs.FieldConnTotal+k] = v
}
count[k] = strconv.Itoa(v)
}
for k, v := range data.ConnStat.RemoteAddr {
count[k] = strconv.Itoa(v)
}
log.Info(context.Background()).
Str("category", "connStat").
Dict("count", log.Struct(count)).
Msg("")
fields[cs.FieldConnTotal] = data.ConnStat.Count
}
// context切换相关
if data.NumCtxSwitchesStat != nil {
fields[cs.FieldCtxSwitchesVoluntary] = data.NumCtxSwitchesStat.Voluntary - prevNumCtxSwitchesStat.Voluntary
fields[cs.FieldCtxSwitchesInvoluntary] = data.NumCtxSwitchesStat.Involuntary - prevNumCtxSwitchesStat.Involuntary
prevNumCtxSwitchesStat = data.NumCtxSwitchesStat
}
// page fault相关
if data.PageFaultsStat != nil {
fields[cs.FieldPageFaultMinor] = data.PageFaultsStat.MinorFaults - prevPageFaultsStat.MinorFaults
fields[cs.FieldPageFaultMajor] = data.PageFaultsStat.MajorFaults - prevPageFaultsStat.MajorFaults
fields[cs.FieldPageFaultChildMinor] = data.PageFaultsStat.ChildMinorFaults - prevPageFaultsStat.ChildMinorFaults
fields[cs.FieldPageFaultChildMajor] = data.PageFaultsStat.ChildMajorFaults - prevPageFaultsStat.ChildMajorFaults
prevPageFaultsStat = data.PageFaultsStat
}
// fd 相关
if data.NumFdsStat != nil {
fields[cs.FieldNumFds] = data.NumFdsStat.Fds
}
// open files的统计
if data.OpenFilesStats != nil &&
len(data.OpenFilesStats.OpenFiles) != 0 {
log.Info(context.Background()).
Str("category", "openFiles").
Dict("stat", log.Struct(data.OpenFilesStats)).
Msg("")
}
prevMemMallocs = data.MemMallocs
prevMemFrees = data.MemFrees
prevNumGC = data.NumGC
prevPauseTotal = data.PauseTotalNs
helper.GetInfluxDB().Write(cs.MeasurementPerformance, nil, fields)
return fields
})
}
// httpInstanceStats http instance stats
func httpInstanceStats() {
doStatsTask("http instance stats", func() map[string]interface{} {
fields := make(map[string]interface{})
statsList := request.GetHTTPStats()
for _, stats := range statsList {
helper.GetInfluxDB().Write(cs.MeasurementHTTPInstanceStats, map[string]string{
cs.TagService: stats.Name,
}, map[string]interface{}{
cs.FieldMaxConcurrency: stats.MaxConcurrency,
cs.FieldProcessing: stats.Concurrency,
})
fields[stats.Name+":"+cs.FieldMaxConcurrency] = stats.MaxConcurrency
fields[stats.Name+":"+cs.FieldProcessing] = stats.Concurrency
}
return fields
})
}
// influxdbPing influxdb ping
func influxdbPing() {
doTask("influxdb ping", helper.GetInfluxDB().Health)
}
// routerConcurrencyStats router concurrency stats
func routerConcurrencyStats() {
doStatsTask("router concurrency stats", func() map[string]interface{} {
result := routerconcurrency.GetLimiter().GetStats()
fields := make(map[string]interface{})
influxSrv := helper.GetInfluxDB()
for key, value := range result {
// 如果并发为0,则不记录
if value == 0 {
continue
}
fields[key] = value
influxSrv.Write(cs.MeasurementRouterConcurrency, map[string]string{
cs.TagRoute: key,
}, map[string]interface{}{
cs.FieldCount: int(value),
})
}
return fields
})
}