How to filter out input from Prometheus for a Grafana bar chart

Viewed 21

I want to count the occurrences of some strings. I'm using a map in Go, e.g. https://go.dev/play/p/gc9f3PTM280

package main

import "fmt"

var repetitions map[string]int
var inputStrings = []string{"value1", "value2", "value1"}

func main() {
    repetitions = make(map[string]int)
    for _, stringValue := range inputStrings {
        repetitions[stringValue]++
    }
    for value, count := range repetitions {
        fmt.Printf("%s found %d time(s)\n", value, count)
    }
}

To visualize the results, I'm exporting the metrics to Prometheus and Grafana. I'm using a Prometheus CounterVec to count the different strings

var stringsMetrics = prometheus.NewCounterVec(prometheus.CounterOpts{
    ...
    ...
    stringsMetrics.With(prometheus.Labels{"myLabel": stringValue}).Inc() 

I created a Grafana panel with a bar chart and Prometheus as input showing how many times the strings are found.

However, there are about 800 different strings (not just the sample program above), so the chart gets quite clogged. Grafana bar diagram

I'd like to see in the bar chart only the top entries, that is, only information for the 10 strings most repeated. I've been trying to find a way to only show the most repeated in Grafana, something like the 'max', but not just 1 value, a number of values.

Is this doable? Can the Grafana panel be configured to dynamically present only some of the input, and not all of them?

0 Answers
Related