Search code examples
prometheuspromql

Time Weighted Range Query in Prometheus


I want to compute performance indices for a recorded tracking error, which is a timeseries persisted in Prometheus. I'm interested in:

  • IAE: Integral Absolute Error
  • ISE: Integral Squared Error
  • ITAE: Integral Time Absolute Error
  • ITSE: Integral Time Squared Error

For clarification, they are calculated as follows:

enter image description here

IAE and ISE should be straightforward. In case of ITAE and ITSE "older" error values have reduced influence and fresh ones are emphasized.

I've already created two Prometheus rules:

rules:
- record: tracking_error:absolute
  expr: abs(tracking_error)
- record: tracking_error:squared
  expr: tracking_error*tracking_error

These enable me to query IAE and ISE:

# IAE
sum_over_time(tracking_error:absolute[1h])
# ISE
sum_over_time(tracking_error:squared[1h])

But I'm having difficulties designing queries for the time-weighted integrals ITSE and ITAE.

My current approach looks like this:

I create an additional set of rules persisting labelled timestamps:

rules: 
- record: tracking_error:timestamp
  expr:  time()
- record: tracking_error:timestamp:labelled
  expr: label_replace(tracking_error:timestamp,"errorName","someError", "","")

Which in turn enables writing queries like:

sum_over_time(
  (
    tracking_error:absolute * ignoring (endpoint,instance,job,namespace,pod,service) group_left (errorName) (tracking_error:timestamp:labelled-(tracking_error:timestamp:labelled offset 1m))
  )[1m:]
)

It feels very icky. Is this even correct and is there another, better approach? I hope somebody can help me out here.


Solution

  • Much better and not dependent on additional rules:

    sum_over_time(
      (
        tracking_error:absolute 
        * 
        (timestamp(tracking_error:absolute)-timestamp(tracking_error:absolute offset 1m))
    
      )[1m:]
    )