From d393b647ac82714ade4f1202ed5b74c119d9e1d2 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 12 Apr 2026 17:30:07 +0200 Subject: [PATCH] f-21: mon: add prometheus data Signed-off-by: GnomeZworc --- internal/prometheus/agent/collector.go | 67 ++++++++++++++++++++++++++ pkg/prometheus/server.go | 20 ++++++++ 2 files changed, 87 insertions(+) create mode 100644 internal/prometheus/agent/collector.go create mode 100644 pkg/prometheus/server.go diff --git a/internal/prometheus/agent/collector.go b/internal/prometheus/agent/collector.go new file mode 100644 index 0000000..04712ad --- /dev/null +++ b/internal/prometheus/agent/collector.go @@ -0,0 +1,67 @@ +package agentmetrics + +import ( + "strings" + + "git.g3e.fr/syonad/two/pkg/db/kv" + "github.com/dgraph-io/badger/v4" + "github.com/prometheus/client_golang/prometheus" +) + +var allStates = []string{"creating", "created", "deleting", "deleted"} + +// AgentCollector implements prometheus.Collector and exposes agent metrics +// by querying the BadgerDB on each scrape. +type AgentCollector struct { + db *badger.DB + vpcsTotal *prometheus.Desc + subnetsTotal *prometheus.Desc +} + +func NewAgentCollector(db *badger.DB) *AgentCollector { + return &AgentCollector{ + db: db, + vpcsTotal: prometheus.NewDesc( + "syonad_vpcs_total", + "Number of VPCs by state.", + []string{"state"}, nil, + ), + subnetsTotal: prometheus.NewDesc( + "syonad_subnets_total", + "Number of subnets by state.", + []string{"state"}, nil, + ), + } +} + +func (c *AgentCollector) Describe(ch chan<- *prometheus.Desc) { + ch <- c.vpcsTotal + ch <- c.subnetsTotal +} + +func (c *AgentCollector) Collect(ch chan<- prometheus.Metric) { + c.collectStates(ch, "vpc/", c.vpcsTotal) + c.collectStates(ch, "subnet/", c.subnetsTotal) +} + +// collectStates counts resources under the given DB prefix by their state value +// and emits one gauge per state label. +func (c *AgentCollector) collectStates(ch chan<- prometheus.Metric, prefix string, desc *prometheus.Desc) { + counts := make(map[string]float64, len(allStates)) + for _, s := range allStates { + counts[s] = 0 + } + + items, err := kv.ListByPrefix(c.db, prefix) + if err == nil { + for key, val := range items { + if strings.HasSuffix(key, "/state") { + counts[val]++ + } + } + } + + for _, state := range allStates { + ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, counts[state], state) + } +} diff --git a/pkg/prometheus/server.go b/pkg/prometheus/server.go new file mode 100644 index 0000000..9f8e557 --- /dev/null +++ b/pkg/prometheus/server.go @@ -0,0 +1,20 @@ +package promserver + +import ( + "log" + "net/http" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +// Start launches the Prometheus metrics HTTP server on the given address. +// The provided registry is used to expose metrics at /metrics. +func Start(address string, registry *prometheus.Registry) { + mux := http.NewServeMux() + mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{ + EnableOpenMetrics: true, + })) + log.Printf("Prometheus server listening on %s", address) + log.Fatal(http.ListenAndServe(address, mux)) +}