|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.buddy.wtf/buddy/open-hardware-monitor-client/lib/client"
|
|
|
|
"github.com/gosimple/slug"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Collector type
|
|
|
|
type Collector struct {
|
|
|
|
vals []client.Value
|
|
|
|
descs []*prometheus.Desc
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromVals gets a collector
|
|
|
|
func FromVals(vals []client.Value) *Collector {
|
|
|
|
descs := make([]*prometheus.Desc, len(vals))
|
|
|
|
for i, val := range vals {
|
|
|
|
params := []string{}
|
|
|
|
for _, hw := range val.Hardware {
|
|
|
|
hwt := hw.Type
|
|
|
|
typeslug := slug.Make(hwt.String())
|
|
|
|
params = append(params, typeslug)
|
|
|
|
}
|
|
|
|
params = append(params, "unit")
|
|
|
|
name, labels := val.MetricName()
|
|
|
|
descs[i] = prometheus.NewDesc(
|
|
|
|
name,
|
|
|
|
name,
|
|
|
|
params,
|
|
|
|
labels,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Collector{
|
|
|
|
vals: vals,
|
|
|
|
descs: descs,
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Describe method for interface
|
|
|
|
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
|
|
|
|
for _, d := range c.descs {
|
|
|
|
ch <- d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect method for interface
|
|
|
|
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
|
|
|
for i, val := range c.vals {
|
|
|
|
desc := c.descs[i]
|
|
|
|
labelVals := []string{}
|
|
|
|
for _, hw := range val.Hardware {
|
|
|
|
labelVals = append(labelVals, hw.Value)
|
|
|
|
}
|
|
|
|
labelVals = append(labelVals, slug.Make(val.Unit.String()))
|
|
|
|
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, val.Float64(), labelVals...)
|
|
|
|
}
|
|
|
|
}
|