Skip to content

Commit

Permalink
Merge pull request #389 from prometheus/fish-use-const-metrics
Browse files Browse the repository at this point in the history
Convert remaining collectors to use ConstMetrics
  • Loading branch information
discordianfish committed Jan 3, 2017
2 parents d2ca252 + 8e50b80 commit 079fd70
Show file tree
Hide file tree
Showing 17 changed files with 455 additions and 614 deletions.
34 changes: 13 additions & 21 deletions collector/bonding_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

type bondingCollector struct {
slaves, active *prometheus.GaugeVec
slaves, active typedDesc
}

func init() {
Expand All @@ -37,22 +37,16 @@ func init() {
// It exposes the number of configured and active slave of linux bonding interfaces.
func NewBondingCollector() (Collector, error) {
return &bondingCollector{
slaves: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Name: "net_bonding_slaves",
Help: "Number of configured slaves per bonding interface.",
},
[]string{"master"},
),
active: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Name: "net_bonding_slaves_active",
Help: "Number of active slaves per bonding interface.",
},
[]string{"master"},
),
slaves: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "bonding", "slaves"),
"Number of configured slaves per bonding interface.",
[]string{"master"}, nil,
), prometheus.GaugeValue},
active: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "bonding", "active"),
"Number of active slaves per bonding interface.",
[]string{"master"}, nil,
), prometheus.GaugeValue},
}, nil
}

Expand All @@ -63,11 +57,9 @@ func (c *bondingCollector) Update(ch chan<- prometheus.Metric) (err error) {
return err
}
for master, status := range bondingStats {
c.slaves.WithLabelValues(master).Set(float64(status[0]))
c.active.WithLabelValues(master).Set(float64(status[1]))
ch <- c.slaves.mustNewConstMetric(float64(status[0]), master)
ch <- c.active.mustNewConstMetric(float64(status[1]), master)
}
c.slaves.Collect(ch)
c.active.Collect(ch)
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ type Collector interface {
// Get new metrics and expose them via prometheus registry.
Update(ch chan<- prometheus.Metric) (err error)
}

type typedDesc struct {
desc *prometheus.Desc
valueType prometheus.ValueType
}

func (d *typedDesc) mustNewConstMetric(value float64, labels ...string) prometheus.Metric {
return prometheus.MustNewConstMetric(d.desc, d.valueType, value, labels...)
}
24 changes: 10 additions & 14 deletions collector/cpu_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,11 @@ func init() {
// CPU stats.
func NewStatCollector() (Collector, error) {
return &statCollector{
cpu: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Name: "cpu_seconds_total",
Help: "Seconds the CPU spent in each mode.",
},
[]string{"cpu", "mode"},
),
cpu: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "cpu", "seconds_total"),
"Seconds the CPU spent in each mode.",
[]string{"cpu", "mode"}, nil,
), prometheus.CounterValue},
}, nil
}

Expand All @@ -118,12 +115,11 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) {
return err
}
for cpu, t := range cpuTimes {
c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(cpu), "mode": "user"}).Set(t.user)
c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(cpu), "mode": "nice"}).Set(t.nice)
c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(cpu), "mode": "system"}).Set(t.sys)
c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(cpu), "mode": "interrupt"}).Set(t.intr)
c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(cpu), "mode": "idle"}).Set(t.idle)
ch <- c.cpu.mustNewConstMetric(float64(cpuTimes[base_idx+C.CP_USER]), strconv.Itoa(cpu), "user")
ch <- c.cpu.mustNewConstMetric(float64(cpuTimes[base_idx+C.CP_NICE]), strconv.Itoa(cpu), "nice")
ch <- c.cpu.mustNewConstMetric(float64(cpuTimes[base_idx+C.CP_SYS]), strconv.Itoa(cpu), "system")
ch <- c.cpu.mustNewConstMetric(float64(cpuTimes[base_idx+C.CP_INTR]), strconv.Itoa(cpu), "interrupt")
ch <- c.cpu.mustNewConstMetric(float64(cpuTimes[base_idx+C.CP_IDLE]), strconv.Itoa(cpu), "idle")
}
c.cpu.Collect(ch)
return err
}
113 changes: 41 additions & 72 deletions collector/devstat_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ const (
)

type devstatCollector struct {
bytes *prometheus.CounterVec
bytes_total *prometheus.CounterVec
transfers *prometheus.CounterVec
duration *prometheus.CounterVec
busyTime *prometheus.CounterVec
blocks *prometheus.CounterVec
bytes typedDesc
bytes_total typedDesc
transfers typedDesc
duration typedDesc
busyTime typedDesc
blocks typedDesc
}

func init() {
Expand All @@ -153,51 +153,31 @@ func init() {
// Device stats.
func NewDevstatCollector() (Collector, error) {
return &devstatCollector{
bytes: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: devstatSubsystem,
Name: "bytes_total",
Help: "The total number of bytes in transactions.",
},
[]string{"device", "type"},
),
transfers: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: devstatSubsystem,
Name: "transfers_total",
Help: "The total number of transactions.",
},
[]string{"device", "type"},
),
duration: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: devstatSubsystem,
Name: "duration_seconds_total",
Help: "The total duration of transactions in seconds.",
},
[]string{"device", "type"},
),
busyTime: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: devstatSubsystem,
Name: "busy_time_seconds_total",
Help: "Total time the device had one or more transactions outstanding in seconds.",
},
[]string{"device"},
),
blocks: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: devstatSubsystem,
Name: "blocks_transferred_total",
Help: "The total number of blocks transferred.",
},
[]string{"device"},
),
bytes: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(Namespace, devstatSubsystem, "bytes_total"),
"The total number of bytes in transactions.",
[]string{"device", "type"}, nil,
), prometheus.CounterValue},
transfers: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(Namespace, devstatSubsystem, "transfers_total"),
"The total number of transactions.",
[]string{"device", "type"}, nil,
), prometheus.CounterValue},
duration: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(Namespace, devstatSubsystem, "duration_seconds_total"),
"The total duration of transactions in seconds.",
[]string{"device", "type"}, nil,
), prometheus.CounterValue},
busyTime: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(Namespace, devstatSubsystem, "busy_time_seconds_total"),
"Total time the device had one or more transactions outstanding in seconds.",
[]string{"device"}, nil,
), prometheus.CounterValue},
blocks: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(Namespace, devstatSubsystem, "blocks_transferred_total"),
"The total number of blocks transferred.",
[]string{"device"}, nil,
), prometheus.CounterValue},
}, nil
}

Expand All @@ -213,27 +193,16 @@ func (c *devstatCollector) Update(ch chan<- prometheus.Metric) (err error) {
for i := C.int(0); i < count; i++ {
stats := C._get_stats(i)
device := fmt.Sprintf("%s%d", C.GoString(&stats.device[0]), stats.unit)
// Free metrics are disabled for now, please see PR #88 for more details.
c.bytes.With(prometheus.Labels{"device": device, "type": "read"}).Set(float64(stats.bytes.read))
c.bytes.With(prometheus.Labels{"device": device, "type": "write"}).Set(float64(stats.bytes.write))
//c.bytes.With(prometheus.Labels{"device": device, "type": "free"}).Set(float64(stats.bytes.free))
c.transfers.With(prometheus.Labels{"device": device, "type": "other"}).Set(float64(stats.transfers.other))
c.transfers.With(prometheus.Labels{"device": device, "type": "read"}).Set(float64(stats.transfers.read))
c.transfers.With(prometheus.Labels{"device": device, "type": "write"}).Set(float64(stats.transfers.write))
//c.transfers.With(prometheus.Labels{"device": device, "type": "free"}).Set(float64(stats.transfers.free))
c.duration.With(prometheus.Labels{"device": device, "type": "other"}).Set(float64(stats.duration.other))
c.duration.With(prometheus.Labels{"device": device, "type": "read"}).Set(float64(stats.duration.read))
c.duration.With(prometheus.Labels{"device": device, "type": "write"}).Set(float64(stats.duration.write))
//c.duration.With(prometheus.Labels{"device": device, "type": "free"}).Set(float64(stats.duration.free))
c.busyTime.With(prometheus.Labels{"device": device}).Set(float64(stats.busyTime))
c.blocks.With(prometheus.Labels{"device": device}).Set(float64(stats.blocks))
ch <- c.bytes.mustNewConstMetric(float64(stats.bytes.read), device, "read")
ch <- c.bytes.mustNewConstMetric(float64(stats.bytes.write), device, "write")
ch <- c.transfers.mustNewConstMetric(float64(stats.transfers.other), device, "other")
ch <- c.transfers.mustNewConstMetric(float64(stats.transfers.read), device, "read")
ch <- c.transfers.mustNewConstMetric(float64(stats.transfers.write), device, "write")
ch <- c.duration.mustNewConstMetric(float64(stats.duration.other), device, "other")
ch <- c.duration.mustNewConstMetric(float64(stats.duration.read), device, "read")
ch <- c.duration.mustNewConstMetric(float64(stats.duration.write), device, "write")
ch <- c.busyTime.mustNewConstMetric(float64(stats.busyTime), device)
ch <- c.blocks.mustNewConstMetric(float64(stats.blocks), device)
}

c.bytes.Collect(ch)
c.transfers.Collect(ch)
c.duration.Collect(ch)
c.busyTime.Collect(ch)
c.blocks.Collect(ch)

return err
}
Loading

0 comments on commit 079fd70

Please sign in to comment.