-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrowkey.go
55 lines (49 loc) · 1.29 KB
/
rowkey.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package promtable
import (
"errors"
"strings"
"github.com/uschen/promtable/prompb"
)
// LabelsToRowKeyComponents - labels must be sorted
func LabelsToRowKeyComponents(labels []prompb.Label) (name, labelsString string, err error) {
if len(labels) == 0 || labels[0].Name != MetricNameLabel {
err = errors.New("no metric name in labels")
return
}
name = labels[0].Value
var lsb strings.Builder
// write label.Name first
for i := 1; i < len(labels); i++ {
lsb.WriteString(labels[i].Name)
lsb.WriteRune(',')
}
// write label.Value
for i := 1; i < len(labels)-1; i++ {
lsb.WriteString(EscapeLabelValue(labels[i].Value))
lsb.WriteRune(',')
}
lsb.WriteString(EscapeLabelValue(labels[len(labels)-1].Value)) // write the last one without append ','
return name, lsb.String(), nil
}
// LabelsFromString -
func LabelsFromString(labelsString string) ([]prompb.Label, error) {
if labelsString == "" {
return nil, nil
}
parts := strings.Split(labelsString, ",")
var (
n = len(parts)
n12 = n / 2
)
if n%2 != 0 {
return nil, errors.New("invalid labelsString received '" + labelsString + "'")
}
var labels = make([]prompb.Label, n12)
for i := 0; i < n12; i++ {
labels[i] = prompb.Label{
Name: parts[i],
Value: UnescapeLabelValue(parts[i+n12]),
}
}
return labels, nil
}