Skip to content

Commit

Permalink
MeasurementCounterCollector.Set check if current value is lower
Browse files Browse the repository at this point in the history
than previous value, if set. if the current value is lower than we skip that value
  • Loading branch information
SchumacherFM committed Jul 7, 2024
1 parent 8ef568b commit a8d9193
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
15 changes: 13 additions & 2 deletions prometheus/measurement_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ func (c *MeasurementCounterCollector) Collect(ch chan<- prometheus.Metric) {
}
}

// Set sets the specified value for provided labelValues at a specified timestamp.
// value must be higher than 0. Otherwise, en error will be logged.
// Set sets the specified value for provided labelValues at a specified
// timestamp. value must be higher than 0 and higher than the previous value.
// Otherwise, en error will be logged.
//
// This function is thread-safe.
func (c *MeasurementCounterCollector) Set(timestamp time.Time, value float64, labelValues ...string) {
Expand All @@ -84,6 +85,16 @@ func (c *MeasurementCounterCollector) Set(timestamp time.Time, value float64, la
defer c.values.Unlock()

lvs := strings.Join(labelValues, keySeparator)
var prevValue float64
if prev, ok := c.values.data[lvs]; ok && prev.value > 0 {
prevValue = prev.value
}

if prevValue > 0 && value < prevValue {
log.Println("[WARN] counters cannot decrease in its value. ignoring.", c.fqName, "value", value, "prevValue", prevValue)
return
}

c.values.data[lvs] = &measurementResult{
value: value,
timestamp: timestamp,
Expand Down
1 change: 0 additions & 1 deletion server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ func (h *Handler) queryDevice(
status.Requests++

measurements, err := dev.Query(h.Manager.Conn.ModbusClient())

if err == nil {
// send ok status
status.Available(true)
Expand Down

0 comments on commit a8d9193

Please sign in to comment.