Skip to content

Commit

Permalink
fix(monitoring): throttle service status updates
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbitfly committed Sep 30, 2024
1 parent c22233e commit f09f1ce
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions services/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ package services
import (
"encoding/json"
"os"
"time"

"github.com/gobitfly/eth2-beaconchain-explorer/db"
"github.com/gobitfly/eth2-beaconchain-explorer/utils"
"github.com/gobitfly/eth2-beaconchain-explorer/version"
)

// Report the status of a particular service, will add current Pid and executable name
// Throttle calls to 1/min for each service name so that we don't report too often
var lastStatusUpdate = make(map[string]time.Time)

func ReportStatus(name, status string, metadata *json.RawMessage) {
if !utils.Config.ReportServiceStatus {
return
}

if lastUpdate, ok := lastStatusUpdate[name]; ok {
if time.Since(lastUpdate) < time.Minute {
return
}
}
pid := os.Getpid()
execName, err := os.Executable()
if err != nil {
Expand All @@ -33,4 +43,5 @@ func ReportStatus(name, status string, metadata *json.RawMessage) {
if err != nil {
utils.LogError(err, "error reporting service status", 0, map[string]interface{}{"name": name, "status": status})
}
lastStatusUpdate[name] = time.Now()
}

0 comments on commit f09f1ce

Please sign in to comment.