From 1f01630e0e349dd08fa0b5e0761252ad4942ebb2 Mon Sep 17 00:00:00 2001 From: Jan Olszak Date: Thu, 26 Jan 2023 19:19:08 +0100 Subject: [PATCH] Generic implementation of monitoring errors #9 --- src/sync/store.go | 4 ++-- src/utils/monitor/keys.go | 14 ++++++++++++++ src/utils/monitor/monitor.go | 32 ++++++++++++++++++++++++-------- 3 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 src/utils/monitor/keys.go diff --git a/src/sync/store.go b/src/sync/store.go index 762ccc98..6a566282 100644 --- a/src/sync/store.go +++ b/src/sync/store.go @@ -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 } @@ -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 diff --git a/src/utils/monitor/keys.go b/src/utils/monitor/keys.go new file mode 100644 index 00000000..9764e601 --- /dev/null +++ b/src/utils/monitor/keys.go @@ -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" +) diff --git a/src/utils/monitor/monitor.go b/src/utils/monitor/monitor.go index b85f5ee1..e44b818d 100644 --- a/src/utils/monitor/monitor.go +++ b/src/utils/monitor/monitor.go @@ -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) { @@ -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 + } }