github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/ingester/client/compat.go (about) 1 package client 2 3 import ( 4 "fmt" 5 6 "github.com/prometheus/common/model" 7 "github.com/prometheus/prometheus/pkg/labels" 8 9 "github.com/cortexproject/cortex/pkg/cortexpb" 10 ) 11 12 // ToQueryRequest builds a QueryRequest proto. 13 func ToQueryRequest(from, to model.Time, matchers []*labels.Matcher) (*QueryRequest, error) { 14 ms, err := toLabelMatchers(matchers) 15 if err != nil { 16 return nil, err 17 } 18 19 return &QueryRequest{ 20 StartTimestampMs: int64(from), 21 EndTimestampMs: int64(to), 22 Matchers: ms, 23 }, nil 24 } 25 26 // FromQueryRequest unpacks a QueryRequest proto. 27 func FromQueryRequest(req *QueryRequest) (model.Time, model.Time, []*labels.Matcher, error) { 28 matchers, err := FromLabelMatchers(req.Matchers) 29 if err != nil { 30 return 0, 0, nil, err 31 } 32 from := model.Time(req.StartTimestampMs) 33 to := model.Time(req.EndTimestampMs) 34 return from, to, matchers, nil 35 } 36 37 // ToExemplarQueryRequest builds an ExemplarQueryRequest proto. 38 func ToExemplarQueryRequest(from, to model.Time, matchers ...[]*labels.Matcher) (*ExemplarQueryRequest, error) { 39 var reqMatchers []*LabelMatchers 40 for _, m := range matchers { 41 ms, err := toLabelMatchers(m) 42 if err != nil { 43 return nil, err 44 } 45 reqMatchers = append(reqMatchers, &LabelMatchers{ms}) 46 } 47 48 return &ExemplarQueryRequest{ 49 StartTimestampMs: int64(from), 50 EndTimestampMs: int64(to), 51 Matchers: reqMatchers, 52 }, nil 53 } 54 55 // FromExemplarQueryRequest unpacks a ExemplarQueryRequest proto. 56 func FromExemplarQueryRequest(req *ExemplarQueryRequest) (int64, int64, [][]*labels.Matcher, error) { 57 var result [][]*labels.Matcher 58 for _, m := range req.Matchers { 59 matchers, err := FromLabelMatchers(m.Matchers) 60 if err != nil { 61 return 0, 0, nil, err 62 } 63 result = append(result, matchers) 64 } 65 66 return req.StartTimestampMs, req.EndTimestampMs, result, nil 67 } 68 69 // ToQueryResponse builds a QueryResponse proto. 70 func ToQueryResponse(matrix model.Matrix) *QueryResponse { 71 resp := &QueryResponse{} 72 for _, ss := range matrix { 73 ts := cortexpb.TimeSeries{ 74 Labels: cortexpb.FromMetricsToLabelAdapters(ss.Metric), 75 Samples: make([]cortexpb.Sample, 0, len(ss.Values)), 76 } 77 for _, s := range ss.Values { 78 ts.Samples = append(ts.Samples, cortexpb.Sample{ 79 Value: float64(s.Value), 80 TimestampMs: int64(s.Timestamp), 81 }) 82 } 83 resp.Timeseries = append(resp.Timeseries, ts) 84 } 85 return resp 86 } 87 88 // FromQueryResponse unpacks a QueryResponse proto. 89 func FromQueryResponse(resp *QueryResponse) model.Matrix { 90 m := make(model.Matrix, 0, len(resp.Timeseries)) 91 for _, ts := range resp.Timeseries { 92 var ss model.SampleStream 93 ss.Metric = cortexpb.FromLabelAdaptersToMetric(ts.Labels) 94 ss.Values = make([]model.SamplePair, 0, len(ts.Samples)) 95 for _, s := range ts.Samples { 96 ss.Values = append(ss.Values, model.SamplePair{ 97 Value: model.SampleValue(s.Value), 98 Timestamp: model.Time(s.TimestampMs), 99 }) 100 } 101 m = append(m, &ss) 102 } 103 104 return m 105 } 106 107 // ToMetricsForLabelMatchersRequest builds a MetricsForLabelMatchersRequest proto 108 func ToMetricsForLabelMatchersRequest(from, to model.Time, matchers []*labels.Matcher) (*MetricsForLabelMatchersRequest, error) { 109 ms, err := toLabelMatchers(matchers) 110 if err != nil { 111 return nil, err 112 } 113 114 return &MetricsForLabelMatchersRequest{ 115 StartTimestampMs: int64(from), 116 EndTimestampMs: int64(to), 117 MatchersSet: []*LabelMatchers{{Matchers: ms}}, 118 }, nil 119 } 120 121 // FromMetricsForLabelMatchersRequest unpacks a MetricsForLabelMatchersRequest proto 122 func FromMetricsForLabelMatchersRequest(req *MetricsForLabelMatchersRequest) (model.Time, model.Time, [][]*labels.Matcher, error) { 123 matchersSet := make([][]*labels.Matcher, 0, len(req.MatchersSet)) 124 for _, matchers := range req.MatchersSet { 125 matchers, err := FromLabelMatchers(matchers.Matchers) 126 if err != nil { 127 return 0, 0, nil, err 128 } 129 matchersSet = append(matchersSet, matchers) 130 } 131 from := model.Time(req.StartTimestampMs) 132 to := model.Time(req.EndTimestampMs) 133 return from, to, matchersSet, nil 134 } 135 136 // FromMetricsForLabelMatchersResponse unpacks a MetricsForLabelMatchersResponse proto 137 func FromMetricsForLabelMatchersResponse(resp *MetricsForLabelMatchersResponse) []model.Metric { 138 metrics := []model.Metric{} 139 for _, m := range resp.Metric { 140 metrics = append(metrics, cortexpb.FromLabelAdaptersToMetric(m.Labels)) 141 } 142 return metrics 143 } 144 145 // ToLabelValuesRequest builds a LabelValuesRequest proto 146 func ToLabelValuesRequest(labelName model.LabelName, from, to model.Time, matchers []*labels.Matcher) (*LabelValuesRequest, error) { 147 ms, err := toLabelMatchers(matchers) 148 if err != nil { 149 return nil, err 150 } 151 152 return &LabelValuesRequest{ 153 LabelName: string(labelName), 154 StartTimestampMs: int64(from), 155 EndTimestampMs: int64(to), 156 Matchers: &LabelMatchers{Matchers: ms}, 157 }, nil 158 } 159 160 // FromLabelValuesRequest unpacks a LabelValuesRequest proto 161 func FromLabelValuesRequest(req *LabelValuesRequest) (string, int64, int64, []*labels.Matcher, error) { 162 var err error 163 var matchers []*labels.Matcher 164 165 if req.Matchers != nil { 166 matchers, err = FromLabelMatchers(req.Matchers.Matchers) 167 if err != nil { 168 return "", 0, 0, nil, err 169 } 170 } 171 172 return req.LabelName, req.StartTimestampMs, req.EndTimestampMs, matchers, nil 173 } 174 175 func toLabelMatchers(matchers []*labels.Matcher) ([]*LabelMatcher, error) { 176 result := make([]*LabelMatcher, 0, len(matchers)) 177 for _, matcher := range matchers { 178 var mType MatchType 179 switch matcher.Type { 180 case labels.MatchEqual: 181 mType = EQUAL 182 case labels.MatchNotEqual: 183 mType = NOT_EQUAL 184 case labels.MatchRegexp: 185 mType = REGEX_MATCH 186 case labels.MatchNotRegexp: 187 mType = REGEX_NO_MATCH 188 default: 189 return nil, fmt.Errorf("invalid matcher type") 190 } 191 result = append(result, &LabelMatcher{ 192 Type: mType, 193 Name: matcher.Name, 194 Value: matcher.Value, 195 }) 196 } 197 return result, nil 198 } 199 200 func FromLabelMatchers(matchers []*LabelMatcher) ([]*labels.Matcher, error) { 201 result := make([]*labels.Matcher, 0, len(matchers)) 202 for _, matcher := range matchers { 203 var mtype labels.MatchType 204 switch matcher.Type { 205 case EQUAL: 206 mtype = labels.MatchEqual 207 case NOT_EQUAL: 208 mtype = labels.MatchNotEqual 209 case REGEX_MATCH: 210 mtype = labels.MatchRegexp 211 case REGEX_NO_MATCH: 212 mtype = labels.MatchNotRegexp 213 default: 214 return nil, fmt.Errorf("invalid matcher type") 215 } 216 matcher, err := labels.NewMatcher(mtype, matcher.Name, matcher.Value) 217 if err != nil { 218 return nil, err 219 } 220 result = append(result, matcher) 221 } 222 return result, nil 223 } 224 225 // FastFingerprint runs the same algorithm as Prometheus labelSetToFastFingerprint() 226 func FastFingerprint(ls []cortexpb.LabelAdapter) model.Fingerprint { 227 if len(ls) == 0 { 228 return model.Metric(nil).FastFingerprint() 229 } 230 231 var result uint64 232 for _, l := range ls { 233 sum := hashNew() 234 sum = hashAdd(sum, l.Name) 235 sum = hashAddByte(sum, model.SeparatorByte) 236 sum = hashAdd(sum, l.Value) 237 result ^= sum 238 } 239 return model.Fingerprint(result) 240 } 241 242 // Fingerprint runs the same algorithm as Prometheus labelSetToFingerprint() 243 func Fingerprint(labels labels.Labels) model.Fingerprint { 244 sum := hashNew() 245 for _, label := range labels { 246 sum = hashAddString(sum, label.Name) 247 sum = hashAddByte(sum, model.SeparatorByte) 248 sum = hashAddString(sum, label.Value) 249 sum = hashAddByte(sum, model.SeparatorByte) 250 } 251 return model.Fingerprint(sum) 252 } 253 254 // LabelsToKeyString is used to form a string to be used as 255 // the hashKey. Don't print, use l.String() for printing. 256 func LabelsToKeyString(l labels.Labels) string { 257 // We are allocating 1024, even though most series are less than 600b long. 258 // But this is not an issue as this function is being inlined when called in a loop 259 // and buffer allocated is a static buffer and not a dynamic buffer on the heap. 260 b := make([]byte, 0, 1024) 261 return string(l.Bytes(b)) 262 }