diff --git a/lib/main.go b/lib/main.go index 8a9f7124..a64c1133 100644 --- a/lib/main.go +++ b/lib/main.go @@ -269,6 +269,10 @@ func (obj *Main) Run() error { if err := prom.Start(); err != nil { return errwrap.Wrapf(err, "can't start initiate Prometheus instance") } + + if err := prom.InitKindMetrics(resources.RegisteredResourcesNames()); err != nil { + return errwrap.Wrapf(err, "can't initialize kind-specific prometheus metrics") + } } if !obj.NoPgp { diff --git a/prometheus/prometheus.go b/prometheus/prometheus.go index 9f203e36..905af280 100644 --- a/prometheus/prometheus.go +++ b/prometheus/prometheus.go @@ -149,6 +149,31 @@ func (obj *Prometheus) Stop() error { return nil } +// InitKindMetrics initialized prometheus counters. For each kind of +// resource checkApply counters are initialized with all the possible value. +func (obj *Prometheus) InitKindMetrics(kinds []string) error { + if obj == nil { + return nil // happens when mgmt is launched without --prometheus + } + bools := []bool{true, false} + for _, kind := range kinds { + for _, apply := range bools { + for _, eventful := range bools { + for _, errorful := range bools { + labels := prometheus.Labels{ + "kind": kind, + "apply": strconv.FormatBool(apply), + "eventful": strconv.FormatBool(eventful), + "errorful": strconv.FormatBool(errorful), + } + obj.checkApplyTotal.With(labels) + } + } + } + } + return nil +} + // UpdateCheckApplyTotal refreshes the failing gauge by parsing the internal // state map. func (obj *Prometheus) UpdateCheckApplyTotal(kind string, apply, eventful, errorful bool) error { diff --git a/resources/resources.go b/resources/resources.go index b6f12c4e..c317d435 100644 --- a/resources/resources.go +++ b/resources/resources.go @@ -52,6 +52,15 @@ func RegisterResource(kind string, fn func() Res) { registeredResources[kind] = fn } +// RegisteredResourcesNames returns the kind of the registered resources. +func RegisteredResourcesNames() []string { + kinds := []string{} + for k := range registeredResources { + kinds = append(kinds, k) + } + return kinds +} + // NewResource returns an empty resource object from a registered kind. It // errors if the resource kind doesn't exist. func NewResource(kind string) (Res, error) { diff --git a/resources/resources_test.go b/resources/resources_test.go index 14812112..6ab07b7b 100644 --- a/resources/resources_test.go +++ b/resources/resources_test.go @@ -78,6 +78,18 @@ func TestIFF(t *testing.T) { } } +func TestRegisteredResourcesNames(t *testing.T) { + kinds := RegisteredResourcesNames() + for _, kind := range kinds { + if kind == "" { + t.Error("Empty kind found") + } + } + if len(kinds) == 0 { + t.Error("No registered resources") + } +} + func TestReadEvent(t *testing.T) { //res := FileRes{}