@@ -45,13 +45,19 @@ var (
4545 "CPU flags." ,
4646 []string {"cpu" , "flag" }, nil ,
4747 )
48+ nodeCPUContextSwitchDesc = prometheus .NewDesc (
49+ prometheus .BuildFQName (namespace , cpuCollectorSubsystem , "context_switches_total" ),
50+ "Number of context switches." ,
51+ []string {"cpu" }, nil ,
52+ )
4853)
4954
5055type cpuCollector struct {
51- cpu typedDesc
52- cpuPhysical typedDesc
53- cpuRunQueue typedDesc
54- cpuFlags typedDesc
56+ cpu typedDesc
57+ cpuPhysical typedDesc
58+ cpuRunQueue typedDesc
59+ cpuFlags typedDesc
60+ cpuContextSwitch typedDesc
5561
5662 logger * slog.Logger
5763 tickPerSecond int64
@@ -75,12 +81,13 @@ func NewCpuCollector(logger *slog.Logger) (Collector, error) {
7581 return nil , err
7682 }
7783 return & cpuCollector {
78- cpu : typedDesc {nodeCPUSecondsDesc , prometheus .CounterValue },
79- cpuPhysical : typedDesc {nodeCPUPhysicalSecondsDesc , prometheus .CounterValue },
80- cpuRunQueue : typedDesc {nodeCPUSRunQueueDesc , prometheus .GaugeValue },
81- cpuFlags : typedDesc {nodeCPUFlagsDesc , prometheus .GaugeValue },
82- logger : logger ,
83- tickPerSecond : ticks ,
84+ cpu : typedDesc {nodeCPUSecondsDesc , prometheus .CounterValue },
85+ cpuPhysical : typedDesc {nodeCPUPhysicalSecondsDesc , prometheus .CounterValue },
86+ cpuRunQueue : typedDesc {nodeCPUSRunQueueDesc , prometheus .GaugeValue },
87+ cpuFlags : typedDesc {nodeCPUFlagsDesc , prometheus .GaugeValue },
88+ cpuContextSwitch : typedDesc {nodeCPUContextSwitchDesc , prometheus .CounterValue },
89+ logger : logger ,
90+ tickPerSecond : ticks ,
8491 }, nil
8592}
8693
@@ -108,6 +115,9 @@ func (c *cpuCollector) Update(ch chan<- prometheus.Metric) error {
108115
109116 // Flags
110117 ch <- c .cpuFlags .mustNewConstMetric (float64 (stat .SpurrFlag ), strconv .Itoa (n ), "spurr" )
118+
119+ // Context switches
120+ ch <- c .cpuContextSwitch .mustNewConstMetric (float64 (stat .CSwitches ), strconv .Itoa (n ))
111121 }
112122 return nil
113123}
0 commit comments