prometheuspromql

How to filter a time series after applying aggregation in PromQL?


I have a promQL expression like this- sum(metric_1) by (label_1). Now, I want to filter out the output of the expression using another label label_2. Something like this- (sum(metric_1) by (label_1, label_2)){label_2="some_value"}. But, this is not working and is giving a parse error. How can I achieve this?


Solution

  • Prometheus doesn't support label-based post-filtering of query results, so all the label-based filtering must be performed at label filters in curly braces before applying any PromQL function. For example, if the sum of metric_1 time series with the label_2="value_2" label must be calculated, then the following query can be used:

    sum(metric_1{label_2="value_2"}) by (label_1)
    

    P.S. If additional post-filtering on labels is needed after applying some PromQL function such as label_replace, then the following MetricsQL functions can be used at VictoriaMetrics - Prometheus-like monitoring solution with PromQL support:

    (I'm the core developer of VictoriaMetrics).