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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use std::collections::HashMap;

#[derive(Deserialize,Debug)]
pub struct Statistics {
    pub name: String,
    #[serde(rename = "type")]
    pub client_type: String,
    pub ts: i64,
    pub time: i64,
    pub replyq: i64,
    pub msg_cnt: i64,
    pub msg_size: i64,
    pub msg_max: i64,
    pub msg_size_max: i64,
    pub simple_cnt: i64,
    pub brokers: HashMap<String, Broker>,
    pub topics: HashMap<String, Topic>,
    pub cgrp: Option<ConsumerGroup>
}

#[derive(Deserialize,Debug)]
pub struct Broker {
    pub name: String,
    pub nodeid: i32,
    pub state: String,
    pub stateage: i64,
    pub outbuf_cnt: i64,
    pub outbuf_msg_cnt: i64,
    pub waitresp_cnt: i64,
    pub waitresp_msg_cnt: i64,
    pub tx: i64,
    pub txbytes: i64,
    pub txerrs: i64,
    pub txretries: i64,
    pub req_timeouts: i64,
    pub rx: i64,
    pub rxbytes: i64,
    pub rxerrs: i64,
    pub rxcorriderrs: i64,
    pub rxpartial: i64,
    pub zbuf_grow: i64,
    pub buf_grow: i64,
    pub wakeups: Option<i64>,
    pub int_latency: Option<Window>,
    pub rtt: Option<Window>,
    pub throttle: Option<Window>,
    pub toppars: HashMap<String, TopicPartition>
}

#[derive(Deserialize,Debug)]
pub struct Window {
    pub min: i64,
    pub max: i64,
    pub avg: i64,
    pub sum: i64,
    pub cnt: i64
}

#[derive(Deserialize,Debug)]
pub struct TopicPartition {
    pub topic: String,
    pub partition: i32
}

#[derive(Deserialize,Debug)]
pub struct Topic {
    pub topic: String,
    pub metadata_age: i64,
    pub partitions: HashMap<i32, Partition>
}

#[derive(Deserialize,Debug)]
pub struct Partition {
    pub partition: i32,
    pub leader: i32,
    pub desired: bool,
    pub unknown: bool,
    pub msgq_cnt: i64,
    pub msgq_bytes: i64,
    pub xmit_msgq_cnt: i64,
    pub xmit_msgq_bytes: i64,
    pub fetchq_cnt: i64,
    pub fetchq_size: i64,
    pub fetch_state: String,
    pub query_offset: i64,
    pub next_offset: i64,
    pub app_offset: i64,
    pub stored_offset: i64,
    pub committed_offset: i64,
    pub eof_offset: i64,
    pub lo_offset: i64,
    pub hi_offset: i64,
    pub consumer_lag: i64,
    pub txmsgs: i64,
    pub txbytes: i64,
    pub msgs: i64,
    pub rx_ver_drops: i64
}

#[derive(Deserialize,Debug)]
pub struct ConsumerGroup {
    pub rebalance_age: i64,
    pub rebalance_cnt: i64,
    pub assignment_size: i32
}

#[cfg(test)]
mod tests {
    use serde_json;
    use super::*;

    #[test]
    fn test_statistics() {
        let stats: Statistics = serde_json::from_str(EXAMPLE).unwrap();

        assert_eq!(stats.name, "rdkafka#consumer-1");
        assert_eq!(stats.client_type, "consumer");
        assert_eq!(stats.ts, 895747604205);
        assert_eq!(stats.time, 1479659343);
        assert_eq!(stats.replyq, 0);
        assert_eq!(stats.msg_cnt, 0);
        assert_eq!(stats.msg_size, 0);
        assert_eq!(stats.msg_max, 0);
        assert_eq!(stats.msg_size_max, 0);
        assert_eq!(stats.simple_cnt, 0);

        assert_eq!(stats.brokers.len(), 4);
        assert_eq!(stats.topics.len(), 1);
    }

    // Example from https://github.com/edenhill/librdkafka/wiki/Statistics
    const EXAMPLE: &'static str = r#"
        {
          "name": "rdkafka#consumer-1",
          "type": "consumer",
          "ts": 895747604205,
          "time": 1479659343,
          "replyq": 0,
          "msg_cnt": 0,
          "msg_size": 0,
          "msg_max": 0,
          "msg_size_max": 0,
          "simple_cnt": 0,
          "brokers": {
            "0:9092/bootstrap": {
              "name": "0:9092/bootstrap",
              "nodeid": -1,
              "state": "UP",
              "stateage": 5989882,
              "outbuf_cnt": 0,
              "outbuf_msg_cnt": 0,
              "waitresp_cnt": 0,
              "waitresp_msg_cnt": 0,
              "tx": 2,
              "txbytes": 56,
              "txerrs": 0,
              "txretries": 0,
              "req_timeouts": 0,
              "rx": 2,
              "rxbytes": 31692,
              "rxerrs": 0,
              "rxcorriderrs": 0,
              "rxpartial": 0,
              "zbuf_grow": 0,
              "buf_grow": 0,
              "wakeups": 0,
              "int_latency": {
                "min": 0,
                "max": 0,
                "avg": 0,
                "sum": 0,
                "cnt": 0
              },
              "rtt": {
                "min": 0,
                "max": 0,
                "avg": 0,
                "sum": 0,
                "cnt": 0
              },
              "throttle": {
                "min": 0,
                "max": 0,
                "avg": 0,
                "sum": 0,
                "cnt": 0
              },
              "toppars": {}
            },
            "localhost:9092/2": {
              "name": "localhost:9092/2",
              "nodeid": 2,
              "state": "UP",
              "stateage": 5958663,
              "outbuf_cnt": 0,
              "outbuf_msg_cnt": 0,
              "waitresp_cnt": 1,
              "waitresp_msg_cnt": 0,
              "tx": 54,
              "txbytes": 3650,
              "txerrs": 0,
              "txretries": 0,
              "req_timeouts": 0,
              "rx": 53,
              "rxbytes": 89546,
              "rxerrs": 0,
              "rxcorriderrs": 0,
              "rxpartial": 0,
              "zbuf_grow": 0,
              "buf_grow": 0,
              "wakeups": 0,
              "int_latency": {
                "min": 0,
                "max": 0,
                "avg": 0,
                "sum": 0,
                "cnt": 0
              },
              "rtt": {
                "min": 721,
                "max": 106064,
                "avg": 87530,
                "sum": 1925664,
                "cnt": 22
              },
              "throttle": {
                "min": 0,
                "max": 0,
                "avg": 0,
                "sum": 0,
                "cnt": 19
              },
              "toppars": {
                "test-1": {
                  "topic": "test",
                  "partition": 1
                }
              }
            },
            "localhost:9094/4": {
              "name": "localhost:9094/4",
              "nodeid": 4,
              "state": "UP",
              "stateage": 5958663,
              "outbuf_cnt": 0,
              "outbuf_msg_cnt": 0,
              "waitresp_cnt": 1,
              "waitresp_msg_cnt": 0,
              "tx": 40,
              "txbytes": 3042,
              "txerrs": 0,
              "txretries": 0,
              "req_timeouts": 0,
              "rx": 39,
              "rxbytes": 87058,
              "rxerrs": 0,
              "rxcorriderrs": 0,
              "rxpartial": 0,
              "zbuf_grow": 0,
              "buf_grow": 0,
              "wakeups": 0,
              "int_latency": {
                "min": 0,
                "max": 0,
                "avg": 0,
                "sum": 0,
                "cnt": 0
              },
              "rtt": {
                "min": 100169,
                "max": 101198,
                "avg": 100730,
                "sum": 2014600,
                "cnt": 20
              },
              "throttle": {
                "min": 0,
                "max": 0,
                "avg": 0,
                "sum": 0,
                "cnt": 20
              },
              "toppars": {
                "test-3": {
                  "topic": "test",
                  "partition": 3
                },
                "test-0": {
                  "topic": "test",
                  "partition": 0
                }
              }
            },
            "localhost:9093/3": {
              "name": "localhost:9093/3",
              "nodeid": 3,
              "state": "UP",
              "stateage": 5958647,
              "outbuf_cnt": 0,
              "outbuf_msg_cnt": 0,
              "waitresp_cnt": 1,
              "waitresp_msg_cnt": 0,
              "tx": 44,
              "txbytes": 2688,
              "txerrs": 0,
              "txretries": 0,
              "req_timeouts": 0,
              "rx": 43,
              "rxbytes": 90161,
              "rxerrs": 0,
              "rxcorriderrs": 0,
              "rxpartial": 0,
              "zbuf_grow": 0,
              "buf_grow": 0,
              "wakeups": 0,
              "int_latency": {
                "min": 0,
                "max": 0,
                "avg": 0,
                "sum": 0,
                "cnt": 0
              },
              "rtt": {
                "min": 99647,
                "max": 101254,
                "avg": 100612,
                "sum": 2012247,
                "cnt": 20
              },
              "throttle": {
                "min": 0,
                "max": 0,
                "avg": 0,
                "sum": 0,
                "cnt": 20
              },
              "toppars": {
                "test-2": {
                  "topic": "test",
                  "partition": 2
                }
              }
            }
          },
          "topics": {
            "test": {
              "topic": "test",
              "metadata_age": 4957,
              "partitions": {
                "0": {
                  "partition": 0,
                  "leader": 4,
                  "desired": true,
                  "unknown": false,
                  "msgq_cnt": 0,
                  "msgq_bytes": 0,
                  "xmit_msgq_cnt": 0,
                  "xmit_msgq_bytes": 0,
                  "fetchq_cnt": 0,
                  "fetchq_size": 0,
                  "fetch_state": "active",
                  "query_offset": -2,
                  "next_offset": 427,
                  "app_offset": 427,
                  "stored_offset": 427,
                  "commited_offset": 427,
                  "committed_offset": 427,
                  "eof_offset": 427,
                  "lo_offset": -1001,
                  "hi_offset": 427,
                  "consumer_lag": 0,
                  "txmsgs": 0,
                  "txbytes": 0,
                  "msgs": 0,
                  "rx_ver_drops": 0
                },
                "1": {
                  "partition": 1,
                  "leader": 2,
                  "desired": true,
                  "unknown": false,
                  "msgq_cnt": 0,
                  "msgq_bytes": 0,
                  "xmit_msgq_cnt": 0,
                  "xmit_msgq_bytes": 0,
                  "fetchq_cnt": 0,
                  "fetchq_size": 0,
                  "fetch_state": "active",
                  "query_offset": -2,
                  "next_offset": 436,
                  "app_offset": 436,
                  "stored_offset": 436,
                  "commited_offset": 436,
                  "committed_offset": 436,
                  "eof_offset": 436,
                  "lo_offset": -1001,
                  "hi_offset": 436,
                  "consumer_lag": 0,
                  "txmsgs": 0,
                  "txbytes": 0,
                  "msgs": 0,
                  "rx_ver_drops": 0
                },
                "2": {
                  "partition": 2,
                  "leader": 3,
                  "desired": true,
                  "unknown": false,
                  "msgq_cnt": 0,
                  "msgq_bytes": 0,
                  "xmit_msgq_cnt": 0,
                  "xmit_msgq_bytes": 0,
                  "fetchq_cnt": 0,
                  "fetchq_size": 0,
                  "fetch_state": "active",
                  "query_offset": -2,
                  "next_offset": 458,
                  "app_offset": 458,
                  "stored_offset": 458,
                  "commited_offset": 458,
                  "committed_offset": 458,
                  "eof_offset": 458,
                  "lo_offset": -1001,
                  "hi_offset": 458,
                  "consumer_lag": 0,
                  "txmsgs": 0,
                  "txbytes": 0,
                  "msgs": 0,
                  "rx_ver_drops": 0
                },
                "3": {
                  "partition": 3,
                  "leader": 4,
                  "desired": true,
                  "unknown": false,
                  "msgq_cnt": 0,
                  "msgq_bytes": 0,
                  "xmit_msgq_cnt": 0,
                  "xmit_msgq_bytes": 0,
                  "fetchq_cnt": 0,
                  "fetchq_size": 0,
                  "fetch_state": "active",
                  "query_offset": -2,
                  "next_offset": 497,
                  "app_offset": 497,
                  "stored_offset": 497,
                  "commited_offset": 497,
                  "committed_offset": 497,
                  "eof_offset": 497,
                  "lo_offset": -1001,
                  "hi_offset": 497,
                  "consumer_lag": 0,
                  "txmsgs": 0,
                  "txbytes": 0,
                  "msgs": 0,
                  "rx_ver_drops": 0
                },
                "-1": {
                  "partition": -1,
                  "leader": -1,
                  "desired": false,
                  "unknown": false,
                  "msgq_cnt": 0,
                  "msgq_bytes": 0,
                  "xmit_msgq_cnt": 0,
                  "xmit_msgq_bytes": 0,
                  "fetchq_cnt": 0,
                  "fetchq_size": 0,
                  "fetch_state": "none",
                  "query_offset": 0,
                  "next_offset": 0,
                  "app_offset": -1001,
                  "stored_offset": -1001,
                  "commited_offset": -1001,
                  "committed_offset": -1001,
                  "eof_offset": -1001,
                  "lo_offset": -1001,
                  "hi_offset": -1001,
                  "consumer_lag": -1,
                  "txmsgs": 0,
                  "txbytes": 0,
                  "msgs": 0,
                  "rx_ver_drops": 0
                }
              }
            }
          },
          "cgrp": {
            "rebalance_age": 5251,
            "rebalance_cnt": 2,
            "assignment_size": 4
          }
        }
        "#;
}