forked from pinpoint-apm/pinpoint-go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
148 lines (128 loc) · 3.51 KB
/
command.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
package pinpoint
import (
pb "github.com/pinpoint-apm/pinpoint-go-agent/protobuf"
"io"
"sync"
"sync/atomic"
"time"
)
var (
gAtcStreamCount int32
realTimeActiveSpan sync.Map
)
type activeSpanInfo struct {
startTime time.Time
txId string
entryPoint string
sampled bool
}
func (agent *agent) runCommandService() {
Log("cmd").Infof("start command goroutine")
defer agent.wg.Done()
gAtcStreamCount = 0
for agent.enable {
stream := agent.cmdGrpc.newCommandStreamWithRetry()
err := stream.sendCommandMessage()
if err != nil {
if err != io.EOF {
Log("cmd").Errorf("send command message - %v", err)
}
stream.close()
continue
}
for agent.enable {
cmdReq, err := stream.recvCommandRequest()
if err != nil {
if agent.enable && err != io.EOF {
Log("cmd").Warnf("recv command request - %v", err)
}
break
}
reqId := cmdReq.GetRequestId()
Log("cmd").Infof("command request: %v, %v", cmdReq, reqId)
switch cmdReq.Command.(type) {
case *pb.PCmdRequest_CommandEcho:
msg := cmdReq.GetCommandEcho().GetMessage()
agent.cmdGrpc.sendEcho(reqId, msg)
break
case *pb.PCmdRequest_CommandActiveThreadCount:
atcStream := agent.cmdGrpc.newActiveThreadCountStream(reqId)
go agent.sendActiveThreadCount(atcStream)
break
case *pb.PCmdRequest_CommandActiveThreadDump:
if c := cmdReq.GetCommandActiveThreadDump(); c != nil {
limit := c.GetLimit()
threadName := c.GetThreadName()
localId := c.GetLocalTraceId()
agent.cmdGrpc.sendActiveThreadDump(reqId, limit, threadName, localId, dumpGoroutine())
}
break
case *pb.PCmdRequest_CommandActiveThreadLightDump:
if c := cmdReq.GetCommandActiveThreadLightDump(); c != nil {
agent.cmdGrpc.sendActiveThreadLightDump(reqId, c.GetLimit(), dumpGoroutine())
}
break
case nil:
default:
break
}
}
stream.close()
}
Log("cmd").Infof("end command goroutine")
}
func (agent *agent) sendActiveThreadCount(s *activeThreadCountStream) {
atomic.AddInt32(&gAtcStreamCount, 1)
Log("cmd").Infof("active thread count stream goroutine start: %d, %d", s.reqId, gAtcStreamCount)
for agent.enable {
err := s.sendActiveThreadCount()
if err != nil {
if err != io.EOF {
Log("cmd").Errorf("send active thread count - %d, %v", s.reqId, err)
}
break
}
time.Sleep(1 * time.Second)
}
s.close()
atomic.AddInt32(&gAtcStreamCount, -1)
Log("cmd").Infof("active thread count stream goroutine finish: %d, %d", s.reqId, gAtcStreamCount)
}
func addRealTimeSampledActiveSpan(span *span) {
if gAtcStreamCount > 0 {
span.goroutineId = curGoroutineID()
s := &activeSpanInfo{span.startTime, span.txId.String(), span.rpcName, true}
realTimeActiveSpan.Store(span.goroutineId, s)
}
}
func dropRealTimeSampledActiveSpan(span *span) {
realTimeActiveSpan.Delete(span.goroutineId)
}
func addRealTimeUnSampledActiveSpan(span *noopSpan) {
if gAtcStreamCount > 0 {
span.goroutineId = curGoroutineID()
s := &activeSpanInfo{span.startTime, "", span.rpcName, false}
realTimeActiveSpan.Store(span.goroutineId, s)
}
}
func dropRealTimeUnSampledActiveSpan(span *noopSpan) {
realTimeActiveSpan.Delete(span.goroutineId)
}
func getActiveSpanCount(now time.Time) []int32 {
counts := []int32{0, 0, 0, 0}
realTimeActiveSpan.Range(func(k, v interface{}) bool {
s := v.(*activeSpanInfo)
d := now.Sub(s.startTime).Seconds()
if d < 1 {
counts[0]++
} else if d < 3 {
counts[1]++
} else if d < 5 {
counts[2]++
} else {
counts[3]++
}
return true
})
return counts
}