-
Notifications
You must be signed in to change notification settings - Fork 455
/
labels.go
40 lines (32 loc) · 1006 Bytes
/
labels.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/util/labels.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package util
import (
"strings"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
)
// LabelsToMetric converts a Labels to Metric
// Don't do this on any performance sensitive paths.
func LabelsToMetric(ls labels.Labels) model.Metric {
m := make(model.Metric, ls.Len())
ls.Range(func(l labels.Label) {
m[model.LabelName(l.Name)] = model.LabelValue(l.Value)
})
return m
}
// LabelMatchersToString returns a string representing the input label matchers.
func LabelMatchersToString(matchers []*labels.Matcher) string {
out := strings.Builder{}
out.WriteRune('{')
for idx, m := range matchers {
if idx > 0 {
out.WriteRune(',')
}
out.WriteString(m.String())
}
out.WriteRune('}')
return out.String()
}