The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/promlib/querydata/exemplar/labels.go

52 lines
1.2 KiB

package exemplar
import (
"sort"
"github.com/grafana/grafana-plugin-sdk-go/data"
)
var _ LabelTracker = (*labelTracker)(nil)
type LabelTracker interface {
Add(labels map[string]string)
AddFields(fields []*data.Field)
GetNames() []string
}
type labelTracker struct {
labelSet map[string]struct{}
}
func NewLabelTracker() LabelTracker {
return &labelTracker{
labelSet: map[string]struct{}{},
}
}
// Add saves label names that haven't been seen before
// so that they can be used to build the label fields in the exemplar frame
func (l *labelTracker) Add(labels map[string]string) {
for k := range labels {
l.labelSet[k] = struct{}{}
}
}
// AddFields saves field names so that they can be used to build the label fields in the exemplar frame.
func (l *labelTracker) AddFields(fields []*data.Field) {
for _, f := range fields {
l.labelSet[f.Name] = struct{}{}
}
}
// GetNames returns sorted unique label names
func (l *labelTracker) GetNames() []string {
labelNames := make([]string, 0, len(l.labelSet))
for k := range l.labelSet {
labelNames = append(labelNames, k)
}
sort.SliceStable(labelNames, func(i, j int) bool {
return labelNames[i] < labelNames[j]
})
return labelNames
}