Skip to content

Commit

Permalink
services: Fix metric from not publishing
Browse files Browse the repository at this point in the history
When the metric variable is defined as a global variable (within the
`var` scope at the package level), then it will be instantiated as a
NoOp metric. Once the metrics package is initialized, then all the
metrics variables will transition from NoOp metrics to a real metric
type. This problem occurred because the global variables instantiation
happened before the metrics package initialization.

This commit fixes it by using the metrics variable after the metrics
package has been initialized. We can assume it's been initialized when
the code executed is production ("live") code.

Fixes: #26511
Fixes: 978b27c ("Metrics: Add services metrics")
Signed-off-by: Chris Tarazi <chris@isovalent.com>
  • Loading branch information
christarazi authored and dylandreimerink committed Jul 21, 2023
1 parent d29f101 commit b86dab8
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ import (

const anyPort = "*"

var (
updateMetric = metrics.ServicesEventsCount.WithLabelValues("update")
deleteMetric = metrics.ServicesEventsCount.WithLabelValues("delete")
addMetric = metrics.ServicesEventsCount.WithLabelValues("add")
)

// ErrLocalRedirectServiceExists represents an error when a Local redirect
// service exists with the same Frontend.
type ErrLocalRedirectServiceExists struct {
Expand Down Expand Up @@ -719,9 +713,9 @@ func (s *Service) upsertService(params *lb.SVC) (bool, lb.ID, error) {
}

if new {
addMetric.Inc()
metrics.ServicesEventsCount.WithLabelValues("add").Inc()
} else {
updateMetric.Inc()
metrics.ServicesEventsCount.WithLabelValues("update").Inc()
}

s.notifyMonitorServiceUpsert(svc.frontend, svc.backends,
Expand Down Expand Up @@ -1617,7 +1611,7 @@ func (s *Service) deleteServiceLocked(svc *svcInfo) error {
s.healthServer.DeleteService(lb.ID(svc.frontend.ID))
}

deleteMetric.Inc()
metrics.ServicesEventsCount.WithLabelValues("delete").Inc()
s.notifyMonitorServiceDelete(svc.frontend.ID)

return nil
Expand Down

0 comments on commit b86dab8

Please sign in to comment.