Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expressions #143

Merged
merged 3 commits into from
May 19, 2024
Merged

Expressions #143

merged 3 commits into from
May 19, 2024

Conversation

chrschn
Copy link
Contributor

@chrschn chrschn commented Dec 19, 2023

Support metrics from arithmetic expressions

This change adds the new metric config option expression which can define a complex expression to calculate the final metric value. The expression is evaluated using github.com/antonmedv/expr as the engine, see expr-lang.org/docs/v1.9/Language-Definition for a language overview. In addition, some basic functions are available during evaluation: now, int, float, round, ceil, floor, abs, min, max.

For each evaluation, the expression gets:

  • the current MQTT value
  • the last MQTT value
  • the result of the last evaluation
  • the elapsed time since the last evaluation

Expressions allow more flexibility to derive metrics from sensor inputs which are very hard or even impossible to do in Prometheus, for example, integrate continuous values over time, filter extreme or zero values, or non-linear scaling. The last value, result, and timestamp are preserved even between restarts of mqtt2prometheus. This means that expressions can be used to produce continuous time series over a long time range.

To make upstream integration easier, the code depends on the rather old v1.8.9 of github.com/antonmedv/expr, which is available in the main repo of all supported Debian versions.

In addition, this PR adds the ability to support more than one configuration for each sensor value. Expressions are more flexible than aggregation rules in Prometheus, so it makes sense to derive multiple metrics from the same sensor input.

@hikhvar
Copy link
Owner

hikhvar commented Jan 13, 2024

While this is quite a bit of feature bloat, I like this. However, I have to read this in depth to understand all of the code. Please bear with me.

To make upstream integration easier, the code depends on the rather old v1.8.9 of github.com/antonmedv/expr, which is available in the main repo of all supported Debian versions.

most probably this will be bumped by the automation in this repository, as I regularly update the dependencies to the latest available packages. This lowers the burden in the long run for me, as I don't have to jump many versions in case of a urgent security issue or bug in a dependency.

@chrschn chrschn force-pushed the expressions branch 2 times, most recently from baa7b27 to bc87d2a Compare February 5, 2024 21:23
@chrschn
Copy link
Contributor Author

chrschn commented Feb 5, 2024

I fixed the code for Go 1.14, all tests should pass now. I'm looking forward to your review.

Christian Schneider added 3 commits March 25, 2024 10:15
This change adds the new metric config option `expression` which can
define a complex expression to calculate the final metric value. The
expression is evaluated using github.com/antonmedv/expr as the engine,
see expr-lang.org/docs/v1.9/Language-Definition for a language overview.
In addition, some basic functions are available during evaluation: now,
int, float, round, ceil, floor, abs, min, max.

For each evaluation, the expression gets:
- the current MQTT value
- the last MQTT value
- the result of the last evaluation
- the elapsed time since the last evaluation

Expressions allow more flexibility to derive metrics from sensor inputs
which are very hard or even impossible to do in Prometheus, for example,
integrate continuous values over time, filter extreme or zero values, or
non-linear scaling. The last value, result, and timestamp are preserved
even between restarts of mqtt2prometheus. This means that expressions
can be used to produce continuous time series over a long time range.

To make upstream integration easier, the code depends on the rather old
v1.8.9 of github.com/antonmedv/expr, which is available in the main repo
of all supported Debian versions.
When generating metric values using expression evaluation, there are new
use-cases for more than one configuration for each sensor value.
Expressions are more flexible than aggregation rules in Prometheus, so
it makes sense to derive multiple metrics from the same sensor input.
@chrschn
Copy link
Contributor Author

chrschn commented Mar 25, 2024

Hi Christoph. Thanks for merging my PR #141. I rebased this PR on top of the current master. PTAL when you find the time.

@hikhvar hikhvar merged commit 0137ffc into hikhvar:master May 19, 2024
13 checks passed
@hikhvar
Copy link
Owner

hikhvar commented May 19, 2024

Thank you for the contributions and patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants