forked from Juniper/jtimon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statshandler.go
244 lines (210 loc) · 7.33 KB
/
statshandler.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
/*
* Copyright (c) 2018, Juniper Networks, Inc.
* All rights reserved.
*/
package main
import (
"fmt"
"sync"
"time"
na_pb "github.com/Juniper/jtimon/telemetry"
"golang.org/x/net/context"
"google.golang.org/grpc/stats"
)
type statsCtx struct {
sync.Mutex // guarding following stats
startTime time.Time
totalIn uint64
totalKV uint64
totalInPayloadLength uint64
totalInPayloadWireLength uint64
totalInHeaderWireLength uint64
totalLatency uint64
totalLatencyPkt uint64
totalDdrops uint64
}
type statshandler struct {
jctx *JCtx
}
func (h *statshandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context {
return ctx
}
func (h *statshandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
return ctx
}
func (h *statshandler) HandleConn(ctx context.Context, s stats.ConnStats) {
switch s.(type) {
case *stats.ConnBegin:
case *stats.ConnEnd:
default:
}
}
func (h *statshandler) HandleRPC(ctx context.Context, s stats.RPCStats) {
h.jctx.stats.Lock()
defer h.jctx.stats.Unlock()
switch s.(type) {
case *stats.InHeader:
h.jctx.stats.totalInHeaderWireLength += uint64(s.(*stats.InHeader).WireLength)
case *stats.OutHeader:
case *stats.OutPayload:
case *stats.InPayload:
h.jctx.stats.totalInPayloadLength += uint64(s.(*stats.InPayload).Length)
h.jctx.stats.totalInPayloadWireLength += uint64(s.(*stats.InPayload).WireLength)
if h.jctx.config.Log.CSVStats {
switch v := (s.(*stats.InPayload).Payload).(type) {
case *na_pb.OpenConfigData:
updateStats(h.jctx, v, false)
for idx, kv := range v.Kv {
updateStatsKV(h.jctx, false)
switch kvvalue := kv.Value.(type) {
case *na_pb.KeyValue_UintValue:
if kv.Key == "__timestamp__" {
var reCTS uint64
var rePGetTS uint64
if len(v.Kv) > idx+2 {
nextKV := v.Kv[idx+1]
if nextKV.Key == "__junos_re_stream_creation_timestamp__" {
reCTS = nextKV.GetUintValue()
}
nextnextKV := v.Kv[idx+2]
if nextnextKV.Key == "__junos_re_payload_get_timestamp__" {
rePGetTS = nextnextKV.GetUintValue()
}
}
jLog(h.jctx, fmt.Sprintf("%s,%d,%d,%d,%d,%d,%d,%d,%d\n",
v.Path, v.SequenceNumber, v.ComponentId, v.SubComponentId, s.(*stats.InPayload).Length, v.Timestamp, kvvalue.UintValue, reCTS, rePGetTS))
}
}
}
}
}
case *stats.InTrailer:
case *stats.End:
default:
}
}
func updateStats(jctx *JCtx, ocData *na_pb.OpenConfigData, needLock bool) {
if !*stateHandler {
return
}
if needLock {
jctx.stats.Lock()
defer jctx.stats.Unlock()
}
jctx.stats.totalIn++
if jctx.config.Log.LatencyCheck {
now := time.Now()
nanos := now.UnixNano()
millis := nanos / 1000000
if millis > int64(ocData.Timestamp) {
jctx.stats.totalLatency += uint64((millis - int64(ocData.Timestamp)))
jctx.stats.totalLatencyPkt++
}
}
}
func updateStatsKV(jctx *JCtx, needLock bool) {
if !*stateHandler {
return
}
if needLock {
jctx.stats.Lock()
defer jctx.stats.Unlock()
}
jctx.stats.totalKV++
}
func periodicStats(jctx *JCtx) {
if !*stateHandler {
return
}
pstats := jctx.config.Log.PeriodicStats
if pstats == 0 {
return
}
headerCounter := 0
for {
tickChan := time.NewTicker(time.Second * time.Duration(pstats)).C
<-tickChan
// Do nothing if we haven't heard back anything from the device
jctx.stats.Lock()
if jctx.stats.totalIn == 0 {
jctx.stats.Unlock()
continue
}
s := fmt.Sprintf("\n")
// print header
if headerCounter%100 == 0 {
if jctx.config.Log.LatencyCheck {
s += "+------------------------------+--------------------+--------------------+--------------------+--------------------+-----------------+\n"
s += "| Timestamp | KV | Packets | Bytes | Bytes(wire) | Average Latency |\n"
s += "+------------------------------+--------------------+--------------------+--------------------+--------------------+-----------------+\n"
} else {
s += "+------------------------------+--------------------+--------------------+--------------------+--------------------+\n"
s += "| Timestamp | KV | Packets | Bytes | Bytes(wire) |\n"
s += "+------------------------------+--------------------+--------------------+--------------------+--------------------+\n"
}
}
if jctx.config.Log.LatencyCheck && jctx.stats.totalLatencyPkt != 0 {
s += fmt.Sprintf("| %s | %18v | %18v | %18v | %18v | %15v |\n", time.Now().Format(time.UnixDate),
jctx.stats.totalKV,
jctx.stats.totalIn,
jctx.stats.totalInPayloadLength,
jctx.stats.totalInPayloadWireLength,
jctx.stats.totalLatency/jctx.stats.totalLatencyPkt)
} else {
s += fmt.Sprintf("| %s | %18v | %18v | %18v | %18v |\n", time.Now().Format(time.UnixDate),
jctx.stats.totalKV,
jctx.stats.totalIn,
jctx.stats.totalInPayloadLength,
jctx.stats.totalInPayloadWireLength)
}
jctx.stats.Unlock()
headerCounter++
if s != "" {
jLog(jctx, fmt.Sprintf("%s\n", s))
}
}
}
func printSummary(jctx *JCtx) {
if !*stateHandler {
return
}
if jctx.config.Log.CSVStats && jctx.config.Log.DropCheck {
dropCheckCSV(jctx)
}
if jctx.config.Log.DropCheck {
printDropDS(jctx)
}
endTime := time.Since(jctx.stats.startTime)
stmap := make(map[string]interface{})
s := fmt.Sprintf("\nCollector Stats for %s:%d (Run time : %s)\n", jctx.config.Host, jctx.config.Port, endTime)
stmap["run-time"] = float64(endTime)
s += fmt.Sprintf("%-12v : in-packets\n", jctx.stats.totalIn)
stmap["in-packets"] = float64(jctx.stats.totalIn)
s += fmt.Sprintf("%-12v : data points (KV pairs)\n", jctx.stats.totalKV)
stmap["kv"] = float64(jctx.stats.totalKV)
s += fmt.Sprintf("%-12v : in-header wirelength (bytes)\n", jctx.stats.totalInHeaderWireLength)
stmap["in-header-wire-length"] = float64(jctx.stats.totalInHeaderWireLength)
s += fmt.Sprintf("%-12v : in-payload length (bytes)\n", jctx.stats.totalInPayloadLength)
stmap["in-payload-length-bytes"] = float64(jctx.stats.totalInPayloadLength)
s += fmt.Sprintf("%-12v : in-payload wirelength (bytes)\n", jctx.stats.totalInPayloadWireLength)
stmap["in-payload-wirelength-bytes"] = float64(jctx.stats.totalInPayloadWireLength)
if uint64(endTime.Seconds()) != 0 {
s += fmt.Sprintf("%-12v : throughput (bytes per seconds)\n", jctx.stats.totalInPayloadLength/uint64(endTime.Seconds()))
stmap["throughput"] = float64(jctx.stats.totalInPayloadLength / uint64(endTime.Seconds()))
}
if jctx.config.Log.LatencyCheck && jctx.stats.totalLatencyPkt != 0 {
s += fmt.Sprintf("%-12v : latency sample packets\n", jctx.stats.totalLatencyPkt)
stmap["latency-sample-packets"] = float64(jctx.stats.totalLatencyPkt)
s += fmt.Sprintf("%-12v : latency (ms)\n", jctx.stats.totalLatency)
stmap["total-latency"] = float64(jctx.stats.totalLatency)
s += fmt.Sprintf("%-12v : average latency (ms)\n", jctx.stats.totalLatency/jctx.stats.totalLatencyPkt)
stmap["average-latency"] = float64(jctx.stats.totalLatency / jctx.stats.totalLatencyPkt)
}
if jctx.config.Log.DropCheck {
s += fmt.Sprintf("%-12v : total packet drops\n", jctx.stats.totalDdrops)
stmap["total-drops"] = float64(jctx.stats.totalDdrops)
}
s += fmt.Sprintf("\n")
jLog(jctx, fmt.Sprintf("\n%s\n", s))
addIDBSummary(jctx, stmap)
}