Skip to content

Commit

Permalink
Merge pull request #2 from rickstaa/snap-main
Browse files Browse the repository at this point in the history
fix: fix string metric bug
  • Loading branch information
paha authored Aug 1, 2023
2 parents 7eaf1b8 + 2f32faa commit 516f4f1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# IDE
.vscode
*code-workspace
42 changes: 27 additions & 15 deletions liquidctl-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ var (
defaultLiquidCMD = "/usr/local/bin/liquidctl"
)

// liquidctl statistic object as of v1.6.x
// liquidctl statistic object as of v1.6.x.
type liquidctlStatistic struct {
Bus string `json:"bus"`
Address string `json:"address"`
Description string `json:"description"`
Status []status `json:"status"`
}

// liquidctl status
// liquidctl status.
type status struct {
Key string `json:"key"`
Value float64 `json:"value"`
Unit string `json:"unit"`
Key string `json:"key"`
Value interface{} `json:"value"` // liquidctl also send string metrics
Unit string `json:"unit"`
}

// Metrics store per device ({deviceID: {metricID: prom.Gauge}})
Expand All @@ -56,7 +56,7 @@ func init() {
if !ok {
path = defaultLiquidCMD
} else {
path = p
path = p
}
log.Infof("liquidctl configured path, %s", path)

Expand All @@ -66,14 +66,21 @@ func init() {
devices[dname] = map[string]prometheus.Gauge{}
for _, m := range d.Status {
name := metricName(m.Key, dname)
log.Infof("Registering metric %s for %s device", name, dname)
devices[dname][name] = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: name,
Help: fmt.Sprintf("%s %s (%s).", d.Description, m.Key, m.Unit),
},
)
prometheus.MustRegister(devices[dname][name])

// 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(
prometheus.GaugeOpts{
Name: name,
Help: fmt.Sprintf("%s %s (%s).", d.Description, m.Key, m.Unit),
},
)
prometheus.MustRegister(devices[dname][name])
default: // Currently only float64 are implemented
log.Warnf("Registering metric '%s' for '%s' device failed since liquidctrl-exported does not yet support metrics of type '%T!'\n", name, dname, t)
}
}
}
}
Expand Down Expand Up @@ -111,7 +118,12 @@ func main() {
dname := deviceName(d.Address)
for _, m := range d.Status {
name := metricName(m.Key, dname)
devices[dname][name].Set(m.Value)

// Push metric to db if it was registered
_, ok := devices[dname][name]
if ok {
devices[dname][name].Set(m.Value.(float64))
}
}
}

Expand Down

0 comments on commit 516f4f1

Please sign in to comment.