Skip to content

Commit

Permalink
Generic implementation of monitoring errors #9
Browse files Browse the repository at this point in the history
  • Loading branch information
janekolszak committed Jan 26, 2023
1 parent 198f8c5 commit 91b5065
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/sync/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (self *Store) insert(pendingInteractions []*model.Interaction, lastTransact
err = self.setLastTransactionBlockHeight(self.Ctx, tx, lastTransactionBlockHeight)
if err != nil {
self.log.WithError(err).Error("Failed to update last transaction block height")
self.monitor.ReportDBError()
self.monitor.Increment(monitor.Kind(monitor.DbLastTransactionBlockHeightError))
return err
}

Expand All @@ -134,7 +134,7 @@ func (self *Store) insert(pendingInteractions []*model.Interaction, lastTransact
if err != nil {
self.log.WithError(err).Error("Failed to insert Interactions")
self.log.WithField("interactions", pendingInteractions).Debug("Failed interactions")
self.monitor.ReportDBError()
self.monitor.Increment(monitor.Kind(monitor.DbInteraction))
return err
}
return nil
Expand Down
14 changes: 14 additions & 0 deletions src/utils/monitor/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package monitor

type Kind string

var (
DbInteraction = "db_interaction"
DbLastTransactionBlockHeightError = "db_last_tx_block_height"
TxValidationErrors = "tx_validation"
TxDownloadErrors = "tx_download"
BlockValidationErrors = "block_validation"
BlockDownloadErrors = "block_download"
PeerDownloadErrors = "peer_download"
NetworkInfoDownloadErrors = "network_info_download"
)
32 changes: 24 additions & 8 deletions src/utils/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,34 @@ type Monitor struct {
log *logrus.Entry
mtx sync.Mutex

report Report
report map[Kind]int

previousReport map[Kind]int
}

type Report struct {
DbErrors int `json:"db"`
DbErrors int `json:"db"`
TxValidationErrors int `json:"tx_validation"`
TxDownloadErrors int `json:"tx_download"`
BlockValidationErrors int `json:"block_validation"`
BlockDownloadErrors int `json:"block_download"`
PeerDownloadErrors int `json:"peer_download"`
NetworkInfoDownloadErrors int `json:"network_info_download"`
}

func NewMonitor() (self *Monitor) {
self = new(Monitor)
self.log = logger.NewSublogger("monitor")
self.report = make(map[Kind]int)
self.previousReport = make(map[Kind]int)
return
}

func (self *Monitor) ReportDBError() {
func (self *Monitor) Increment(kind Kind) {
self.mtx.Lock()
defer self.mtx.Unlock()

self.report.DbErrors += 1
self.report[kind] = self.report[kind] + 1
}

func (self *Monitor) OnGet(c *gin.Context) {
Expand All @@ -41,11 +51,17 @@ func (self *Monitor) OnGet(c *gin.Context) {
self.log.WithField("report", self.report).Info("Getting monitor stats")

status := http.StatusOK
if self.report.DbErrors != 0 {
status = http.StatusInternalServerError
for k, v := range self.report {
if self.previousReport[k] != v {
status = http.StatusInternalServerError
break
}
}

c.JSON(status, &self.report)

// Reset counters
self.report.DbErrors = 0
// Copy the report
for k, v := range self.report {
self.previousReport[k] = v
}
}

0 comments on commit 91b5065

Please sign in to comment.