jmetrics library refactor
This commit is contained in:
parent
a8c952b039
commit
5e80ec9991
3 changed files with 16 additions and 58 deletions
61
exporter.go
61
exporter.go
|
@ -7,12 +7,11 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.drjosh.dev/josh/jmetrics"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
|
@ -30,61 +29,15 @@ var (
|
|||
[]string{"var"},
|
||||
)
|
||||
|
||||
lifepo4weredSummaries = map[string]*liteSummary{
|
||||
"VIN": newLiteSummary("voltage_in", "Voltage in (mV)"),
|
||||
"VOUT": newLiteSummary("voltage_out", "Voltage out (mV)"),
|
||||
"VBAT": newLiteSummary("voltage_bat", "Battery voltage (mV)"),
|
||||
"IOUT": newLiteSummary("current_out", "Current out (mA)"),
|
||||
lifepo4weredSummaries = map[string]*jmetrics.LiteGaugeSummary{
|
||||
"VIN": jmetrics.NewLiteGaugeSummary("lifepo4wered_voltage_in", "Voltage in (mV)"),
|
||||
"VOUT": jmetrics.NewLiteGaugeSummary("lifepo4wered_voltage_out", "Voltage out (mV)"),
|
||||
"VBAT": jmetrics.NewLiteGaugeSummary("lifepo4wered_voltage_bat", "Battery voltage (mV)"),
|
||||
"IOUT": jmetrics.NewLiteGaugeSummary("lifepo4wered_current_out", "Current out (mA)"),
|
||||
}
|
||||
lifepo4weredPOut = newLiteSummary("power_out", "Power out (mW)")
|
||||
lifepo4weredPOut = jmetrics.NewLiteGaugeSummary("lifepo4wered_power_out", "Power out (mW)")
|
||||
)
|
||||
|
||||
type liteSummary struct {
|
||||
mu sync.Mutex
|
||||
min float64
|
||||
max float64
|
||||
sum float64
|
||||
count int
|
||||
|
||||
desc *prometheus.Desc
|
||||
}
|
||||
|
||||
func newLiteSummary(varName, help string) *liteSummary {
|
||||
s := &liteSummary{
|
||||
min: math.Inf(1),
|
||||
max: math.Inf(-1),
|
||||
desc: prometheus.NewDesc("lifepo4wered_"+varName, help, []string{"stat"}, nil),
|
||||
}
|
||||
prometheus.MustRegister(s)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *liteSummary) Describe(ch chan<- *prometheus.Desc) { ch <- s.desc }
|
||||
|
||||
func (s *liteSummary) Collect(ch chan<- prometheus.Metric) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
ch <- prometheus.MustNewConstMetric(s.desc, prometheus.GaugeValue, s.min, "min")
|
||||
ch <- prometheus.MustNewConstMetric(s.desc, prometheus.GaugeValue, s.max, "max")
|
||||
mean := s.sum
|
||||
if s.count > 0 {
|
||||
mean /= float64(s.count)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(s.desc, prometheus.GaugeValue, mean, "mean")
|
||||
s.min = math.Inf(1)
|
||||
s.max = math.Inf(-1)
|
||||
s.sum, s.count = 0, 0
|
||||
}
|
||||
|
||||
func (s *liteSummary) Observe(x float64) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.min = min(s.min, x)
|
||||
s.max = max(s.max, x)
|
||||
s.sum += x
|
||||
s.count++
|
||||
}
|
||||
|
||||
func pollVars() {
|
||||
cmd := exec.Command("lifepo4wered-cli", "get")
|
||||
out, err := cmd.Output()
|
||||
|
|
7
go.mod
7
go.mod
|
@ -1,8 +1,11 @@
|
|||
module gitea.drjosh.dev/josh/lifepo4wered-exporter
|
||||
|
||||
go 1.23.0
|
||||
go 1.23.1
|
||||
|
||||
require github.com/prometheus/client_golang v1.20.2
|
||||
require (
|
||||
gitea.drjosh.dev/josh/jmetrics v0.0.1
|
||||
github.com/prometheus/client_golang v1.20.4
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
|
|
6
go.sum
6
go.sum
|
@ -1,3 +1,5 @@
|
|||
gitea.drjosh.dev/josh/jmetrics v0.0.1 h1:s79Ye9SEAXen17hXFWuTjAhwjvgYs4L1YW2Vs9mdaR0=
|
||||
gitea.drjosh.dev/josh/jmetrics v0.0.1/go.mod h1:2nQ3pf8j3kSfLIjRVWrKWuWGPjh/TKnu7MDx6PFb4BM=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
|
@ -10,8 +12,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
|
|||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/prometheus/client_golang v1.20.2 h1:5ctymQzZlyOON1666svgwn3s6IKWgfbjsejTMiXIyjg=
|
||||
github.com/prometheus/client_golang v1.20.2/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
|
||||
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI=
|
||||
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
|
||||
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
|
||||
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
|
||||
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
|
||||
|
|
Loading…
Reference in a new issue