grafanagrafana-loki

Round label values in Grafana visualization chart


Given the following grafana logs visualization script:

sum by(score) (
  count_over_time(
  {container_name="dal-api"}
  |= update_score_day5
  | logfmt
  | json score="score_service"
  [1d])
) 

I want to round scores, currently i have too many visualization bars because the results can be 1.3 1.5 2.4 etc


Solution

  • This can be achieved with Template functions:

    sum by(score) (
     count_over_time(
      {container_name="dal-api"}
      |= update_score_day5
      | logfmt
      | json
      | score=`{{round .score_service 0}}`
      [1d])
    ) 
    

    Here field score_service will be preserved as-is (obviously only inside of aggregation), and new field score will be added with rounded value. Instead of round, you can also use ceil or floor

    Demo of similar query can be seen here.