-
Notifications
You must be signed in to change notification settings - Fork 74
/
AgentRouter.go
282 lines (247 loc) · 7.23 KB
/
AgentRouter.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
package agent
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/pinpoint-apm/pinpoint-c-agent/collector-agent/common"
log "github.com/sirupsen/logrus"
)
type RawPacket struct {
Type uint32
RawData []byte
}
type I_PacketRouter interface {
DispatchPacket(*RawPacket) error
Clean()
}
type AgentRouter struct {
AgentMap map[string]*GrpcAgent
PingId int32
Quit chan bool
WG *sync.WaitGroup
rwMutex sync.RWMutex
}
type TSpanEvent struct {
Name string `json:"name"`
ExceptionInfo string `json:"EXP,omitempty"`
DestinationId string `json:"dst,omitempty"`
NextSpanId int64 `json:"nsid,string,omitempty"`
EndPoint string `json:"server,omitempty"`
StartElapsed int32 `json:"S"`
EndElapsed int32 `json:"E"`
StartElapsedV2 int32 `json:":S"`
EndElapsedV2 int32 `json:":E"`
ServiceType int32 `json:"stp,string"`
Clues []string `json:"clues,omitempty"`
Calls []TSpanEvent `json:"calls,omitempty"`
SqlMeta string `json:"SQL,omitempty"`
}
func (spanEv *TSpanEvent) GetEndElapsed() int32 {
if spanEv.EndElapsedV2 != 0 {
return spanEv.EndElapsedV2
} else {
return spanEv.EndElapsed
}
}
func (spanEv *TSpanEvent) GetStartElapsed() int32 {
if spanEv.StartElapsedV2 != 0 {
return spanEv.StartElapsedV2
} else {
return spanEv.StartElapsed
}
}
type TErrorInfo struct {
Msg string `json:"msg"`
File string `json:"file"`
Line int `json:"line"`
}
type TSpan struct {
AppServerType int32 `json:"FT"`
AppServerTypeV2 int32 `json:":FT"`
ParentAppServerType int32 `json:"ptype,string"`
ParentSpanId int64 `json:"psid,string"`
ParentApplicationName string `json:"pname"`
StartTime int64 `json:"S"`
StartTimeV2 int64 `json:":S"`
ElapsedTime int32 `json:"E"`
ElapsedTimeV2 int32 `json:":E"`
AppId string `json:"appid"`
AppIdV2 string `json:":appid"`
AppName string `json:"appname"`
AppNameV2 string `json:":appname"`
Calls []TSpanEvent `json:"calls"`
Clues []string `json:"clues,omitempty"`
SpanName string `json:"name"`
SpanId int64 `json:"sid,string"`
ServerType int32 `json:"stp,string"`
TransactionId string `json:"tid"`
Uri string `json:"uri"`
UT string `json:"UT,omitempty"`
EndPoint string `json:"server"`
RemoteAddr string `json:"client"`
AcceptorHost string `json:"Ah"`
ExceptionInfo string `json:"EXP,omitempty"`
ErrorInfo *TErrorInfo `json:"ERR,omitempty"`
NginxHeader string `json:"NP,omitempty"`
ApacheHeader string `json:"AP,omitempty"`
}
func (span *TSpan) IsFailed() bool {
if span.ErrorInfo != nil || len(span.ExceptionInfo) > 0 {
return true
}
return false
}
//note
// FindHistogramLevel must come with histogramSize
func (span *TSpan) FindHistogramLevel() int {
if span.GetElapsedTime() <= 100 {
return 0
} else if span.GetElapsedTime() <= 300 {
return 1
} else if span.GetElapsedTime() <= 500 {
return 2
} else if span.GetElapsedTime() <= 1000 {
return 3
} else if span.GetElapsedTime() <= 3000 {
return 4
} else if span.GetElapsedTime() <= 5000 {
return 5
} else if span.GetElapsedTime() <= 8000 {
return 6
} else {
return 7
}
}
func (span *TSpan) GetAppServerType() int32 {
if span.AppServerTypeV2 != 0 {
return span.AppServerTypeV2
} else {
return span.AppServerType
}
}
func (span *TSpan) GetElapsedTime() int32 {
if span.ElapsedTimeV2 != 0 {
return span.ElapsedTimeV2
} else {
return span.ElapsedTime
}
}
func (span *TSpan) GetStartTime() int64 {
if span.StartTimeV2 != 0 {
return span.StartTimeV2
} else {
return span.StartTime
}
}
func (span *TSpan) GetAppid() string {
if len(span.AppIdV2) > 0 {
return span.AppIdV2
} else {
return span.AppId
}
}
func (span *TSpan) GetAppName() string {
if len(span.AppNameV2) > 0 {
return span.AppNameV2
} else {
return span.AppName
}
}
func (manager *AgentRouter) Clean() {
config := common.GetConfig()
ctime := time.Now().Unix()
manager.rwMutex.RLock()
for id, agent := range manager.AgentMap {
if agent.GetLastBusyTime()+int64(config.AgentFreeOnlineSurvivalTimeSec) < ctime {
log.Warnf("agent:%s expired after:%d sec. busyTime:%d", agent, config.AgentFreeOnlineSurvivalTimeSec, agent.GetLastBusyTime())
manager.rwMutex.RUnlock()
manager.rwMutex.Lock()
delete(manager.AgentMap, id)
agent.Stop()
manager.rwMutex.Unlock()
manager.rwMutex.RLock()
}
}
manager.rwMutex.RUnlock()
}
//todo rename createAgent
func (manager *AgentRouter) createAgent(id, name string, agentType int32, startTime string) *GrpcAgent {
agent := GrpcAgent{PingId: manager.PingId, AgentOnLine: false}
manager.PingId += 1
agent.Init(id, name, agentType, startTime)
agent.Start()
log.Infof("agent:%v is launched", &agent)
return &agent
}
func GetAgentInfo(span *TSpan) (appid, name string, appServerType int32, startTime string, err error) {
// new feat: get current startTime
startTime = strconv.FormatInt(common.GetConfig().StartTime, 10) + "000"
holder := strings.Split(span.TransactionId, "^")
if len(holder) < 3 {
log.Warn("tid in wrong format")
} else if len(holder[1]) <= 10 { // seconds format
startTime = holder[1] + "000"
} else { // milliseconds format
startTime = holder[1]
}
appid = span.GetAppid()
if len(appid) == 0 {
return "", "", 0, "", errors.New("no appid")
}
name = span.GetAppName()
if len(name) == 0 {
return "", "", 0, "", errors.New("no appName")
}
appServerType = span.GetAppServerType()
if appServerType == 0 {
return "", "", 0, "", errors.New("no AppServerType(FT)")
}
return appid, name, appServerType, startTime, nil
}
func (manager *AgentRouter) DispatchPacket(packet *RawPacket) error {
//note: set default var of TSpan
span := &TSpan{
// ParentSpanId:-1 is part of logic in pinpoint
ParentSpanId: -1,
}
if err := json.Unmarshal(packet.RawData, span); err != nil {
log.Warnf("json.Unmarshal err:%v", err)
goto PACKET_INVALID
}
if appid, appName, serverType, startTime, err := GetAgentInfo(span); err == nil {
manager.rwMutex.RLock()
log.Debug("Read-lock is holding")
agent, OK := manager.AgentMap[appid]
if !OK {
// create a new agent
manager.rwMutex.RUnlock()
log.Infof("agent:%s not find, create a new agent.", appid)
log.Debug("Try to get write-lock")
manager.rwMutex.Lock()
log.Debug("Write-lock is holding")
if _t, OK := manager.AgentMap[appid]; OK {
agent = _t
} else {
agent = manager.createAgent(appid, appName, serverType, startTime)
}
manager.AgentMap[appid] = agent
manager.rwMutex.Unlock()
log.Debug("Write-lock is release")
} else {
manager.rwMutex.RUnlock()
log.Debug("Read-lock is release")
}
agent.CheckValid(span)
agent.SendSpan(span)
return nil
} else {
log.Warn(err)
return err
}
PACKET_INVALID:
return fmt.Errorf("input packet invalid %s", packet.RawData)
}