2017-05-23 02:55:50 -07:00
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2021-10-03 04:35:24 -07:00
//go:build !noqdisc
2017-05-23 02:55:50 -07:00
// +build !noqdisc
package collector
import (
"encoding/json"
2022-04-10 04:58:01 -07:00
"fmt"
2022-07-27 11:59:39 -07:00
"os"
2017-05-23 02:55:50 -07:00
"path/filepath"
"github.com/ema/qdisc"
2020-11-14 02:53:51 -08:00
"github.com/go-kit/log"
2017-05-23 02:55:50 -07:00
"github.com/prometheus/client_golang/prometheus"
2017-08-12 06:07:24 -07:00
"gopkg.in/alecthomas/kingpin.v2"
2017-05-23 02:55:50 -07:00
)
type qdiscStatCollector struct {
2022-04-10 04:58:01 -07:00
logger log . Logger
deviceFilter deviceFilter
bytes typedDesc
packets typedDesc
drops typedDesc
requeues typedDesc
overlimits typedDesc
qlength typedDesc
backlog typedDesc
2017-05-23 02:55:50 -07:00
}
var (
2022-04-10 04:58:01 -07:00
collectorQdisc = kingpin . Flag ( "collector.qdisc.fixtures" , "test fixtures to use for qdisc collector end-to-end testing" ) . Default ( "" ) . String ( )
collectorQdiskDeviceInclude = kingpin . Flag ( "collector.qdisk.device-include" , "Regexp of qdisk devices to include (mutually exclusive to device-exclude)." ) . String ( )
collectorQdiskDeviceExclude = kingpin . Flag ( "collector.qdisk.device-exclude" , "Regexp of qdisk devices to exclude (mutually exclusive to device-include)." ) . String ( )
2017-05-23 02:55:50 -07:00
)
func init ( ) {
2017-09-28 06:06:26 -07:00
registerCollector ( "qdisc" , defaultDisabled , NewQdiscStatCollector )
2017-05-23 02:55:50 -07:00
}
2018-04-29 05:34:47 -07:00
// NewQdiscStatCollector returns a new Collector exposing queuing discipline statistics.
2019-12-31 08:19:37 -08:00
func NewQdiscStatCollector ( logger log . Logger ) ( Collector , error ) {
2022-04-10 04:58:01 -07:00
if * collectorQdiskDeviceExclude != "" && * collectorQdiskDeviceInclude != "" {
return nil , fmt . Errorf ( "collector.qdisk.device-include and collector.qdisk.device-exclude are mutaly exclusive" )
}
2017-05-23 02:55:50 -07:00
return & qdiscStatCollector {
bytes : typedDesc { prometheus . NewDesc (
2017-09-28 06:06:26 -07:00
prometheus . BuildFQName ( namespace , "qdisc" , "bytes_total" ) ,
2017-05-23 02:55:50 -07:00
"Number of bytes sent." ,
[ ] string { "device" , "kind" } , nil ,
) , prometheus . CounterValue } ,
packets : typedDesc { prometheus . NewDesc (
2017-09-28 06:06:26 -07:00
prometheus . BuildFQName ( namespace , "qdisc" , "packets_total" ) ,
2017-05-23 02:55:50 -07:00
"Number of packets sent." ,
[ ] string { "device" , "kind" } , nil ,
) , prometheus . CounterValue } ,
drops : typedDesc { prometheus . NewDesc (
2017-09-28 06:06:26 -07:00
prometheus . BuildFQName ( namespace , "qdisc" , "drops_total" ) ,
2017-05-23 02:55:50 -07:00
"Number of packets dropped." ,
[ ] string { "device" , "kind" } , nil ,
) , prometheus . CounterValue } ,
requeues : typedDesc { prometheus . NewDesc (
2017-09-28 06:06:26 -07:00
prometheus . BuildFQName ( namespace , "qdisc" , "requeues_total" ) ,
2017-05-23 02:55:50 -07:00
"Number of packets dequeued, not transmitted, and requeued." ,
[ ] string { "device" , "kind" } , nil ,
) , prometheus . CounterValue } ,
overlimits : typedDesc { prometheus . NewDesc (
2017-09-28 06:06:26 -07:00
prometheus . BuildFQName ( namespace , "qdisc" , "overlimits_total" ) ,
2017-05-23 02:55:50 -07:00
"Number of overlimit packets." ,
[ ] string { "device" , "kind" } , nil ,
) , prometheus . CounterValue } ,
2020-06-03 07:30:18 -07:00
qlength : typedDesc { prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "qdisc" , "current_queue_length" ) ,
"Number of packets currently in queue to be sent." ,
[ ] string { "device" , "kind" } , nil ,
) , prometheus . GaugeValue } ,
backlog : typedDesc { prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "qdisc" , "backlog" ) ,
"Number of bytes currently in queue to be sent." ,
[ ] string { "device" , "kind" } , nil ,
) , prometheus . GaugeValue } ,
2022-04-10 04:58:01 -07:00
logger : logger ,
deviceFilter : newDeviceFilter ( * collectorQdiskDeviceExclude , * collectorQdiskDeviceExclude ) ,
2017-05-23 02:55:50 -07:00
} , nil
}
func testQdiscGet ( fixtures string ) ( [ ] qdisc . QdiscInfo , error ) {
var res [ ] qdisc . QdiscInfo
2022-07-27 11:59:39 -07:00
b , err := os . ReadFile ( filepath . Join ( fixtures , "results.json" ) )
2017-05-23 02:55:50 -07:00
if err != nil {
return res , err
}
err = json . Unmarshal ( b , & res )
return res , err
}
func ( c * qdiscStatCollector ) Update ( ch chan <- prometheus . Metric ) error {
var msgs [ ] qdisc . QdiscInfo
var err error
fixtures := * collectorQdisc
if fixtures == "" {
msgs , err = qdisc . Get ( )
} else {
msgs , err = testQdiscGet ( fixtures )
}
if err != nil {
return err
}
for _ , msg := range msgs {
// Only report root qdisc information.
if msg . Parent != 0 {
continue
}
2022-04-10 04:58:01 -07:00
if c . deviceFilter . ignored ( msg . IfaceName ) {
continue
}
2017-05-23 02:55:50 -07:00
ch <- c . bytes . mustNewConstMetric ( float64 ( msg . Bytes ) , msg . IfaceName , msg . Kind )
ch <- c . packets . mustNewConstMetric ( float64 ( msg . Packets ) , msg . IfaceName , msg . Kind )
ch <- c . drops . mustNewConstMetric ( float64 ( msg . Drops ) , msg . IfaceName , msg . Kind )
ch <- c . requeues . mustNewConstMetric ( float64 ( msg . Requeues ) , msg . IfaceName , msg . Kind )
ch <- c . overlimits . mustNewConstMetric ( float64 ( msg . Overlimits ) , msg . IfaceName , msg . Kind )
2020-06-03 07:30:18 -07:00
ch <- c . qlength . mustNewConstMetric ( float64 ( msg . Qlen ) , msg . IfaceName , msg . Kind )
ch <- c . backlog . mustNewConstMetric ( float64 ( msg . Backlog ) , msg . IfaceName , msg . Kind )
2017-05-23 02:55:50 -07:00
}
return nil
}