-
Notifications
You must be signed in to change notification settings - Fork 2k
/
custom.go
110 lines (93 loc) · 2.64 KB
/
custom.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
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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package exemplarspb
import (
"encoding/json"
"math/big"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/exemplar"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/thanos-io/thanos/pkg/store/labelpb"
)
// ExemplarStore wraps the ExemplarsClient and contains the info of external labels.
type ExemplarStore struct {
ExemplarsClient
LabelSets []labels.Labels
}
// UnmarshalJSON implements json.Unmarshaler.
func (m *Exemplar) UnmarshalJSON(b []byte) error {
v := struct {
Labels labelpb.ZLabelSet
TimeStamp model.Time
Value model.SampleValue
}{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
m.Labels = v.Labels
m.Ts = int64(v.TimeStamp)
m.Value = float64(v.Value)
return nil
}
// MarshalJSON implements json.Marshaler.
func (m *Exemplar) MarshalJSON() ([]byte, error) {
v := struct {
Labels labels.Labels `json:"labels"`
TimeStamp model.Time `json:"timestamp"`
Value model.SampleValue `json:"value"`
}{
Labels: labelpb.ZLabelsToPromLabels(m.Labels.Labels),
TimeStamp: model.Time(m.Ts),
Value: model.SampleValue(m.Value),
}
return json.Marshal(v)
}
func NewExemplarsResponse(e *ExemplarData) *ExemplarsResponse {
return &ExemplarsResponse{
Result: &ExemplarsResponse_Data{
Data: e,
},
}
}
func NewWarningExemplarsResponse(warning error) *ExemplarsResponse {
return &ExemplarsResponse{
Result: &ExemplarsResponse_Warning{
Warning: warning.Error(),
},
}
}
// Compare only compares the series labels of two exemplar data.
func (s1 *ExemplarData) Compare(s2 *ExemplarData) int {
return labels.Compare(s1.SeriesLabels.PromLabels(), s2.SeriesLabels.PromLabels())
}
func (s *ExemplarData) SetSeriesLabels(ls labels.Labels) {
var result labelpb.ZLabelSet
if len(ls) > 0 {
result = labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(ls)}
}
s.SeriesLabels = result
}
// Compare is used for sorting and comparing exemplars. Start from timestamp, then labels, finally values.
func (e1 *Exemplar) Compare(e2 *Exemplar) int {
if e1.Ts < e2.Ts {
return -1
}
if e1.Ts > e2.Ts {
return 1
}
if d := labels.Compare(e1.Labels.PromLabels(), e2.Labels.PromLabels()); d != 0 {
return d
}
return big.NewFloat(e1.Value).Cmp(big.NewFloat(e2.Value))
}
func ExemplarsFromPromExemplars(exemplars []exemplar.Exemplar) []*Exemplar {
ex := make([]*Exemplar, 0, len(exemplars))
for _, e := range exemplars {
ex = append(ex, &Exemplar{
Labels: labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(e.Labels)},
Value: e.Value,
Ts: e.Ts,
})
}
return ex
}