Fix: Only compute rates with a valid previous state

This commit is contained in:
Holger Obermaier 2022-03-08 09:29:35 +01:00
parent 556ca4a890
commit f8fdc4b377

View File

@ -53,7 +53,6 @@ func (m *InfinibandCollector) Init(config json.RawMessage) error {
"source": m.name, "source": m.name,
"group": "Network", "group": "Network",
} }
m.lastTimestamp = time.Now()
// Set default configuration, // Set default configuration,
m.config.SendAbsoluteValues = true m.config.SendAbsoluteValues = true
@ -123,7 +122,7 @@ func (m *InfinibandCollector) Init(config json.RawMessage) error {
// Initialize last state // Initialize last state
lastState := make(map[string]int64) lastState := make(map[string]int64)
for counter := range portCounterFiles { for counter := range portCounterFiles {
lastState[counter] = 0 lastState[counter] = -1
} }
m.info = append(m.info, m.info = append(m.info,
@ -194,9 +193,11 @@ func (m *InfinibandCollector) Read(interval time.Duration, output chan lp.CCMetr
// Send derived values // Send derived values
if m.config.SendDerivedValues { if m.config.SendDerivedValues {
rate := float64((v - info.lastState[counterName])) / timeDiff if info.lastState[counterName] >= 0 {
if y, err := lp.New(counterName+"_bw", info.tagSet, m.meta, map[string]interface{}{"value": rate}, now); err == nil { rate := float64((v - info.lastState[counterName])) / timeDiff
output <- y if y, err := lp.New(counterName+"_bw", info.tagSet, m.meta, map[string]interface{}{"value": rate}, now); err == nil {
output <- y
}
} }
// Save current state // Save current state
info.lastState[counterName] = v info.lastState[counterName] = v