Repository navigation
Expand file tree
/
Copy pathconv.go
More file actions
89 lines (77 loc) · 2.48 KB
/
Copy pathconv.go
File metadata and controls
89 lines (77 loc) · 2.48 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package util
import (
"math"
"sort"
"strings"
"time"
"unsafe"
"github.com/cortexproject/cortex/pkg/ingester/client"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/grafana/loki/pkg/logql"
)
type byLabel []client.LabelAdapter
func (s byLabel) Len() int { return len(s) }
func (s byLabel) Less(i, j int) bool { return strings.Compare(s[i].Name, s[j].Name) < 0 }
func (s byLabel) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// ToClientLabels parses the labels and converts them to the Cortex type.
func ToClientLabels(labels string) ([]client.LabelAdapter, error) {
matchers, err := logql.ParseMatchers(labels)
if err != nil {
return nil, err
}
result := make([]client.LabelAdapter, 0, len(matchers))
for _, m := range matchers {
result = append(result, client.LabelAdapter{
Name: m.Name,
Value: m.Value,
})
}
sort.Sort(byLabel(result))
return result, nil
}
// ParseLabels parses labels from a string using logql parser.
func ParseLabels(lbs string) (labels.Labels, error) {
matchers, err := logql.ParseMatchers(lbs)
if err != nil {
return nil, err
}
result := make(labels.Labels, 0, len(matchers))
for _, m := range matchers {
result = append(result, labels.Label{
Name: m.Name,
Value: m.Value,
})
}
sort.Sort(result)
return result, nil
}
// ModelLabelSetToMap convert a model.LabelSet to a map[string]string
func ModelLabelSetToMap(m model.LabelSet) map[string]string {
if len(m) == 0 {
return map[string]string{}
}
return *(*map[string]string)(unsafe.Pointer(&m))
}
// MapToModelLabelSet converts a map into a model.LabelSet
func MapToModelLabelSet(m map[string]string) model.LabelSet {
if len(m) == 0 {
return model.LabelSet{}
}
return *(*map[model.LabelName]model.LabelValue)(unsafe.Pointer(&m))
}
// RoundToMilliseconds returns milliseconds precision time from nanoseconds.
// from will be rounded down to the nearest milliseconds while through is rounded up.
func RoundToMilliseconds(from, through time.Time) (model.Time, model.Time) {
return model.Time(int64(math.Floor(float64(from.UnixNano()) / float64(time.Millisecond)))),
model.Time(int64(math.Ceil(float64(through.UnixNano()) / float64(time.Millisecond))))
}
// 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, len(ls))
for _, l := range ls {
m[model.LabelName(l.Name)] = model.LabelValue(l.Value)
}
return m
}