/
api-component.go
301 lines (283 loc) · 7.42 KB
/
api-component.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
// Copyright 2021 冯立强 fenglq@tingyun.com. All rights reserved.
package tingyun3
import (
"net/url"
"time"
)
// Component : 构成事务的组件过程
type Component struct {
action *Action
parent *Component
method string
classname string
txdata string
protocol string
vender string
instance string
table string
op string
callStack []string
errors []*errInfo
time timeRange
tracerID int32
tracerParentID int32
statusCode int32
exID bool
_type uint8
}
func (c *Component) getVender() string {
if c == nil {
return ""
}
if len(c.vender) > 0 {
return c.vender
}
switch c._type {
case ComponentDefaultDB:
return "DB"
case ComponentMysql:
return "MySQL"
case ComponentPostgreSQL:
return "PostgreSQL"
case ComponentMSSQL:
return "MSSQL"
case ComponentSQLite:
return "SQLite"
case ComponentOracle:
return "Oracle"
case ComponentMongo:
return "MongoDB"
case ComponentRedis:
return "Redis"
case ComponentMQC:
return "MQC"
case ComponentMQP:
return "MQP"
case ComponentMemCache:
return "Memcache"
case ComponentExternal:
case ComponentDefault:
default:
}
return ""
}
// GetAction : 取对应的事务对象
func (c *Component) GetAction() *Action {
if c == nil {
return nil
}
return c.action
}
func (c *Component) SetURL(url string) {
if c == nil {
return
}
if c.action == nil {
return
}
if c._type != ComponentExternal {
return
}
c.protocol, c.op = parseURL(url)
if len(c.op) > 0 {
if c.op[0] != '/' {
c.op = "/" + c.op
}
}
}
func (c *Component) SetStatusCode(statusCode int) {
if c == nil {
return
}
if c.action == nil {
return
}
c.statusCode = int32(statusCode)
}
func (c *Component) setError(e interface{}, errType string, isError bool) {
if c == nil {
return
} //errorTrace 聚合,以 callstack + message
errTime := time.Now()
c.errors = append(c.errors, &errInfo{errTime, patchSize(toString(e), 4000), callStack(1), errType, isError})
}
// SetError : 组件错误捕获
func (c *Component) SetError(e interface{}, errType string, skipStack int) {
if skipStack < 0 {
skipStack = 0
}
if c == nil {
return
} //errorTrace 聚合,以 callstack + message
errTime := time.Now()
c.errors = append(c.errors, &errInfo{errTime, patchSize(toString(e), 4000), callStack(skipStack + 1), errType, true})
}
// SetException : 组件异常捕获
func (c *Component) SetException(e interface{}, errType string, skipStack int) {
if skipStack < 0 {
skipStack = 0
}
if c == nil {
return
} //errorTrace 聚合,以 callstack + message
errTime := time.Now()
c.errors = append(c.errors, &errInfo{errTime, patchSize(toString(e), 4000), callStack(skipStack + 1), errType, false})
}
// Finish : 停止组件计时
// 性能分解组件时长 = Finish时刻 - CreateComponent时刻
// 当时长超出堆栈阈值时,记录当前组件的代码堆栈
//go:noinline
func (c *Component) Finish() {
c.End(1)
}
func (c *Component) SetMethod(method string) {
if c != nil {
c.method = method
}
}
//End : 内部使用, skip为跳过的调用栈数
//go:noinline
func (c *Component) End(skip int) {
if c == nil {
return
}
c.FixStackEnd(skip+1, func(a string) bool {
return subString(a, 0, 21) == "github.com/TingYunGo/"
})
}
//End : 内部使用, skip为跳过的调用栈数
func (c *Component) FixStackEnd(skip int, checkRemovedFunction func(string) bool) {
if c != nil {
if c.action == nil {
return
}
if c.parent == nil && c != c.action.root {
c.parent = c.action.root
}
c.time.End()
if (c._type == ComponentDefault || c._type == ComponentMQC) && c.action.current == c && c != c.action.root {
c.action.current = c.parent
}
if len(c.callStack) > 0 {
return
}
if readServerConfigBool(configServerConfigBoolActionTracerStackTraceEnabled, true) {
//超阈值取callstack
if c.time.duration >= time.Duration(readServerConfigInt(configServerConfigIntegerActionTracerStacktraceThreshold, 500))*time.Millisecond {
c.callStack = validCallStack(skip+1, checkRemovedFunction)
}
}
}
}
//CreateTrackID : 跨应用追踪接口,用于调用端,生成一个跨应用追踪id,通过http头或者私有协议发送到被调用端
//
//返回值: 字符串,一个包含授权id,应用id,实例id,事务id等信息的追踪id
func (c *Component) CreateTrackID() string {
if app == nil || c == nil || c.action == nil || (c._type != ComponentExternal && c._type != ComponentMQP) {
return ""
}
if !readLocalConfigBool(configLocalBoolTransactionEnabled, true) {
return ""
}
if !readServerConfigBool(configServerConfigBoolActionTracerEnabled, false) {
return ""
}
//c=CALL_LIST;x=TRANSACTION_TRACE_GUID;e=EXTERNAL_TRACE_GUID;n=TRANSACTION_NAME_MD5;
if secID := app.configs.server.CStrings.Read(configServerStringTingyunIDSecret, ""); len(secID) != 0 {
c.exID = true
callList, transactionID := c.action.parseTrackID()
if len(callList) > 0 {
callList = "," + callList
}
if len(transactionID) == 0 {
transactionID = c.action.unicID()
}
return "c=S|" + secID + callList + ";x=" + transactionID + ";e=" + c.unicID() + ";n=" + md5sum(c.action.GetName()) + ";"
}
return ""
}
//SetTxData : 跨应用追踪接口,用于调用端,将被调用端返回的事务性能数据保存到外部调用组件
//
//参数: 被调用端返回的事务的性能数据
func (c *Component) SetTxData(txData string) {
if app == nil || c == nil || c.action == nil || c._type != ComponentExternal {
return
}
if !readLocalConfigBool(configLocalBoolTransactionEnabled, true) {
return
}
if readServerConfigBool(configServerConfigBoolActionTracerEnabled, false) {
c.txdata = txData
}
}
//AppendSQL : 用于数据库组件,通过此接口将sql查询语句保存到数据库组件,在报表慢事务追踪列表展示
//
//参数: sql语句
func (c *Component) AppendSQL(sql string) {
if app == nil || c == nil || c.action == nil ||
(c._type != ComponentExternal && c._type != ComponentDefaultDB && c._type != ComponentMysql && c._type != ComponentPostgreSQL && c._type != ComponentMSSQL && c._type != ComponentSQLite) {
return
}
c.op = fixSQL(sql)
}
// CreateComponent : 在函数/方法中调用其他函数/方法时,如果认为有必要,调用此方法测量子过程性能
func (c *Component) CreateComponent(method string) *Component {
if c == nil || c.action == nil || c._type != ComponentDefault || c._type != ComponentMQC {
return nil
}
if !c.action.checkComponent() {
return nil
}
r := &Component{
action: c.action,
parent: c.action.current,
method: url.QueryEscape(method),
callStack: nil,
tracerParentID: c.tracerID,
tracerID: c.action.makeTracerID(),
time: timeRange{time.Now(), -1},
exID: false,
statusCode: 0,
_type: ComponentDefault,
}
c.action.current = c
c.action.cache.Put(r)
return r
}
func (c *Component) destroy() {
if c == nil {
return
}
if c._type == componentUnused {
return
}
c.method = ""
c.parent = nil
c.classname = ""
c.txdata = ""
c.protocol = ""
c.vender = ""
c.instance = ""
c.table = ""
c.op = ""
c.callStack = nil
c.errors = nil
c.action = nil
c._type = componentUnused
}
//FixBegin : 校正事务开始时间
func (c *Component) FixBegin(begin time.Time) {
if c == nil {
return
}
c.time.begin = begin
}
func (c *Component) unicID() string {
if c == nil {
return ""
}
if c.exID {
return unicID(c.time.begin, c)
}
return ""
}