Skip to content

Commit

Permalink
feat: improve prometheus variable naming
Browse files Browse the repository at this point in the history
This commit enhances Prometheus variable naming by adding the device as
a label instead of including it in the variable name. This change is made
to accommodate the dynamic nature of UNIX devices that may change with
reboots.
  • Loading branch information
rickstaa committed Nov 8, 2023
1 parent 516f4f1 commit 0b3ecb0
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions liquidctl-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
)

var (
defaultPort = "9530"
defaultPort = "9531"
defaultInterval = "10"
defaultLiquidCMD = "/usr/local/bin/liquidctl"
)
Expand All @@ -46,7 +46,7 @@ type status struct {
}

// Metrics store per device ({deviceID: {metricID: prom.Gauge}})
var devices = map[string]map[string]prometheus.Gauge{}
var devices = map[string]map[string]*prometheus.GaugeVec{}

// path to liquidctl executable
var path string
Expand All @@ -63,19 +63,20 @@ func init() {
// Register metrics available for each liquidctl device
for _, d := range liquidctl_stats() {
dname := deviceName(d.Address)
devices[dname] = map[string]prometheus.Gauge{}
devices[dname] = map[string]*prometheus.GaugeVec{}
for _, m := range d.Status {
name := metricName(m.Key, dname)

// Register metrics based on type.
switch t := m.Value.(type) {
case float64:
log.Infof("Registering metric '%s' for '%s' device", name, dname)
devices[dname][name] = prometheus.NewGauge(
devices[dname][name] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: name,
Help: fmt.Sprintf("%s %s (%s).", d.Description, m.Key, m.Unit),
},
[]string{"device"},
)
prometheus.MustRegister(devices[dname][name])
default: // Currently only float64 are implemented
Expand Down Expand Up @@ -122,7 +123,7 @@ func main() {
// Push metric to db if it was registered
_, ok := devices[dname][name]
if ok {
devices[dname][name].Set(m.Value.(float64))
devices[dname][name].WithLabelValues(dname).Set(m.Value.(float64))
}
}
}
Expand Down Expand Up @@ -154,7 +155,7 @@ func metricName(n, device string) string {
// trim + signes
name = strings.Trim(strings.ToLower(name), "+")
// Append common prefix for all metrics
name = fmt.Sprintf("%s_%s_%s", metricPrefix, device, name)
name = fmt.Sprintf("%s_%s", metricPrefix, name)
// trimming dots by split+join
return strings.Join(strings.Split(name, "."), "")
}
Expand Down

0 comments on commit 0b3ecb0

Please sign in to comment.