Merge pull request #177 from prometheus/cpu

Switch linux stat to using ConstMetric
pull/170/merge
Brian Brazil 2015-12-18 13:36:06 +00:00
commit 514f5ad4a9
1 changed files with 49 additions and 58 deletions

View File

@ -29,15 +29,16 @@ const (
) )
type statCollector struct { type statCollector struct {
cpu *prometheus.CounterVec cpu *prometheus.Desc
intr prometheus.Counter intr *prometheus.Desc
ctxt prometheus.Counter ctxt *prometheus.Desc
forks prometheus.Counter forks *prometheus.Desc
btime prometheus.Gauge btime *prometheus.Desc
procsRunning prometheus.Gauge procsRunning *prometheus.Desc
procsBlocked prometheus.Gauge procsBlocked *prometheus.Desc
} }
func init() { func init() {
Factories["stat"] = NewStatCollector Factories["stat"] = NewStatCollector
} }
@ -46,44 +47,41 @@ func init() {
// kernel/system statistics. // kernel/system statistics.
func NewStatCollector() (Collector, error) { func NewStatCollector() (Collector, error) {
return &statCollector{ return &statCollector{
cpu: prometheus.NewCounterVec( cpu: prometheus.NewDesc(
prometheus.CounterOpts{ prometheus.BuildFQName(Namespace, "", "cpu"),
Namespace: Namespace, "Seconds the cpus spent in each mode.",
Name: "cpu", []string{"cpu", "mode"}, nil,
Help: "Seconds the cpus spent in each mode.", ),
}, intr: prometheus.NewDesc(
[]string{"cpu", "mode"}, prometheus.BuildFQName(Namespace, "", "intr"),
"Total number of interrupts serviced.",
nil, nil,
),
ctxt: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", "context_switches"),
"Total number of context switches.",
nil, nil,
),
forks: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", "forks"),
"Total number of forks.",
nil, nil,
),
btime: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", "boot_time"),
"Node boot time, in unixtime.",
nil, nil,
),
procsRunning: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", "procs_running"),
"Number of processes in runnable state.",
nil, nil,
),
procsBlocked: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", "procs_blocked"),
"Number of processes blocked waiting for I/O to complete.",
nil, nil,
), ),
intr: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: Namespace,
Name: "intr",
Help: "Total number of interrupts serviced.",
}),
ctxt: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: Namespace,
Name: "context_switches",
Help: "Total number of context switches.",
}),
forks: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: Namespace,
Name: "forks",
Help: "Total number of forks.",
}),
btime: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "boot_time",
Help: "Node boot time, in unixtime.",
}),
procsRunning: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "procs_running",
Help: "Number of processes in runnable state.",
}),
procsBlocked: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "procs_blocked",
Help: "Number of processes blocked waiting for I/O to complete.",
}),
}, nil }, nil
} }
@ -121,7 +119,7 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) {
} }
// Convert from ticks to seconds // Convert from ticks to seconds
value /= userHz value /= userHz
c.cpu.With(prometheus.Labels{"cpu": parts[0], "mode": cpuFields[i]}).Set(value) ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, value, parts[0], cpuFields[i])
} }
case parts[0] == "intr": case parts[0] == "intr":
// Only expose the overall number, use the 'interrupts' collector for more detail. // Only expose the overall number, use the 'interrupts' collector for more detail.
@ -129,45 +127,38 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) {
if err != nil { if err != nil {
return err return err
} }
c.intr.Set(value) ch <- prometheus.MustNewConstMetric(c.intr, prometheus.CounterValue, value)
case parts[0] == "ctxt": case parts[0] == "ctxt":
value, err := strconv.ParseFloat(parts[1], 64) value, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
return err return err
} }
c.ctxt.Set(value) ch <- prometheus.MustNewConstMetric(c.ctxt, prometheus.CounterValue, value)
case parts[0] == "processes": case parts[0] == "processes":
value, err := strconv.ParseFloat(parts[1], 64) value, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
return err return err
} }
c.forks.Set(value) ch <- prometheus.MustNewConstMetric(c.forks, prometheus.CounterValue, value)
case parts[0] == "btime": case parts[0] == "btime":
value, err := strconv.ParseFloat(parts[1], 64) value, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
return err return err
} }
c.btime.Set(value) ch <- prometheus.MustNewConstMetric(c.btime, prometheus.GaugeValue, value)
case parts[0] == "procs_running": case parts[0] == "procs_running":
value, err := strconv.ParseFloat(parts[1], 64) value, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
return err return err
} }
c.procsRunning.Set(value) ch <- prometheus.MustNewConstMetric(c.procsRunning, prometheus.GaugeValue, value)
case parts[0] == "procs_blocked": case parts[0] == "procs_blocked":
value, err := strconv.ParseFloat(parts[1], 64) value, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
return err return err
} }
c.procsBlocked.Set(value) ch <- prometheus.MustNewConstMetric(c.procsBlocked, prometheus.GaugeValue, value)
} }
} }
c.cpu.Collect(ch)
c.ctxt.Collect(ch)
c.intr.Collect(ch)
c.forks.Collect(ch)
c.btime.Collect(ch)
c.procsRunning.Collect(ch)
c.procsBlocked.Collect(ch)
return err return err
} }