Skip to content

Commit

Permalink
prometheus: Initialize mgmt_checkapply_total metrics
Browse files Browse the repository at this point in the history
It is recommended by Prometheus to initialize metrics:

https://prometheus.io/docs/practices/instrumentation/#avoid-missing-metrics

This commits initialize the mgmt_checkapply_total metric
for each registered resource.

Signed-off-by: Julien Pivotto <[email protected]>
  • Loading branch information
roidelapluie committed Nov 23, 2017
1 parent bfc2549 commit fdce9d6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions resources/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down

0 comments on commit fdce9d6

Please sign in to comment.