How to use Arithmetic Binary Operators for raw values in Loki's Log record?

How can I use Arithmetic Binary Operators in raw Loki row data?

For example, I have row with JSON {"total_duration_sec": 123, "total_items": 41} and need to build graph by “total_duration_sec / total_items” formula.

I can build query like this:

avg_over_time({job="my-job"} | json | total_duration_sec [$__interval])
/
avg_over_time({job="my-job"} | json | total_items [$__interval])

But it will give me wrong results, because of averaging the value in each interval, so we will receive the avg(total_duration_sec) / avg(total_items), instead of avg(total_duration_sec / total_items)!

So I need to have something like this:

avg_over_time( 
  ({job="my-job"} | json | total_duration_sec)
  /
  ({job="my-job"} | json | total_items)
[$__interval])

But this isn’t work.

Can anyone give me a working example, that show me exact calculated values?

Bumping this.

Did you find a solution? I have the same use case and struggle to construct a query that allows me to do that.

Same problem here. Any solution? It seems a very usual use case…

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.