influxdata / telegraf

Agent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.
https://influxdata.com/telegraf
MIT License
14.63k stars 5.58k forks source link

aggregators.derivative - Generate zero when not enough input measurements #12027

Open matthijskooijman opened 2 years ago

matthijskooijman commented 2 years ago

Use Case

I'm using the derivative plugin to calculate instantaneous power (in W) from the meter pulse count (in kWh).

The input plugin only produces a kWh measurement whenever the value increases (LED pulse on the meter seen). This means that when no power is being consumed, no measurements are produced.

Expected behavior

If there is not enough info to produce an aggregate (i.e. no or only one input measurements), the plugin should produce an aggregate with a zero rate.

This is probably not wanted for all usecases, so I suppose it should be controlled by a configuration option (disabled by default).

Actual behavior

When the derivative plugin has just one measurement to work with (e.g. rolled over from the previous period and no measurements in the current period), it outputs https://github.com/influxdata/telegraf/blob/5b48f5da8c4c9edaec7b2e2e5c10c1bef2b217a6/plugins/aggregators/derivative/derivative.go#L122 instead of producing an aggregate.

When the derivative has no measurements to work with (i.e. max_roll_over was exceeded and no new input measurements came in), it simply skips the metric entirely.

In both cases, no measurement is generated, meaning the last non-zero value that was produced will stay in the database as the most recent value.

Additional info

No response

sspaink commented 2 years ago

@matthijskooijman I think adding this option makes sense, putting it behind a configuration option would be best. Would you be able to open a pull request perhaps?

matthijskooijman commented 2 years ago

@sspaink I won't have time for a PR, I'm afraid.