-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
232 lines (205 loc) · 6.33 KB
/
types.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
package lookupd
import (
"fmt"
"sort"
"time"
"github.com/bitly/nsq/util"
"github.com/bitly/nsq/util/semver"
)
type ProducerTopic struct {
Topic string `json:"topic"`
Tombstoned bool `json:"tombstoned"`
}
type ProducerTopics []ProducerTopic
func (pt ProducerTopics) Len() int { return len(pt) }
func (pt ProducerTopics) Swap(i, j int) { pt[i], pt[j] = pt[j], pt[i] }
func (pt ProducerTopics) Less(i, j int) bool { return pt[i].Topic < pt[j].Topic }
type Producer struct {
RemoteAddresses []string `json:"remote_addresses"`
Hostname string `json:"hostname"`
BroadcastAddress string `json:"broadcast_address"`
TcpPort int `json:"tcp_port"`
HttpPort int `json:"http_port"`
Version string `json:"version"`
VersionObj *semver.Version `json:"-"`
Topics ProducerTopics `json:"topics"`
OutOfDate bool
}
func (p *Producer) HTTPAddress() string {
return fmt.Sprintf("%s:%d", p.BroadcastAddress, p.HttpPort)
}
func (p *Producer) TCPAddress() string {
return fmt.Sprintf("%s:%d", p.BroadcastAddress, p.TcpPort)
}
// IsInconsistent checks for cases where an unexpected number of nsqd connections are
// reporting the same information to nsqlookupd (ie: multiple instances are using the
// same broadcast address), or cases where some nsqd are not reporting to all nsqlookupd.
func (p *Producer) IsInconsistent(numLookupd int) bool {
return len(p.RemoteAddresses) != numLookupd
}
type TopicStats struct {
HostAddress string
TopicName string
Depth int64
MemoryDepth int64
BackendDepth int64
MessageCount int64
ChannelCount int
Aggregate bool
Channels []*ChannelStats
Paused bool
E2eProcessingLatency *util.E2eProcessingLatencyAggregate
numAggregates int
}
func (t *TopicStats) Add(a *TopicStats) {
t.Aggregate = true
t.TopicName = a.TopicName
t.Depth += a.Depth
t.MemoryDepth += a.MemoryDepth
t.BackendDepth += a.BackendDepth
t.MessageCount += a.MessageCount
if a.ChannelCount > t.ChannelCount {
t.ChannelCount = a.ChannelCount
}
if a.Paused {
t.Paused = a.Paused
}
t.numAggregates += 1
t.E2eProcessingLatency = t.E2eProcessingLatency.Add(a.E2eProcessingLatency, t.numAggregates)
}
func (t *TopicStats) Target(key string) ([]string, string) {
color := "blue"
if key == "depth" || key == "deferred_count" {
color = "red"
}
target := fmt.Sprintf("sumSeries(%%stopic.%s.%s)", t.TopicName, key)
return []string{target}, color
}
func (t *TopicStats) Host() string {
h := t.HostAddress
if t.Aggregate {
h = "*"
}
return h
}
type ChannelStats struct {
HostAddress string
TopicName string
ChannelName string
Depth int64
MemoryDepth int64
BackendDepth int64
InFlightCount int64
DeferredCount int64
RequeueCount int64
TimeoutCount int64
MessageCount int64
ClientCount int
Selected bool
HostStats []*ChannelStats
Clients []*ClientStats
Paused bool
E2eProcessingLatency *util.E2eProcessingLatencyAggregate
}
func (c *ChannelStats) Add(a *ChannelStats) {
c.Depth += a.Depth
c.MemoryDepth += a.MemoryDepth
c.BackendDepth += a.BackendDepth
c.InFlightCount += a.InFlightCount
c.DeferredCount += a.DeferredCount
c.RequeueCount += a.RequeueCount
c.TimeoutCount += a.TimeoutCount
c.MessageCount += a.MessageCount
c.ClientCount += a.ClientCount
if a.Paused {
c.Paused = a.Paused
}
c.HostStats = append(c.HostStats, a)
c.E2eProcessingLatency = c.E2eProcessingLatency.Add(a.E2eProcessingLatency, len(c.HostStats))
sort.Sort(ChannelStatsByHost{c.HostStats})
}
func (c *ChannelStats) Target(key string) ([]string, string) {
color := "blue"
if key == "depth" || key == "deferred_count" {
color = "red"
}
target := fmt.Sprintf("sumSeries(%%stopic.%s.channel.%s.%s)", c.TopicName, c.ChannelName, key)
return []string{target}, color
}
func (c *ChannelStats) Host() string {
h := "*"
if len(c.HostStats) == 0 {
h = c.HostAddress
}
return h
}
type ClientStats struct {
HostAddress string
RemoteAddress string
Version string
ClientID string
Hostname string
UserAgent string
ConnectedDuration time.Duration
InFlightCount int
ReadyCount int
FinishCount int64
RequeueCount int64
MessageCount int64
SampleRate int32
Deflate bool
Snappy bool
Authed bool
AuthIdentity string
AuthIdentityUrl string
TLS bool
CipherSuite string `json:"tls_cipher_suite"`
TLSVersion string `json:"tls_version"`
TLSNegotiatedProtocol string `json:"tls_negotiated_protocol"`
TLSNegotiatedProtocolIsMutual bool `json:"tls_negotiated_protocol_is_mutual"`
}
func (c *ClientStats) HasUserAgent() bool {
return c.UserAgent != ""
}
func (c *ClientStats) HasSampleRate() bool {
return c.SampleRate > 0
}
type ChannelStatsList []*ChannelStats
type ChannelStatsByHost struct {
ChannelStatsList
}
type ClientStatsList []*ClientStats
type ClientsByHost struct {
ClientStatsList
}
type TopicStatsList []*TopicStats
type TopicStatsByHost struct {
TopicStatsList
}
type ProducerList []*Producer
type ProducersByHost struct {
ProducerList
}
func (c ChannelStatsList) Len() int { return len(c) }
func (c ChannelStatsList) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c ClientStatsList) Len() int { return len(c) }
func (c ClientStatsList) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (t TopicStatsList) Len() int { return len(t) }
func (t TopicStatsList) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t ProducerList) Len() int { return len(t) }
func (t ProducerList) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (c ChannelStatsByHost) Less(i, j int) bool {
return c.ChannelStatsList[i].HostAddress < c.ChannelStatsList[j].HostAddress
}
func (c ClientsByHost) Less(i, j int) bool {
if c.ClientStatsList[i].ClientID == c.ClientStatsList[j].ClientID {
return c.ClientStatsList[i].HostAddress < c.ClientStatsList[j].HostAddress
}
return c.ClientStatsList[i].ClientID < c.ClientStatsList[j].ClientID
}
func (c TopicStatsByHost) Less(i, j int) bool {
return c.TopicStatsList[i].HostAddress < c.TopicStatsList[j].HostAddress
}
func (c ProducersByHost) Less(i, j int) bool {
return c.ProducerList[i].BroadcastAddress < c.ProducerList[j].BroadcastAddress
}