mirror of
				https://github.com/prometheus/node_exporter.git
				synced 2025-08-20 18:33:52 -07:00 
			
		
		
		
	diskstats_linux.go: add collector.diskstats.disable-queue-stats flag
Skips call to `SysBlockDeviceQueueStats` to improve performance over a large number of devices.
This commit is contained in:
		
							parent
							
								
									47e2bb34ce
								
							
						
					
					
						commit
						66ed666f2e
					
				| 
						 | 
					@ -24,6 +24,7 @@ import (
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/alecthomas/kingpin/v2"
 | 
				
			||||||
	"github.com/prometheus/client_golang/prometheus"
 | 
						"github.com/prometheus/client_golang/prometheus"
 | 
				
			||||||
	"github.com/prometheus/procfs/blockdevice"
 | 
						"github.com/prometheus/procfs/blockdevice"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
| 
						 | 
					@ -65,6 +66,10 @@ const (
 | 
				
			||||||
	udevSCSIIdentSerial         = "SCSI_IDENT_SERIAL"
 | 
						udevSCSIIdentSerial         = "SCSI_IDENT_SERIAL"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						disableQueueStats = kingpin.Flag("collector.diskstats.disable-queue-stats", "disable queue stats").Default("false").Bool()
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type typedFactorDesc struct {
 | 
					type typedFactorDesc struct {
 | 
				
			||||||
	desc      *prometheus.Desc
 | 
						desc      *prometheus.Desc
 | 
				
			||||||
	valueType prometheus.ValueType
 | 
						valueType prometheus.ValueType
 | 
				
			||||||
| 
						 | 
					@ -106,13 +111,16 @@ func NewDiskstatsCollector(logger *slog.Logger) (Collector, error) {
 | 
				
			||||||
		return nil, fmt.Errorf("failed to parse device filter flags: %w", err)
 | 
							return nil, fmt.Errorf("failed to parse device filter flags: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						infoLabels := []string{"device", "major", "minor", "path", "wwn", "model", "serial", "revision"}
 | 
				
			||||||
 | 
						if !*disableQueueStats {
 | 
				
			||||||
 | 
							infoLabels = append(infoLabels, "rotational")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	collector := diskstatsCollector{
 | 
						collector := diskstatsCollector{
 | 
				
			||||||
		deviceFilter: deviceFilter,
 | 
							deviceFilter: deviceFilter,
 | 
				
			||||||
		fs:           fs,
 | 
							fs:           fs,
 | 
				
			||||||
		infoDesc: typedFactorDesc{
 | 
							infoDesc: typedFactorDesc{
 | 
				
			||||||
			desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "info"),
 | 
								desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "info"),
 | 
				
			||||||
				"Info of /sys/block/<block_device>.",
 | 
									"Info of /sys/block/<block_device>.", infoLabels,
 | 
				
			||||||
				[]string{"device", "major", "minor", "path", "wwn", "model", "serial", "revision", "rotational"},
 | 
					 | 
				
			||||||
				nil,
 | 
									nil,
 | 
				
			||||||
			), valueType: prometheus.GaugeValue,
 | 
								), valueType: prometheus.GaugeValue,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
| 
						 | 
					@ -294,13 +302,8 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
 | 
				
			||||||
			serial = info[udevIDSerialShort]
 | 
								serial = info[udevIDSerialShort]
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		queueStats, err := c.fs.SysBlockDeviceQueueStats(dev)
 | 
							labels := []string{
 | 
				
			||||||
		// Block Device Queue stats may not exist for all devices.
 | 
								dev,
 | 
				
			||||||
		if err != nil && !os.IsNotExist(err) {
 | 
					 | 
				
			||||||
			c.logger.Debug("Failed to get block device queue stats", "device", dev, "err", err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		ch <- c.infoDesc.mustNewConstMetric(1.0, dev,
 | 
					 | 
				
			||||||
			fmt.Sprint(stats.MajorNumber),
 | 
								fmt.Sprint(stats.MajorNumber),
 | 
				
			||||||
			fmt.Sprint(stats.MinorNumber),
 | 
								fmt.Sprint(stats.MinorNumber),
 | 
				
			||||||
			info[udevIDPath],
 | 
								info[udevIDPath],
 | 
				
			||||||
| 
						 | 
					@ -308,8 +311,18 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
 | 
				
			||||||
			info[udevIDModel],
 | 
								info[udevIDModel],
 | 
				
			||||||
			serial,
 | 
								serial,
 | 
				
			||||||
			info[udevIDRevision],
 | 
								info[udevIDRevision],
 | 
				
			||||||
			strconv.FormatUint(queueStats.Rotational, 2),
 | 
							}
 | 
				
			||||||
		)
 | 
					
 | 
				
			||||||
 | 
							if !*disableQueueStats {
 | 
				
			||||||
 | 
								queueStats, err := c.fs.SysBlockDeviceQueueStats(dev)
 | 
				
			||||||
 | 
								// Block Device Queue stats may not exist for all devices.
 | 
				
			||||||
 | 
								if err != nil && !os.IsNotExist(err) {
 | 
				
			||||||
 | 
									c.logger.Debug("Failed to get block device queue stats", "device", dev, "err", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								labels = append(labels, strconv.FormatUint(queueStats.Rotational, 2))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ch <- c.infoDesc.mustNewConstMetric(1.0, labels...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		statCount := stats.IoStatsCount - 3 // Total diskstats record count, less MajorNumber, MinorNumber and DeviceName
 | 
							statCount := stats.IoStatsCount - 3 // Total diskstats record count, less MajorNumber, MinorNumber and DeviceName
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue