forked from nsqio/nsq
/
stats.go
217 lines (199 loc) · 5.86 KB
/
stats.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
package main
import (
"../util"
"fmt"
"io"
"log"
"net"
"net/http"
"sort"
"time"
)
type ClientStats struct {
version string
address string
name string
state int32
inFlightCount int64
readyCount int64
messageCount uint64
finishCount uint64
requeueCount uint64
connectTime time.Time
}
type Topics []*Topic
type TopicsByName struct {
Topics
}
type Channels []*Channel
type ChannelsByName struct {
Channels
}
func (t Topics) Len() int { return len(t) }
func (t Topics) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (c Channels) Len() int { return len(c) }
func (c Channels) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (t TopicsByName) Less(i, j int) bool { return t.Topics[i].name < t.Topics[j].name }
func (c ChannelsByName) Less(i, j int) bool { return c.Channels[i].name < c.Channels[j].name }
// print out stats for each topic/channel
func statsHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
log.Printf("ERROR: failed to parse request params - %s", err.Error())
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
formatString, _ := reqParams.Query("format")
jsonFormat := formatString == "json"
now := time.Now()
nsqd.RLock()
defer nsqd.RUnlock()
if !jsonFormat {
io.WriteString(w, fmt.Sprintf("nsqd v%s\n\n", util.BINARY_VERSION))
}
if len(nsqd.topicMap) == 0 {
if jsonFormat {
util.ApiResponse(w, 500, "NO_TOPICS", nil)
} else {
io.WriteString(w, "NO_TOPICS\n")
}
return
}
realTopics := make([]*Topic, len(nsqd.topicMap))
topics := make([]interface{}, len(nsqd.topicMap))
topic_index := 0
for _, t := range nsqd.topicMap {
realTopics[topic_index] = t
topic_index++
}
sort.Sort(TopicsByName{realTopics})
for topic_index, t := range realTopics {
t.RLock()
if !jsonFormat {
io.WriteString(w, fmt.Sprintf("\n[%-15s] depth: %-5d be-depth: %-5d msgs: %-8d\n",
t.name,
t.Depth(),
t.backend.Depth(),
t.messageCount))
}
realChannels := make([]*Channel, len(t.channelMap))
channel_index := 0
for _, c := range t.channelMap {
realChannels[channel_index] = c
channel_index++
}
sort.Sort(ChannelsByName{realChannels})
channels := make([]interface{}, len(t.channelMap))
for channel_index, c := range realChannels {
c.RLock()
if jsonFormat {
clients := make([]interface{}, len(c.clients))
for client_index, client := range c.clients {
clientStats := client.Stats()
clients[client_index] = struct {
Version string `json:"version"`
RemoteAddress string `json:"remote_address"`
Name string `json:"name"`
State int32 `json:"state"`
ReadyCount int64 `json:"ready_count"`
InFlightCount int64 `json:"in_flight_count"`
MessageCount uint64 `json:"message_count"`
FinishCount uint64 `json:"finish_count"`
RequeueCount uint64 `json:"requeue_count"`
ConnectTime int64 `json:"connect_ts"`
}{
clientStats.version,
clientStats.address,
clientStats.name,
clientStats.state,
clientStats.readyCount,
clientStats.inFlightCount,
clientStats.messageCount,
clientStats.finishCount,
clientStats.requeueCount,
clientStats.connectTime.Unix(),
}
}
channels[channel_index] = struct {
ChannelName string `json:"channel_name"`
Depth int64 `json:"depth"`
BackendDepth int64 `json:"backend_depth"`
InFlightCount int `json:"in_flight_count"`
DeferredCount int `json:"deferred_count"`
MessageCount uint64 `json:"message_count"`
RequeueCount uint64 `json:"requeue_count"`
TimeoutCount uint64 `json:"timeout_count"`
Clients []interface{} `json:"clients"`
Paused bool `json:"paused"`
}{
c.name,
c.Depth(),
c.backend.Depth(),
len(c.inFlightMessages),
len(c.deferredMessages),
c.messageCount,
c.requeueCount,
c.timeoutCount,
clients,
c.IsPaused(),
}
channel_index++
} else {
var pausedPrefix string
if c.IsPaused() {
pausedPrefix = " *P "
} else {
pausedPrefix = " "
}
io.WriteString(w,
fmt.Sprintf("%s[%-25s] depth: %-5d be-depth: %-5d inflt: %-4d def: %-4d re-q: %-5d timeout: %-5d msgs: %-8d\n",
pausedPrefix,
c.name,
c.Depth(),
c.backend.Depth(),
len(c.inFlightMessages),
len(c.deferredMessages),
c.requeueCount,
c.timeoutCount,
c.messageCount))
for _, client := range c.clients {
clientStats := client.Stats()
duration := now.Sub(clientStats.connectTime).Seconds()
_, port, _ := net.SplitHostPort(clientStats.address)
io.WriteString(w, fmt.Sprintf(" [%s %-21s] state: %d inflt: %-4d rdy: %-4d fin: %-8d re-q: %-8d msgs: %-8d connected: %s\n",
clientStats.version,
fmt.Sprintf("%s:%s", clientStats.name, port),
clientStats.state,
clientStats.inFlightCount,
clientStats.readyCount,
clientStats.finishCount,
clientStats.requeueCount,
clientStats.messageCount,
time.Duration(int64(duration))*time.Second, // truncate to the second
))
}
}
c.RUnlock()
}
topics[topic_index] = struct {
TopicName string `json:"topic_name"`
Channels []interface{} `json:"channels"`
Depth int64 `json:"depth"`
BackendDepth int64 `json:"backend_depth"`
MessageCount uint64 `json:"message_count"`
}{
TopicName: t.name,
Channels: channels,
Depth: t.Depth(),
BackendDepth: t.backend.Depth(),
MessageCount: t.messageCount,
}
topic_index++
t.RUnlock()
}
if jsonFormat {
util.ApiResponse(w, 200, "OK", struct {
Topics []interface{} `json:"topics"`
}{topics})
}
}