From 471ba437c43db4eefe228fa6a007833a40b55af1 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 16 Dec 2023 00:22:09 +0000 Subject: [PATCH] fix(metrics): adhere to units in metric names The `*_amps` and `*_watts` metrics now use Ah and Wh values, instead of mAh and mWh as they were before. --- main.go | 65 +++++++++++++++++++++++++++-------------------- prom/collector.go | 25 +++++++++++------- prom/server.go | 4 ++- 3 files changed, 57 insertions(+), 37 deletions(-) diff --git a/main.go b/main.go index 5b3ec63..111b905 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,9 @@ import ( ) var ( + version = "0.0.0-dev" + commit = "unknown" + outputFlag = flag.String( "o", "", "Output file to write to in Prometheus format", ) @@ -24,8 +27,11 @@ var ( "b", "127.0.0.1", "Bind address to run server on", ) portFlag = flag.Int("p", 9108, "Port to run server on") - namespaceFlag = flag.String("n", "node", "Namespace for metrics") - logLevelFlag = flag.String("l", "info", "Log level") + namespaceFlag = flag.String( + "n", prom.DefaultNamespace, "Namespace for metrics", + ) + logLevelFlag = flag.String("l", "info", "Log level") + versionFlag = flag.Bool("v", false, "Print version and exit") ) func main() { @@ -42,6 +48,11 @@ func mainE() error { return err } + if *versionFlag { + fmt.Printf("macos-battery-exporter %s (%s)\n", version, commit) + return nil + } + if *serverFlag { opts := prom.ServerOptions{ Bind: *bindFlag, @@ -53,32 +64,32 @@ func mainE() error { prometheus.DefaultRegisterer.(*prometheus.Registry), opts, ) + } + + registry := prometheus.NewRegistry() + err = registry.Register(prom.NewCollector(*namespaceFlag)) + if err != nil { + return err + } + + gatherers := prometheus.Gatherers{registry} + metricFamilies, err := gatherers.Gather() + if err != nil { + return err + } + + var sb strings.Builder + for _, mf := range metricFamilies { + _, err := expfmt.MetricFamilyToText(&sb, mf) + if err != nil { + return err + } + } + + if *outputFlag != "" { + return writeToFile(sb.String(), *outputFlag) } else { - registry := prometheus.NewRegistry() - err := registry.Register(prom.NewCollector(*namespaceFlag)) - if err != nil { - return err - } - - gatherers := prometheus.Gatherers{registry} - metricFamilies, err := gatherers.Gather() - if err != nil { - return err - } - - var sb strings.Builder - for _, mf := range metricFamilies { - _, err := expfmt.MetricFamilyToText(&sb, mf) - if err != nil { - return err - } - } - - if *outputFlag != "" { - return writeToFile(sb.String(), *outputFlag) - } else { - fmt.Print(sb.String()) - } + fmt.Print(sb.String()) } return nil diff --git a/prom/collector.go b/prom/collector.go index 9012db8..7963064 100644 --- a/prom/collector.go +++ b/prom/collector.go @@ -4,6 +4,7 @@ package prom import ( "log/slog" + "math" "github.com/jimeh/macos-battery-exporter/battery" "github.com/prometheus/client_golang/prometheus" @@ -237,25 +238,25 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric( c.descChargeRateAmps, prometheus.GaugeValue, - float64(battery.ChargeRateAmps)/1000, + roundTo(float64(battery.ChargeRateAmps)/1000, 6), labels..., ) ch <- prometheus.MustNewConstMetric( c.descChargeRateWatts, prometheus.GaugeValue, - battery.ChargeRateWatts/1000, + roundTo(battery.ChargeRateWatts/1000, 6), labels..., ) ch <- prometheus.MustNewConstMetric( c.descCurrentCapacityAmps, prometheus.GaugeValue, - float64(battery.CurrentCapacityAmps)/1000, + roundTo(float64(battery.CurrentCapacityAmps)/1000, 6), labels..., ) ch <- prometheus.MustNewConstMetric( c.descCurrentCapacityWatts, prometheus.GaugeValue, - battery.CurrentCapacityWatts/1000, + roundTo(battery.CurrentCapacityWatts/1000, 6), labels..., ) ch <- prometheus.MustNewConstMetric( @@ -273,13 +274,13 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric( c.descDesignCapacityAmps, prometheus.GaugeValue, - float64(battery.DesignCapacityAmps)/1000, + roundTo(float64(battery.DesignCapacityAmps)/1000, 6), labels..., ) ch <- prometheus.MustNewConstMetric( c.descDesignCapacityWatts, prometheus.GaugeValue, - battery.DesignCapacityWatts/1000, + roundTo(battery.DesignCapacityWatts/1000, 6), labels..., ) ch <- prometheus.MustNewConstMetric( @@ -303,13 +304,13 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric( c.descMaxCapacityAmps, prometheus.GaugeValue, - float64(battery.MaxCapacityAmps)/1000, + roundTo(float64(battery.MaxCapacityAmps)/1000, 6), labels..., ) ch <- prometheus.MustNewConstMetric( c.descMaxCapacityWatts, prometheus.GaugeValue, - battery.MaxCapacityWatts/1000, + roundTo(battery.MaxCapacityWatts/1000, 6), labels..., ) ch <- prometheus.MustNewConstMetric( @@ -327,7 +328,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric( c.descVoltage, prometheus.GaugeValue, - float64(battery.Voltage)/1000, + roundTo(float64(battery.Voltage)/1000, 6), labels..., ) } @@ -348,3 +349,9 @@ func boolToFloat64(b bool) float64 { return 0 } + +// roundTo rounds a float64 to 'places' decimal places +func roundTo(value float64, places int) float64 { + shift := math.Pow(10, float64(places)) + return math.Round(value*shift) / shift +} diff --git a/prom/server.go b/prom/server.go index ee5c96e..073b28b 100644 --- a/prom/server.go +++ b/prom/server.go @@ -12,6 +12,8 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) +const DefaultNamespace = "macos" + type Registry interface { prometheus.Registerer prometheus.Gatherer @@ -96,7 +98,7 @@ func RunServer( options ServerOptions, ) error { if namespace == "" { - namespace = "node" + namespace = DefaultNamespace } s := NewServer(registry, options)