mirror of
https://github.com/jimeh/macos-battery-exporter.git
synced 2026-02-19 01:16:40 +00:00
The `*_amps` and `*_watts` metrics now use Ah and Wh values, instead of mAh and mWh as they were before.
114 lines
2.0 KiB
Go
114 lines
2.0 KiB
Go
//go:build darwin
|
|
|
|
package prom
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
const DefaultNamespace = "macos"
|
|
|
|
type Registry interface {
|
|
prometheus.Registerer
|
|
prometheus.Gatherer
|
|
}
|
|
|
|
type ServerOptions struct {
|
|
Bind string
|
|
Port int
|
|
ReadTimeout time.Duration
|
|
WriteTimeout time.Duration
|
|
IdleTimeout time.Duration
|
|
Logger *slog.Logger
|
|
}
|
|
|
|
type Server struct {
|
|
*http.Server
|
|
registry Registry
|
|
mux *http.ServeMux
|
|
}
|
|
|
|
func NewServer(registry *prometheus.Registry, options ServerOptions) *Server {
|
|
if options.Bind == "" {
|
|
options.Bind = "127.0.0.1"
|
|
}
|
|
if options.Port == 0 {
|
|
options.Port = 9108
|
|
}
|
|
if options.ReadTimeout == 0 {
|
|
options.ReadTimeout = 5 * time.Second
|
|
}
|
|
if options.WriteTimeout == 0 {
|
|
options.WriteTimeout = 10 * time.Second
|
|
}
|
|
if options.IdleTimeout == 0 {
|
|
options.IdleTimeout = 30 * time.Second
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
mux.Handle(
|
|
"/metrics",
|
|
promhttp.HandlerFor(registry, promhttp.HandlerOpts{}),
|
|
)
|
|
|
|
return &Server{
|
|
mux: mux,
|
|
registry: registry,
|
|
Server: &http.Server{
|
|
Addr: fmt.Sprintf("%s:%d", options.Bind, options.Port),
|
|
ReadTimeout: options.ReadTimeout,
|
|
WriteTimeout: options.WriteTimeout,
|
|
IdleTimeout: options.IdleTimeout,
|
|
Handler: mux,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (s *Server) Register(
|
|
collectors ...prometheus.Collector,
|
|
) error {
|
|
for _, c := range collectors {
|
|
err := s.registry.Register(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) ListenAndServe() error {
|
|
slog.Info(
|
|
"starting prometheus server",
|
|
slog.String("addr", s.Addr),
|
|
)
|
|
|
|
return s.Server.ListenAndServe()
|
|
}
|
|
|
|
func RunServer(
|
|
namespace string,
|
|
registry *prometheus.Registry,
|
|
options ServerOptions,
|
|
) error {
|
|
if namespace == "" {
|
|
namespace = DefaultNamespace
|
|
}
|
|
|
|
s := NewServer(registry, options)
|
|
|
|
collector := NewCollector(namespace)
|
|
err := s.Register(collector)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return s.ListenAndServe()
|
|
}
|