-
Notifications
You must be signed in to change notification settings - Fork 6
/
error_collection.go
130 lines (108 loc) · 2.97 KB
/
error_collection.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
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/huajiao-tv/pepperbus/utility/msgRedis"
)
const (
// ErrorCollectionKeyFMT 定义错误收集存储的 Key 名字
ErrorCollectionKeyFMT = "EC:%s:%s"
)
// ErrorCollection 收集 CGI 错误信息入存储
type ErrorCollection struct {
c *StorageConfig
redisCli *msgRedis.MultiPool
address string
}
// ErrorCollectionData 收集的结果结构
type ErrorCollectionData struct {
Time string
Jobs string
SysCode string
SysError string
UserCode string
UserMsg string
}
// NewErrorCollection 新建一个收集实例
func NewErrorCollection(c *StorageConfig) *ErrorCollection {
errorCollection := &ErrorCollection{
c: c,
}
// 初始化 Redis
switch c.Type {
case "redis":
redisAddressWithAuth := fmt.Sprintf("%s:%s", c.Address, c.Auth)
errorCollection.redisCli = msgRedis.NewMultiPool(
[]string{redisAddressWithAuth},
c.MaxConnNum,
c.MaxIdleNum,
c.MaxIdleSeconds,
)
errorCollection.address = redisAddressWithAuth
}
return errorCollection
}
// AddError 记录一条错误,当 Enable 为 False 时,直接返回
//
func (ec *ErrorCollection) AddError(queue QueueName, topic TopicName,
jobs *Jobs, respCode string, respErrorCode string, respBody string, errMsg error) {
if !netQueueConf().ErrorCollectionEnable {
return
}
key := fmt.Sprintf(ErrorCollectionKeyFMT, queue, topic)
tmp := respBody
hostname, _ := os.Hostname()
respBody = fmt.Sprintf("%s ------ [Host] %s", tmp, hostname)
var errorInfo string
if errMsg != nil {
errorInfo = fmt.Sprintf("%v,,,,%s,,,,%s,,,,%s,,,,%s,,,,%s", time.Now().Unix(), jobs, respCode, respErrorCode, respBody, errMsg)
} else {
errorInfo = fmt.Sprintf("%v,,,,%s,,,,%s,,,,%s,,,,%s", time.Now().Unix(), jobs, respCode, respErrorCode, respBody)
}
_, err := ec.redisCli.Call(ec.address).LPUSH(key, []string{errorInfo})
if err != nil {
flog.Error("ErrorCollection AddError Failed", err)
}
}
// GetError 返回存储的错误信息,有条数限制,默认为最新的10条
func (ec *ErrorCollection) GetError(queue QueueName, topic TopicName, length int) []*ErrorCollectionData {
if !netQueueConf().ErrorCollectionEnable {
return nil
}
if length <= 0 {
length = 10
}
key := fmt.Sprintf(ErrorCollectionKeyFMT, queue, topic)
tmp, err := ec.redisCli.Call(ec.address).LRANGE(key, 0, length-1)
if err != nil {
flog.Error("redisCli GetError Failed ", ec.address, err)
return nil
}
var ret []*ErrorCollectionData
for _, td := range tmp {
tb, ok := td.([]byte)
if !ok {
continue
}
data := string(tb)
splitData := strings.Split(data, ",,,,")
splitDataLen := len(splitData)
if splitDataLen != 6 && splitDataLen != 5 {
continue
}
errorData := &ErrorCollectionData{
Time: splitData[0],
Jobs: splitData[1],
SysCode: splitData[2],
UserCode: splitData[3],
UserMsg: splitData[4],
}
if splitDataLen == 6 {
errorData.SysError = splitData[5]
}
ret = append(ret, errorData)
}
return ret
}