f-37: periph: add final touch #37
All checks were successful
Pre Release Workflow / set-release-target (push) Successful in 1s
Pre Release Workflow / build (agent, amd64, linux) (push) Successful in 1m35s
Pre Release Workflow / build (metadata, amd64, linux) (push) Successful in 1m31s
Pre Release Workflow / upload-scripts (run-dnsmasq-in-netns.sh) (push) Successful in 7s
Pre Release Workflow / prerelease (push) Successful in 10s
All checks were successful
Pre Release Workflow / set-release-target (push) Successful in 1s
Pre Release Workflow / build (agent, amd64, linux) (push) Successful in 1m35s
Pre Release Workflow / build (metadata, amd64, linux) (push) Successful in 1m31s
Pre Release Workflow / upload-scripts (run-dnsmasq-in-netns.sh) (push) Successful in 7s
Pre Release Workflow / prerelease (push) Successful in 10s
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
fef04b10fc
commit
2150530ef2
5 changed files with 176 additions and 15 deletions
|
|
@ -3,12 +3,13 @@ package agentmetrics
|
|||
import (
|
||||
"strings"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"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"}
|
||||
var allStates = state.All()
|
||||
|
||||
// AgentCollector implements prometheus.Collector and exposes agent metrics
|
||||
// by querying the BadgerDB on each scrape.
|
||||
|
|
@ -47,7 +48,7 @@ func (c *AgentCollector) Collect(ch chan<- prometheus.Metric) {
|
|||
// 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))
|
||||
counts := make(map[state.State]float64, len(allStates))
|
||||
for _, s := range allStates {
|
||||
counts[s] = 0
|
||||
}
|
||||
|
|
@ -55,13 +56,18 @@ func (c *AgentCollector) collectStates(ch chan<- prometheus.Metric, prefix strin
|
|||
items, err := kv.ListByPrefix(c.db, prefix)
|
||||
if err == nil {
|
||||
for key, val := range items {
|
||||
if strings.HasSuffix(key, "/state") {
|
||||
counts[val]++
|
||||
if !strings.HasSuffix(key, "/state") {
|
||||
continue
|
||||
}
|
||||
// Une valeur hors enum n'est pas comptée : la migration au
|
||||
// démarrage les a toutes ramenées dans l'enum.
|
||||
if s, err := state.Parse(val); err == nil {
|
||||
counts[s]++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, state := range allStates {
|
||||
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, counts[state], state)
|
||||
for _, s := range allStates {
|
||||
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, counts[s], string(s))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
126
internal/prometheus/agent/collector_test.go
Normal file
126
internal/prometheus/agent/collector_test.go
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
package agentmetrics
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
)
|
||||
|
||||
func newTestDB(t *testing.T) *badger.DB {
|
||||
t.Helper()
|
||||
db := kv.InitDB(kv.Config{Path: t.TempDir()}, false)
|
||||
t.Cleanup(func() { db.Close() })
|
||||
return db
|
||||
}
|
||||
|
||||
// collect exécute une collecte et retourne, par nom de métrique, la valeur de
|
||||
// chaque série indexée par son label state.
|
||||
func collect(t *testing.T, c *AgentCollector) map[string]map[string]float64 {
|
||||
t.Helper()
|
||||
ch := make(chan prometheus.Metric, 64)
|
||||
c.Collect(ch)
|
||||
close(ch)
|
||||
|
||||
out := map[string]map[string]float64{}
|
||||
for m := range ch {
|
||||
var pb dto.Metric
|
||||
if err := m.Write(&pb); err != nil {
|
||||
t.Fatalf("write metric: %v", err)
|
||||
}
|
||||
// Le fqName n'est pas exposé directement : il figure dans la
|
||||
// représentation textuelle du Desc.
|
||||
desc := m.Desc().String()
|
||||
var name string
|
||||
switch {
|
||||
case strings.Contains(desc, "syonad_vpcs_total"):
|
||||
name = "vpcs"
|
||||
case strings.Contains(desc, "syonad_subnets_total"):
|
||||
name = "subnets"
|
||||
default:
|
||||
t.Fatalf("métrique inattendue : %s", desc)
|
||||
}
|
||||
if out[name] == nil {
|
||||
out[name] = map[string]float64{}
|
||||
}
|
||||
for _, l := range pb.GetLabel() {
|
||||
if l.GetName() == "state" {
|
||||
out[name][l.GetValue()] = pb.GetGauge().GetValue()
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestCollector_CountsByState(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
for resource, s := range map[string]state.State{
|
||||
"vpc/vpc-1": state.Running,
|
||||
"vpc/vpc-2": state.Running,
|
||||
"vpc/vpc-3": state.Error,
|
||||
"subnet/sn-1": state.Creating,
|
||||
} {
|
||||
if err := state.Set(db, resource, s); err != nil {
|
||||
t.Fatalf("préparation du test : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
got := collect(t, NewAgentCollector(db))
|
||||
wantVPC := map[string]float64{"creating": 0, "running": 2, "error": 1, "deleting": 0, "deleted": 0}
|
||||
for s, want := range wantVPC {
|
||||
if got["vpcs"][s] != want {
|
||||
t.Errorf("vpcs{state=%q} = %v, attendu %v", s, got["vpcs"][s], want)
|
||||
}
|
||||
}
|
||||
if got["subnets"]["creating"] != 1 {
|
||||
t.Errorf("subnets{state=creating} = %v, attendu 1", got["subnets"]["creating"])
|
||||
}
|
||||
}
|
||||
|
||||
// Une série par état doit être émise même à zéro : une métrique qui disparaît
|
||||
// côté Prometheus casse les alertes qui s'en servent.
|
||||
func TestCollector_EmitsEveryState(t *testing.T) {
|
||||
got := collect(t, NewAgentCollector(newTestDB(t)))
|
||||
for _, family := range []string{"vpcs", "subnets"} {
|
||||
if len(got[family]) != len(state.All()) {
|
||||
t.Errorf("%s : %d séries, attendu %d", family, len(got[family]), len(state.All()))
|
||||
}
|
||||
for _, s := range state.All() {
|
||||
if _, ok := got[family][string(s)]; !ok {
|
||||
t.Errorf("%s : série manquante pour l'état %q", family, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_IgnoresNonStateKeys(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
if err := state.Set(db, "vpc/vpc-1", state.Running); err != nil {
|
||||
t.Fatalf("préparation du test : %v", err)
|
||||
}
|
||||
kv.AddInDB(db, "vpc/vpc-1/cidr", "10.0.0.0/16")
|
||||
|
||||
got := collect(t, NewAgentCollector(db))
|
||||
if got["vpcs"]["running"] != 1 {
|
||||
t.Errorf("vpcs{state=running} = %v, attendu 1", got["vpcs"]["running"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_Describe(t *testing.T) {
|
||||
ch := make(chan *prometheus.Desc, 10)
|
||||
NewAgentCollector(newTestDB(t)).Describe(ch)
|
||||
close(ch)
|
||||
|
||||
var n int
|
||||
for range ch {
|
||||
n++
|
||||
}
|
||||
if n != 2 {
|
||||
t.Errorf("%d descripteurs, attendu 2", n)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue