-
Notifications
You must be signed in to change notification settings - Fork 25
/
read.go
55 lines (48 loc) · 1.74 KB
/
read.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 paths
import (
"fmt"
"sort"
"strings"
graphite_tmpl "github.com/criteo/graphite-remote-adapter/client/graphite/template"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/prompb"
)
// MetricLabelsFromTags provides labels for given tags.
func MetricLabelsFromTags(tags map[string]string, prefix string) ([]*prompb.Label, error) {
// It translates Graphite tags directly into label and values.
var labels []*prompb.Label
var names []string
// Sort tags to have a deterministic order, better for tests.
for k := range tags {
names = append(names, k)
}
sort.Strings(names)
for _, k := range names {
v := tags[k]
if k == "name" {
v = strings.TrimPrefix(v, prefix)
labels = append(labels, &prompb.Label{Name: model.MetricNameLabel, Value: v})
} else {
labels = append(labels, &prompb.Label{Name: k, Value: v})
}
}
return labels, nil
}
// MetricLabelsFromPath provides labels from given path.
func MetricLabelsFromPath(path string, prefix string) ([]*prompb.Label, error) {
// It uses the "default" write format to read back (See defaultPath function)
// <prefix.><__name__.>[<labelName>.<labelValue>. for each label in alphabetic order]
var labels []*prompb.Label
cleanedPath := strings.TrimPrefix(path, prefix)
cleanedPath = strings.Trim(cleanedPath, ".")
nodes := strings.Split(cleanedPath, ".")
labels = append(labels, &prompb.Label{Name: model.MetricNameLabel, Value: nodes[0]})
if len(nodes[1:])%2 != 0 {
err := fmt.Errorf("Unable to parse labels from path: odd number of nodes in path")
return nil, err
}
for i := 1; i < len(nodes); i += 2 {
labels = append(labels, &prompb.Label{Name: graphite_tmpl.Unescape(nodes[i]), Value: graphite_tmpl.Unescape(nodes[i+1])})
}
return labels, nil
}