forked from grafana/loki
/
conv.go
49 lines (41 loc) · 1.46 KB
/
conv.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
package util
import (
"time"
"unsafe"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
)
// 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) {
fromMs := from.UnixNano() / int64(time.Millisecond)
throughMs := through.UnixNano() / int64(time.Millisecond)
// add a millisecond to the through time if the nanosecond offset within the second is not a multiple of milliseconds
if int64(through.Nanosecond())%int64(time.Millisecond) != 0 {
throughMs++
}
return model.Time(fromMs), model.Time(throughMs)
}
// 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
}