-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathcompat.go
More file actions
166 lines (141 loc) · 4.4 KB
/
compat.go
File metadata and controls
166 lines (141 loc) · 4.4 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) The Cortex Authors.
// Licensed under the Apache License 2.0.
package cortexpb
import (
stdjson "encoding/json"
"fmt"
"math"
"sort"
"strconv"
"strings"
"time"
"unsafe"
jsoniter "github.com/json-iterator/go"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/thanos-io/thanos/internal/cortex/util"
)
// FromLabelAdaptersToLabels converts []LabelAdapter to labels.Labels.
func FromLabelAdaptersToLabels(ls []LabelAdapter) labels.Labels {
if len(ls) == 0 {
return labels.EmptyLabels()
}
b := labels.NewScratchBuilder(len(ls))
for _, l := range ls {
b.Add(l.Name, l.Value)
}
b.Sort()
return b.Labels()
}
// FromLabelsToLabelAdapters converts labels.Labels to []LabelAdapter.
func FromLabelsToLabelAdapters(ls labels.Labels) []LabelAdapter {
if ls.IsEmpty() {
return nil
}
result := make([]LabelAdapter, 0, ls.Len())
ls.Range(func(l labels.Label) {
result = append(result, LabelAdapter{Name: l.Name, Value: l.Value})
})
return result
}
// FromLabelAdaptersToMetric converts []LabelAdapter to a model.Metric.
// Don't do this on any performance sensitive paths.
func FromLabelAdaptersToMetric(ls []LabelAdapter) model.Metric {
return util.LabelsToMetric(FromLabelAdaptersToLabels(ls))
}
// FromMetricsToLabelAdapters converts model.Metric to []LabelAdapter.
// Don't do this on any performance sensitive paths.
// The result is sorted.
func FromMetricsToLabelAdapters(metric model.Metric) []LabelAdapter {
result := make([]LabelAdapter, 0, len(metric))
for k, v := range metric {
result = append(result, LabelAdapter{
Name: string(k),
Value: string(v),
})
}
sort.Sort(byLabel(result)) // The labels should be sorted upon initialisation.
return result
}
type byLabel []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] }
// isTesting is only set from tests to get special behaviour to verify that custom sample encode and decode is used,
// both when using jsonitor or standard json package.
var isTesting = false
// MarshalJSON implements json.Marshaler.
func (s Sample) MarshalJSON() ([]byte, error) {
if isTesting && math.IsNaN(s.Value) {
return nil, fmt.Errorf("test sample")
}
t, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(model.Time(s.TimestampMs))
if err != nil {
return nil, err
}
v, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(model.SampleValue(s.Value))
if err != nil {
return nil, err
}
return fmt.Appendf(nil, "[%s,%s]", t, v), nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (s *Sample) UnmarshalJSON(b []byte) error {
var t model.Time
var v model.SampleValue
vs := [...]stdjson.Unmarshaler{&t, &v}
if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(b, &vs); err != nil {
return err
}
s.TimestampMs = int64(t)
s.Value = float64(v)
if isTesting && math.IsNaN(float64(v)) {
return fmt.Errorf("test sample")
}
return nil
}
func SampleJsoniterEncode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
sample := (*Sample)(ptr)
if isTesting && math.IsNaN(sample.Value) {
stream.Error = fmt.Errorf("test sample")
return
}
stream.WriteArrayStart()
stream.WriteFloat64(float64(sample.TimestampMs) / float64(time.Second/time.Millisecond))
stream.WriteMore()
stream.WriteString(model.SampleValue(sample.Value).String())
stream.WriteArrayEnd()
}
func SampleJsoniterDecode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
if !iter.ReadArray() {
iter.ReportError("cortexpb.Sample", "expected [")
return
}
t := model.Time(iter.ReadFloat64() * float64(time.Second/time.Millisecond))
if !iter.ReadArray() {
iter.ReportError("cortexpb.Sample", "expected ,")
return
}
bs := iter.ReadStringAsSlice()
ss := *(*string)(unsafe.Pointer(&bs))
v, err := strconv.ParseFloat(ss, 64)
if err != nil {
iter.ReportError("cortexpb.Sample", err.Error())
return
}
if isTesting && math.IsNaN(v) {
iter.Error = fmt.Errorf("test sample")
return
}
if iter.ReadArray() {
iter.ReportError("cortexpb.Sample", "expected ]")
}
*(*Sample)(ptr) = Sample{
TimestampMs: int64(t),
Value: v,
}
}
func init() {
jsoniter.RegisterTypeEncoderFunc("cortexpb.Sample", SampleJsoniterEncode, func(unsafe.Pointer) bool { return false })
jsoniter.RegisterTypeDecoderFunc("cortexpb.Sample", SampleJsoniterDecode)
}