@@ -218,6 +218,16 @@ var (
218
218
"Zone serial number." ,
219
219
[]string {"view" , "zone_name" }, nil ,
220
220
)
221
+ trafficReceived = prometheus .NewDesc (
222
+ prometheus .BuildFQName (namespace , "traffic" , "received_size" ),
223
+ "Received traffic packet sizes." ,
224
+ []string {"type" }, nil ,
225
+ )
226
+ trafficSent = prometheus .NewDesc (
227
+ prometheus .BuildFQName (namespace , "traffic" , "sent_size" ),
228
+ "Received traffic packet sizes." ,
229
+ []string {"transport" }, nil ,
230
+ )
221
231
)
222
232
223
233
type collectorConstructor func (log.Logger , * bind.Statistics ) prometheus.Collector
@@ -387,6 +397,87 @@ func (c *taskCollector) Collect(ch chan<- prometheus.Metric) {
387
397
)
388
398
}
389
399
400
+ type trafficCollector struct {
401
+ logger log.Logger
402
+ stats * bind.Statistics
403
+ }
404
+
405
+ // newTrafficCollector implements collectorConstructor.
406
+ func newTrafficCollector (logger log.Logger , s * bind.Statistics ) prometheus.Collector {
407
+ return & trafficCollector {logger : logger , stats : s }
408
+ }
409
+
410
+ // Describe implements prometheus.Collector.
411
+ func (c * trafficCollector ) Describe (ch chan <- * prometheus.Desc ) {
412
+ ch <- trafficReceived
413
+ ch <- trafficSent
414
+ }
415
+
416
+ // Collect implements prometheus.Collector.
417
+ func (c * trafficCollector ) Collect (ch chan <- prometheus.Metric ) {
418
+ // IPv4 traffic histograms.
419
+ buckets , count := c .makeHistogram (c .stats .TrafficHistograms .ReceivedUDPv4 )
420
+ ch <- prometheus .MustNewConstHistogram (
421
+ trafficReceived , count , math .NaN (), buckets , "udpv4" ,
422
+ )
423
+ buckets , count = c .makeHistogram (c .stats .TrafficHistograms .SentUDPv4 )
424
+ ch <- prometheus .MustNewConstHistogram (
425
+ trafficSent , count , math .NaN (), buckets , "udpv4" ,
426
+ )
427
+ buckets , count = c .makeHistogram (c .stats .TrafficHistograms .ReceivedTCPv4 )
428
+ ch <- prometheus .MustNewConstHistogram (
429
+ trafficReceived , count , math .NaN (), buckets , "tcpv4" ,
430
+ )
431
+ buckets , count = c .makeHistogram (c .stats .TrafficHistograms .SentTCPv4 )
432
+ ch <- prometheus .MustNewConstHistogram (
433
+ trafficSent , count , math .NaN (), buckets , "tcpv4" ,
434
+ )
435
+
436
+ // IPv6 traffic histograms.
437
+ buckets , count = c .makeHistogram (c .stats .TrafficHistograms .ReceivedUDPv6 )
438
+ ch <- prometheus .MustNewConstHistogram (
439
+ trafficReceived , count , math .NaN (), buckets , "udpv6" ,
440
+ )
441
+ buckets , count = c .makeHistogram (c .stats .TrafficHistograms .SentUDPv6 )
442
+ ch <- prometheus .MustNewConstHistogram (
443
+ trafficSent , count , math .NaN (), buckets , "udpv6" ,
444
+ )
445
+ buckets , count = c .makeHistogram (c .stats .TrafficHistograms .ReceivedTCPv6 )
446
+ ch <- prometheus .MustNewConstHistogram (
447
+ trafficReceived , count , math .NaN (), buckets , "tcpv6" ,
448
+ )
449
+ buckets , count = c .makeHistogram (c .stats .TrafficHistograms .SentTCPv6 )
450
+ ch <- prometheus .MustNewConstHistogram (
451
+ trafficSent , count , math .NaN (), buckets , "tcpv6" ,
452
+ )
453
+ }
454
+
455
+ // makeHistogram translates the non-aggregated bucket slice into an aggregated map, suitable for
456
+ // use by prometheus.MustNewConstHistogram().
457
+ func (c * trafficCollector ) makeHistogram (rawBuckets []uint64 ) (map [float64 ]uint64 , uint64 ) {
458
+ var (
459
+ buckets = map [float64 ]uint64 {}
460
+ count uint64
461
+ )
462
+
463
+ for i , v := range rawBuckets {
464
+ if v > 0 {
465
+ var idx float64
466
+
467
+ if i == len (rawBuckets )- 1 {
468
+ idx = math .Inf (1 )
469
+ } else {
470
+ idx = float64 ((i + 2 )* bind .TrafficBucketSize ) - 1
471
+ }
472
+
473
+ count += v
474
+ buckets [idx ] = count
475
+ }
476
+ }
477
+
478
+ return buckets , count
479
+ }
480
+
390
481
// Exporter collects Binds stats from the given server and exports them using
391
482
// the prometheus metrics package.
392
483
type Exporter struct {
@@ -411,10 +502,12 @@ func NewExporter(logger log.Logger, version, url string, timeout time.Duration,
411
502
switch g {
412
503
case bind .ServerStats :
413
504
cs = append (cs , newServerCollector )
414
- case bind .ViewStats :
415
- cs = append (cs , newViewCollector )
416
505
case bind .TaskStats :
417
506
cs = append (cs , newTaskCollector )
507
+ case bind .TrafficStats :
508
+ cs = append (cs , newTrafficCollector )
509
+ case bind .ViewStats :
510
+ cs = append (cs , newViewCollector )
418
511
}
419
512
}
420
513
@@ -503,10 +596,12 @@ func (s *statisticGroups) Set(value string) error {
503
596
switch dt {
504
597
case string (bind .ServerStats ):
505
598
sg = bind .ServerStats
506
- case string (bind .ViewStats ):
507
- sg = bind .ViewStats
508
599
case string (bind .TaskStats ):
509
600
sg = bind .TaskStats
601
+ case string (bind .TrafficStats ):
602
+ sg = bind .TrafficStats
603
+ case string (bind .ViewStats ):
604
+ sg = bind .ViewStats
510
605
default :
511
606
return fmt .Errorf ("unknown stats group %q" , dt )
512
607
}
@@ -544,7 +639,8 @@ func main() {
544
639
toolkitFlags := webflag .AddFlags (kingpin .CommandLine , ":9119" )
545
640
546
641
kingpin .Flag ("bind.stats-groups" ,
547
- "Comma-separated list of statistics to collect" ,
642
+ "Comma-separated list of statistics to collect. " +
643
+ "One or more of: [server, tasks, traffic, view]" ,
548
644
).Default ((& statisticGroups {
549
645
bind .ServerStats , bind .ViewStats ,
550
646
}).String ()).SetValue (& groups )
0 commit comments