Skip to content

Commit

Permalink
pkg/services: add ver & sha params to HealthChecker (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored and samsondav committed Jan 11, 2024
1 parent 591aacb commit 10af017
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/loop/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *Server) start() error {
return fmt.Errorf("error starting prometheus server: %w", err)
}

s.checker = services.NewChecker()
s.checker = services.NewChecker("", "")
if err := s.checker.Start(); err != nil {
return fmt.Errorf("error starting health checker: %w", err)
}
Expand Down
27 changes: 19 additions & 8 deletions pkg/services/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type HealthChecker struct {
chStop chan struct{}
chDone chan struct{}

ver, sha string

servicesMu sync.RWMutex
services map[string]HealthReporter

Expand Down Expand Up @@ -70,8 +72,23 @@ var (
)
)

func NewChecker() *HealthChecker {
func NewChecker(ver, sha string) *HealthChecker {
if ver == "" || sha == "" {
if bi, ok := debug.ReadBuildInfo(); ok {
if ver == "" {
ver = bi.Main.Version
}
if sha == "" {
sha = bi.Main.Sum
}
}
}
if len(sha) > 7 {
sha = sha[:7]
}
return &HealthChecker{
ver: ver,
sha: sha,
services: make(map[string]HealthReporter, 10),
healthy: make(map[string]error, 10),
ready: make(map[string]error, 10),
Expand All @@ -82,13 +99,7 @@ func NewChecker() *HealthChecker {

func (c *HealthChecker) Start() error {
return c.StartOnce("HealthCheck", func() error {
if bi, ok := debug.ReadBuildInfo(); ok {
hash := bi.Main.Sum
if len(hash) > 7 {
hash = hash[:7]
}
version.WithLabelValues(bi.Main.Version, hash).Inc()
}
version.WithLabelValues(c.ver, c.sha).Inc()

// update immediately
c.update()
Expand Down

0 comments on commit 10af017

Please sign in to comment.