forked from uber/tchannel-go
/
introspection.go
305 lines (261 loc) · 9.88 KB
/
introspection.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
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tchannel
import (
"encoding/json"
"runtime"
"golang.org/x/net/context"
)
// IntrospectionOptions are the options used when introspecting the Channel.
type IntrospectionOptions struct {
// IncludeExchanges will include all the IDs in the message exchanges.
IncludeExchanges bool `json:"includeExchanges"`
// IncludeEmptyPeers will include peers, even if they have no connections.
IncludeEmptyPeers bool `json:"includeEmptyPeers"`
}
// RuntimeState is a snapshot of the runtime state for a channel.
type RuntimeState struct {
// LocalPeer is the local peer information (service name, host-port, etc).
LocalPeer LocalPeerInfo `json:"localPeer"`
// SubChannels contains information about any subchannels.
SubChannels map[string]SubChannelRuntimeState `json:"subChannels"`
// RootPeers contains information about all the peers on this channel and their connections.
RootPeers map[string]PeerRuntimeState `json:"rootPeers"`
// Peers is the list of shared peers for this channel.
Peers []string `json:"peers"`
// NumConnections is the number of connections stored in the channel.
NumConnections int `json:"numConnections"`
}
// GoRuntimeStateOptions are the options used when getting Go runtime state.
type GoRuntimeStateOptions struct {
// IncludeGoStacks will include all goroutine stacks.
IncludeGoStacks bool `json:"includeGoStacks"`
}
// GoRuntimeState is a snapshot of runtime stats from the runtime.
type GoRuntimeState struct {
MemStats runtime.MemStats `json:"memStats"`
NumGoroutines int `json:"numGoRoutines"`
NumCPU int `json:"numCPU"`
NumCGo int64 `json:"numCGo"`
GoStacks []byte `json:"goStacks,omitempty"`
}
// SubChannelRuntimeState is the runtime state for a subchannel.
type SubChannelRuntimeState struct {
Service string `json:"service"`
Isolated bool `json:"isolated"`
// IsolatedPeers is the list of all isolated peers for this channel.
IsolatedPeers []string `json:"isolatedPeers,omitempty"`
}
// ConnectionRuntimeState is the runtime state for a single connection.
type ConnectionRuntimeState struct {
ID uint32 `json:"id"`
ConnectionState string `json:"connectionState"`
LocalHostPort string `json:"localHostPort"`
RemoteHostPort string `json:"remoteHostPort"`
IsEphemeral bool `json:"isEphemeral"`
InboundExchange ExchangeRuntimeState `json:"inboundExchange"`
OutboundExchange ExchangeRuntimeState `json:"outboundExchange"`
}
// ExchangeRuntimeState is the runtime state for a message exchange set.
type ExchangeRuntimeState struct {
Name string `json:"name"`
Count int `json:"count"`
Exchanges []uint32 `json:"exchanges,omitempty"`
}
// PeerRuntimeState is the runtime state for a single peer.
type PeerRuntimeState struct {
HostPort string `json:"hostPort"`
OutboundConnections []ConnectionRuntimeState `json:"outboundConnections"`
InboundConnections []ConnectionRuntimeState `json:"inboundConnections"`
ChosenCount uint64 `json:"chosenCount"`
SCCount uint32 `json:"scCount"`
}
// IntrospectState returns the RuntimeState for this channel.
// Note: this is purely for debugging and monitoring, and may slow down your Channel.
func (ch *Channel) IntrospectState(opts *IntrospectionOptions) *RuntimeState {
mut := ch.mutable
mut.mut.RLock()
conns := len(ch.mutable.conns)
mut.mut.RUnlock()
return &RuntimeState{
LocalPeer: ch.PeerInfo(),
SubChannels: ch.subChannels.IntrospectState(opts),
RootPeers: ch.rootPeers().IntrospectState(opts),
Peers: ch.Peers().IntrospectList(opts),
NumConnections: conns,
}
}
type containsPeerList interface {
Copy() map[string]*Peer
}
func fromPeerList(peers containsPeerList, opts *IntrospectionOptions) map[string]PeerRuntimeState {
m := make(map[string]PeerRuntimeState)
for _, peer := range peers.Copy() {
peerState := peer.IntrospectState(opts)
if len(peerState.InboundConnections)+len(peerState.OutboundConnections) > 0 || opts.IncludeEmptyPeers {
m[peer.HostPort()] = peerState
}
}
return m
}
// IntrospectState returns the runtime state of the peer list.
func (l *PeerList) IntrospectState(opts *IntrospectionOptions) map[string]PeerRuntimeState {
return fromPeerList(l, opts)
}
// IntrospectState returns the runtime state of the
func (l *RootPeerList) IntrospectState(opts *IntrospectionOptions) map[string]PeerRuntimeState {
return fromPeerList(l, opts)
}
// IntrospectState returns the runtime state of the subchannels.
func (subChMap *subChannelMap) IntrospectState(opts *IntrospectionOptions) map[string]SubChannelRuntimeState {
m := make(map[string]SubChannelRuntimeState)
subChMap.mut.RLock()
for k, sc := range subChMap.subchannels {
state := SubChannelRuntimeState{
Service: k,
Isolated: sc.Isolated(),
}
if state.Isolated {
state.IsolatedPeers = sc.Peers().IntrospectList(opts)
}
m[k] = state
}
subChMap.mut.RUnlock()
return m
}
func getConnectionRuntimeState(conns []*Connection, opts *IntrospectionOptions) []ConnectionRuntimeState {
connStates := make([]ConnectionRuntimeState, len(conns))
for i, conn := range conns {
connStates[i] = conn.IntrospectState(opts)
}
return connStates
}
// IntrospectState returns the runtime state for this peer.
func (p *Peer) IntrospectState(opts *IntrospectionOptions) PeerRuntimeState {
p.mut.RLock()
defer p.mut.RUnlock()
return PeerRuntimeState{
HostPort: p.hostPort,
InboundConnections: getConnectionRuntimeState(p.inboundConnections, opts),
OutboundConnections: getConnectionRuntimeState(p.outboundConnections, opts),
ChosenCount: p.chosenCount,
SCCount: p.scCount,
}
}
// IntrospectState returns the runtime state for this connection.
func (c *Connection) IntrospectState(opts *IntrospectionOptions) ConnectionRuntimeState {
c.stateMut.RLock()
defer c.stateMut.RUnlock()
return ConnectionRuntimeState{
ID: c.connID,
ConnectionState: c.state.String(),
LocalHostPort: c.conn.LocalAddr().String(),
RemoteHostPort: c.conn.RemoteAddr().String(),
IsEphemeral: c.remotePeerInfo.IsEphemeral,
InboundExchange: c.inbound.IntrospectState(opts),
OutboundExchange: c.outbound.IntrospectState(opts),
}
}
// IntrospectState returns the runtime state for this messsage exchange set.
func (mexset *messageExchangeSet) IntrospectState(opts *IntrospectionOptions) ExchangeRuntimeState {
mexset.mut.RLock()
state := ExchangeRuntimeState{
Name: mexset.name,
Count: len(mexset.exchanges),
}
if opts.IncludeExchanges {
state.Exchanges = make([]uint32, 0, len(mexset.exchanges))
for k := range mexset.exchanges {
state.Exchanges = append(state.Exchanges, k)
}
}
mexset.mut.RUnlock()
return state
}
func getStacks() []byte {
var buf []byte
for n := 4096; n < 10*1024*1024; n *= 2 {
buf = make([]byte, n)
stackLen := runtime.Stack(buf, true /* all */)
if stackLen < n {
return buf
}
}
// return the first 10MB of stacks if we have more than 10MB.
return buf
}
func (ch *Channel) handleIntrospection(arg3 []byte) interface{} {
var opts IntrospectionOptions
json.Unmarshal(arg3, &opts)
return ch.IntrospectState(&opts)
}
// IntrospectList returns the list of peers (host:port) in this peer list.
func (l *PeerList) IntrospectList(opts *IntrospectionOptions) []string {
var peers []string
l.RLock()
for peer := range l.peersByHostPort {
peers = append(peers, peer)
}
l.RUnlock()
return peers
}
func handleInternalRuntime(arg3 []byte) interface{} {
var opts GoRuntimeStateOptions
json.Unmarshal(arg3, &opts)
state := GoRuntimeState{
NumGoroutines: runtime.NumGoroutine(),
NumCPU: runtime.NumCPU(),
NumCGo: runtime.NumCgoCall(),
}
runtime.ReadMemStats(&state.MemStats)
if opts.IncludeGoStacks {
state.GoStacks = getStacks()
}
return state
}
// registerInternal registers the following internal handlers which return runtime state:
// _gometa_introspect: TChannel internal state.
// _gometa_runtime: Golang runtime stats.
func (ch *Channel) registerInternal() {
endpoints := []struct {
name string
handler func([]byte) interface{}
}{
{"_gometa_introspect", ch.handleIntrospection},
{"_gometa_runtime", handleInternalRuntime},
}
for _, ep := range endpoints {
// We need ep in our closure.
ep := ep
handler := func(ctx context.Context, call *InboundCall) {
var arg2, arg3 []byte
if err := NewArgReader(call.Arg2Reader()).Read(&arg2); err != nil {
return
}
if err := NewArgReader(call.Arg3Reader()).Read(&arg3); err != nil {
return
}
if err := NewArgWriter(call.Response().Arg2Writer()).Write(nil); err != nil {
return
}
NewArgWriter(call.Response().Arg3Writer()).WriteJSON(ep.handler(arg3))
}
ch.Register(HandlerFunc(handler), ep.name)
}
}