@@ -27,8 +27,15 @@ import (
2727 "github.com/prometheus/procfs"
2828)
2929
30+ const (
31+ psiResourceCPU = "cpu"
32+ psiResourceIO = "io"
33+ psiResourceMemory = "memory"
34+ psiResourceIRQ = "irq"
35+ )
36+
3037var (
31- psiResources = []string {"cpu" , "io" , "memory" , "irq" }
38+ psiResources = []string {psiResourceCPU , psiResourceIO , psiResourceMemory , psiResourceIRQ }
3239)
3340
3441type pressureStatsCollector struct {
@@ -93,13 +100,18 @@ func NewPressureStatsCollector(logger *slog.Logger) (Collector, error) {
93100
94101// Update calls procfs.NewPSIStatsForResource for the different resources and updates the values
95102func (c * pressureStatsCollector ) Update (ch chan <- prometheus.Metric ) error {
103+ foundResources := 0
96104 for _ , res := range psiResources {
97105 c .logger .Debug ("collecting statistics for resource" , "resource" , res )
98106 vals , err := c .fs .PSIStatsForResource (res )
99107 if err != nil {
100- if errors .Is (err , os .ErrNotExist ) {
101- c .logger .Debug ("pressure information is unavailable, you need a Linux kernel >= 4.20 and/or CONFIG_PSI enabled for your kernel" )
102- return ErrNoData
108+ if errors .Is (err , os .ErrNotExist ) && res != psiResourceIRQ {
109+ c .logger .Debug ("pressure information is unavailable, you need a Linux kernel >= 4.20 and/or CONFIG_PSI enabled for your kernel" , "resource" , res )
110+ continue
111+ }
112+ if errors .Is (err , os .ErrNotExist ) && res == psiResourceIRQ {
113+ c .logger .Debug ("IRQ pressure information is unavailable, you need a Linux kernel >= 6.1 and/or CONFIG_PSI enabled for your kernel" , "resource" , res )
114+ continue
103115 }
104116 if errors .Is (err , syscall .ENOTSUP ) {
105117 c .logger .Debug ("pressure information is disabled, add psi=1 kernel command line to enable it" )
@@ -109,28 +121,35 @@ func (c *pressureStatsCollector) Update(ch chan<- prometheus.Metric) error {
109121 }
110122 // IRQ pressure does not have 'some' data.
111123 // See https://github.com/torvalds/linux/blob/v6.9/include/linux/psi_types.h#L65
112- if vals .Some == nil && res != "irq" {
124+ if vals .Some == nil && res != psiResourceIRQ {
113125 c .logger .Debug ("pressure information returned no 'some' data" )
114126 return ErrNoData
115127 }
116- if vals .Full == nil && res != "cpu" {
128+ if vals .Full == nil && res != psiResourceCPU {
117129 c .logger .Debug ("pressure information returned no 'full' data" )
118130 return ErrNoData
119131 }
120132 switch res {
121- case "cpu" :
133+ case psiResourceCPU :
122134 ch <- prometheus .MustNewConstMetric (c .cpu , prometheus .CounterValue , float64 (vals .Some .Total )/ 1000.0 / 1000.0 )
123- case "io" :
135+ case psiResourceIO :
124136 ch <- prometheus .MustNewConstMetric (c .io , prometheus .CounterValue , float64 (vals .Some .Total )/ 1000.0 / 1000.0 )
125137 ch <- prometheus .MustNewConstMetric (c .ioFull , prometheus .CounterValue , float64 (vals .Full .Total )/ 1000.0 / 1000.0 )
126- case "memory" :
138+ case psiResourceMemory :
127139 ch <- prometheus .MustNewConstMetric (c .mem , prometheus .CounterValue , float64 (vals .Some .Total )/ 1000.0 / 1000.0 )
128140 ch <- prometheus .MustNewConstMetric (c .memFull , prometheus .CounterValue , float64 (vals .Full .Total )/ 1000.0 / 1000.0 )
129- case "irq" :
141+ case psiResourceIRQ :
130142 ch <- prometheus .MustNewConstMetric (c .irqFull , prometheus .CounterValue , float64 (vals .Full .Total )/ 1000.0 / 1000.0 )
131143 default :
132144 c .logger .Debug ("did not account for resource" , "resource" , res )
145+ continue
133146 }
147+ foundResources ++
148+ }
149+
150+ if foundResources == 0 {
151+ c .logger .Debug ("pressure information is unavailable, you need a Linux kernel >= 4.20 and/or CONFIG_PSI enabled for your kernel" )
152+ return ErrNoData
134153 }
135154
136155 return nil
0 commit comments