Skip to content

Commit

Permalink
Merge pull request #14 from utilitywarehouse/metrics-init-counters
Browse files Browse the repository at this point in the history
metrics: initialize counters with 0 value
  • Loading branch information
ribbybibby authored Apr 29, 2021
2 parents 0b2ef93 + 82f0e8f commit fd6c388
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
28 changes: 16 additions & 12 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,31 @@ var (
},
[]string{"type"},
)
syncQueueFullFailures = prometheus.NewCounterVec(
syncQueueFullFailures = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "semaphore_policy_sync_queue_full_failures",
Help: "Number of times a sync task was not added to the sync queue in time because the queue was full.",
},
[]string{"globalnetworkset"},
)
syncRequeue = prometheus.NewCounterVec(
syncRequeue = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "semaphore_policy_sync_requeue",
Help: "Number of attempts to requeue a sync.",
},
[]string{"globalnetworkset"},
)
)

func init() {
// Retrieving a Counter from a CounterVec will initialize it with a 0 value if it
// doesn't already have a value. This ensures that all possible counters
// start with a 0 value.
for _, t := range []string{"get", "list", "create", "update", "patch", "watch", "delete"} {
podWatcherFailures.With(prometheus.Labels{"type": t})
for _, s := range []string{"0", "1"} {
calicoClientRequest.With(prometheus.Labels{"type": t, "success": s})
}
}

prometheus.MustRegister(calicoClientRequest)
prometheus.MustRegister(podWatcherFailures)
prometheus.MustRegister(syncQueueFullFailures)
Expand All @@ -66,14 +74,10 @@ func IncPodWatcherFailures(t string) {
}).Inc()
}

func IncSyncQueueFullFailures(globalnetworkset string) {
syncQueueFullFailures.With(prometheus.Labels{
"globalnetworkset": globalnetworkset,
}).Inc()
func IncSyncQueueFullFailures() {
syncQueueFullFailures.Inc()
}

func IncSyncRequeue(globalnetworkset string) {
syncRequeue.With(prometheus.Labels{
"globalnetworkset": globalnetworkset,
}).Inc()
func IncSyncRequeue() {
syncRequeue.Inc()
}
4 changes: 2 additions & 2 deletions networksets.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (nss *NetworkSetStore) RunSyncLoop() {

func (nss *NetworkSetStore) requeue(id string) {
log.Logger.Debug("Requeueing sync task", "id", id)
metrics.IncSyncRequeue(id)
metrics.IncSyncRequeue()
go func() {
time.Sleep(1)
nss.enqueue(id)
Expand All @@ -170,7 +170,7 @@ func (nss *NetworkSetStore) enqueue(id string) {
log.Logger.Debug("Sync task queued", "id", id)
case <-time.After(5 * time.Second):
log.Logger.Error("Timed out trying to queue a sync action for netset, run queue is full", "id", id)
metrics.IncSyncQueueFullFailures(id)
metrics.IncSyncQueueFullFailures()
nss.requeue(id)
}
}
Expand Down

0 comments on commit fd6c388

Please sign in to comment.