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.
This commit is contained in:
2023-12-16 00:22:09 +00:00
parent 28589d0d16
commit 471ba437c4
3 changed files with 57 additions and 37 deletions

65
main.go
View File

@@ -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

View File

@@ -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
}

View File

@@ -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)