-
Notifications
You must be signed in to change notification settings - Fork 53
/
admin.go
964 lines (899 loc) · 27.2 KB
/
admin.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
package cortex
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/cortexproject/cortex/pkg/cortexpb"
"github.com/cortexproject/cortex/pkg/distributor"
"github.com/rancher/opni/plugins/metrics/apis/cortexadmin"
"github.com/samber/lo"
"github.com/tidwall/gjson"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/rancher/opni/pkg/config/v1beta1"
"github.com/rancher/opni/pkg/util"
metricsutil "github.com/rancher/opni/plugins/metrics/pkg/util"
)
type CortexAdminServer struct {
cortexadmin.UnsafeCortexAdminServer
CortexAdminServerConfig
util.Initializer
}
type CortexAdminServerConfig struct {
CortexClientSet ClientSet `validate:"required"`
Config *v1beta1.GatewayConfigSpec `validate:"required"`
Logger *zap.SugaredLogger `validate:"required"`
}
func (p *CortexAdminServer) Initialize(conf CortexAdminServerConfig) {
p.InitOnce(func() {
if err := metricsutil.Validate.Struct(conf); err != nil {
panic(err)
}
p.CortexAdminServerConfig = conf
})
}
var _ cortexadmin.CortexAdminServer = (*CortexAdminServer)(nil)
func (p *CortexAdminServer) AllUserStats(ctx context.Context, _ *emptypb.Empty) (*cortexadmin.UserIDStatsList, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
client := p.CortexClientSet
req, err := http.NewRequestWithContext(ctx, "GET",
fmt.Sprintf("https://%s/distributor/all_user_stats", p.Config.Cortex.Distributor.HTTPAddress), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
resp, err := client.HTTP().Do(req)
if err != nil {
return nil, fmt.Errorf("failed to get cluster stats: %w", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
p.Logger.With(
"err", err,
).Error("failed to close response body")
}
}(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to get cluster stats: %v", resp.StatusCode)
}
var stats []distributor.UserIDStats
if err = json.NewDecoder(resp.Body).Decode(&stats); err != nil {
return nil, fmt.Errorf("failed to decode user stats: %w", err)
}
statsList := &cortexadmin.UserIDStatsList{
Items: make([]*cortexadmin.UserIDStats, len(stats)),
}
for i, s := range stats {
statsList.Items[i] = &cortexadmin.UserIDStats{
UserID: s.UserID,
IngestionRate: s.IngestionRate,
NumSeries: s.NumSeries,
APIIngestionRate: s.APIIngestionRate,
RuleIngestionRate: s.RuleIngestionRate,
}
}
return statsList, nil
}
func mapLabels(l *cortexadmin.Label, _ int) cortexpb.LabelAdapter {
return cortexpb.LabelAdapter{
Name: l.Name,
Value: l.Value,
}
}
func mapSamples(s *cortexadmin.Sample, _ int) cortexpb.Sample {
return cortexpb.Sample{
TimestampMs: s.TimestampMs,
Value: s.Value,
}
}
func mapExemplars(e *cortexadmin.Exemplar, _ int) cortexpb.Exemplar {
return cortexpb.Exemplar{
Value: e.Value,
TimestampMs: e.TimestampMs,
Labels: lo.Map(e.Labels, mapLabels),
}
}
func mapMetadata(m *cortexadmin.MetricMetadata, _ int) *cortexpb.MetricMetadata {
return &cortexpb.MetricMetadata{
Type: cortexpb.MetricMetadata_MetricType(m.Type),
MetricFamilyName: m.MetricFamilyName,
Help: m.Help,
Unit: m.Unit,
}
}
func mapTimeSeries(t *cortexadmin.TimeSeries, _ int) cortexpb.PreallocTimeseries {
return cortexpb.PreallocTimeseries{
TimeSeries: &cortexpb.TimeSeries{
Labels: lo.Map(t.Labels, mapLabels),
Samples: lo.Map(t.Samples, mapSamples),
Exemplars: lo.Map(t.Exemplars, mapExemplars),
},
}
}
func (p *CortexAdminServer) WriteMetrics(ctx context.Context, in *cortexadmin.WriteRequest) (*cortexadmin.WriteResponse, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
lg := p.Logger.With(
"clusterID", in.ClusterID,
"seriesCount", len(in.Timeseries),
)
if in.ClusterID == "" {
return nil, status.Error(codes.InvalidArgument, "clusterID is required")
}
cortexReq := &cortexpb.WriteRequest{
Timeseries: lo.Map(in.Timeseries, mapTimeSeries),
Source: cortexpb.API,
Metadata: lo.Map(in.Metadata, mapMetadata),
}
lg.Debug("writing metrics to cortex")
_, err := p.CortexClientSet.Distributor().Push(outgoingContext(ctx, in), cortexReq)
if err != nil {
p.Logger.With(zap.Error(err)).Error("failed to write metrics")
return nil, err
}
return &cortexadmin.WriteResponse{}, nil
}
type clusterIDGetter interface {
GetClusterID() string
}
func outgoingContext(ctx context.Context, in clusterIDGetter) context.Context {
return metadata.NewOutgoingContext(ctx, metadata.Pairs(
"x-scope-orgid", in.GetClusterID(),
))
}
func (p *CortexAdminServer) Query(
ctx context.Context,
in *cortexadmin.QueryRequest,
) (*cortexadmin.QueryResponse, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
lg := p.Logger.With(
"query", in.Query,
)
lg.Debug("handling query")
req, err := http.NewRequestWithContext(ctx, "POST",
fmt.Sprintf("https://%s/prometheus/api/v1/query", p.Config.Cortex.QueryFrontend.HTTPAddress), nil)
if err != nil {
return nil, err
}
values := url.Values{}
values.Add("query", in.Query)
req.Body = io.NopCloser(strings.NewReader(values.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
req.Header.Set(orgIDCodec.Key(), orgIDCodec.Encode(in.Tenants))
resp, err := p.CortexClientSet.HTTP().Do(req)
if err != nil {
lg.With(
"error", err,
).Error("query failed")
return nil, err
}
if resp.StatusCode != http.StatusOK {
lg.With(
"status", resp.Status,
).Error("query failed")
return nil, fmt.Errorf("query failed: %s", resp.Status)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
lg.With(
"error", err,
).Error("failed to close response body")
}
}(resp.Body)
responseBuf := new(bytes.Buffer)
if _, err := io.Copy(responseBuf, resp.Body); err != nil {
lg.With(
"error", err,
).Error("failed to read response body")
return nil, err
}
return &cortexadmin.QueryResponse{
Data: responseBuf.Bytes(),
}, nil
}
func (p *CortexAdminServer) QueryRange(
ctx context.Context,
in *cortexadmin.QueryRangeRequest,
) (*cortexadmin.QueryResponse, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
lg := p.Logger.With(
"query", in.Query,
)
client := p.CortexClientSet
values := url.Values{}
values.Add("query", in.Query)
values.Add("start", formatTime(in.Start.AsTime()))
values.Add("end", formatTime(in.End.AsTime()))
values.Add("step", in.Step.AsDuration().String())
req, err := http.NewRequestWithContext(ctx, "POST",
fmt.Sprintf("https://%s/prometheus/api/v1/query_range", p.Config.Cortex.QueryFrontend.HTTPAddress),
strings.NewReader(values.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
req.Header.Set(orgIDCodec.Key(), orgIDCodec.Encode(in.Tenants))
lg.Debug(req.URL.RawQuery)
resp, err := client.HTTP().Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
lg.With(
"status", resp.Status,
).Error("query failed")
return nil, fmt.Errorf("query failed: %s", resp.Status)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
lg.With(
"error", err,
).Error("failed to close response body")
}
}(resp.Body)
responseBuf := new(bytes.Buffer)
if _, err := io.Copy(responseBuf, resp.Body); err != nil {
lg.With(
"error", err,
).Error("failed to read response body")
return nil, err
}
return &cortexadmin.QueryResponse{
Data: responseBuf.Bytes(),
}, nil
}
func (p *CortexAdminServer) GetMetricMetadata(ctx context.Context, req *cortexadmin.MetricMetadataRequest) (*cortexadmin.MetricMetadata, error) {
tenant := orgIDCodec.Encode(req.Tenants)
m := &cortexadmin.MetricMetadata{
MetricFamilyName: req.MetricName,
}
resp, err := p.fetchCortexMetricMetadata(ctx, tenant, req.MetricName)
if err != nil {
return nil, err
}
metadata, err := p.parseCortexSeriesMetadata(resp, req.MetricName)
if err != nil {
return nil, err
}
if metricHelp, ok := metadata["help"]; ok {
m.Help = metricHelp.String()
}
if metricType, ok := metadata["type"]; ok {
m.Type = cortexadmin.MetricMetadata_MetricType(cortexadmin.MetricMetadata_MetricType_value[strings.ToUpper(metricType.String())])
}
if metricUnit, ok := metadata["unit"]; ok {
m.Unit = metricUnit.String()
}
return m, nil
}
func (p *CortexAdminServer) GetRule(ctx context.Context,
in *cortexadmin.GetRuleRequest,
) (*cortexadmin.QueryResponse, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
lg := p.Logger.With(
"group name", in.GroupName,
)
req, err := http.NewRequestWithContext(ctx, "GET",
fmt.Sprintf("https://%s/api/v1/rules/%s/%s",
p.Config.Cortex.Ruler.HTTPAddress, in.Namespace, in.GroupName), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set(orgIDCodec.Key(), orgIDCodec.Encode([]string{in.ClusterId}))
resp, err := p.CortexClientSet.HTTP().Do(req)
if err != nil {
lg.With(
"error", err,
).Error("fetch failed")
return nil, err
}
if resp.StatusCode == http.StatusNotFound {
err := status.Error(codes.NotFound, "fetch failed : 404 not found")
return nil, err
}
if resp.StatusCode != http.StatusOK {
lg.With(
"status", resp.Status,
).Error("fetch failed")
return nil, fmt.Errorf("fetch failed: %s", resp.Status)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
lg.With(
"error", err,
).Error("failed to close response body")
}
}(resp.Body)
responseBuf := new(bytes.Buffer)
if _, err := io.Copy(responseBuf, resp.Body); err != nil {
lg.With(
"error", err,
).Error("failed to read response body")
return nil, err
}
return &cortexadmin.QueryResponse{
Data: responseBuf.Bytes(),
}, nil
}
func (p *CortexAdminServer) ListRules(ctx context.Context, req *cortexadmin.ListRulesRequest) (*cortexadmin.ListRulesResponse, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
lg := p.Logger.With(
"cluster id", req.ClusterId,
)
if err := req.Validate(); err != nil {
return nil, err
}
returnGroup := make([][]*cortexadmin.RuleGroup, len(req.ClusterId))
var wg sync.WaitGroup
// cortex only allows us to proxy the request to one tenant at a time, returns a 500 internal error otherwise
for i, clusterId := range req.ClusterId {
i := i
clusterId := clusterId
wg.Add(1)
go func() {
defer wg.Done()
resp, err := p.listCortexRules(ctx, clusterId)
if err != nil {
lg.Error(err)
return
}
if resp.StatusCode != http.StatusOK {
lg.With(
"status", resp.Status,
).Error("list rules failed")
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
lg.Errorf("failed to read response body: %w", err)
return
}
ruleResp := &cortexadmin.ListRulesResponse{}
err = json.Unmarshal(body, ruleResp)
if err != nil {
lg.Error(err)
return
}
filteredGroup := req.Filter(ruleResp.Data, clusterId)
returnGroup[i] = filteredGroup.Groups
}()
}
wg.Wait()
return &cortexadmin.ListRulesResponse{
Status: "success",
Data: &cortexadmin.RuleGroups{
Groups: lo.Flatten(returnGroup),
},
}, nil
}
// LoadRules This method is responsible for Creating and Updating Rules
func (p *CortexAdminServer) LoadRules(ctx context.Context,
in *cortexadmin.LoadRuleRequest,
) (*emptypb.Empty, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
lg := p.Logger.With(
"cluster", in.ClusterId,
)
if err := in.Validate(); err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "POST",
fmt.Sprintf("https://%s/api/v1/rules/%s", p.Config.Cortex.Ruler.HTTPAddress, in.Namespace), nil)
if err != nil {
return nil, err
}
req.Body = io.NopCloser(bytes.NewReader(in.YamlContent))
req.Header.Set("Content-Type", "application/yaml")
req.Header.Set(orgIDCodec.Key(), orgIDCodec.Encode([]string{in.ClusterId}))
resp, err := p.CortexClientSet.HTTP().Do(req)
if err != nil {
lg.With(
"error", err,
).Error("loading rules failed")
return nil, err
}
if resp.StatusCode != http.StatusAccepted {
lg.With(
"Code", resp.StatusCode,
"error", resp.Status,
).Error("loading rules failed")
return nil, fmt.Errorf("loading rules failed: %d", resp.StatusCode)
}
return &emptypb.Empty{}, nil
}
func (p *CortexAdminServer) DeleteRule(
ctx context.Context,
in *cortexadmin.DeleteRuleRequest,
) (*emptypb.Empty, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
lg := p.Logger.With(
"group", in.GroupName,
"namespace", in.Namespace,
"cluster", in.ClusterId,
)
req, err := http.NewRequestWithContext(ctx, "DELETE",
fmt.Sprintf("https://%s/api/v1/rules/%s/%s",
p.Config.Cortex.Ruler.HTTPAddress, in.Namespace, in.GroupName), nil)
if err != nil {
return nil, err
}
req.Header.Set(orgIDCodec.Key(), orgIDCodec.Encode([]string{in.ClusterId}))
resp, err := p.CortexClientSet.HTTP().Do(req)
if err != nil {
lg.With(
"error", err,
).Error("delete rule group failed")
return nil, err
}
if resp.StatusCode != http.StatusAccepted {
lg.With(
"status", resp.Status,
).Error("delete rule group failed")
if resp.StatusCode == http.StatusNotFound { // return grpc not found in this case
err := status.Error(codes.NotFound, fmt.Sprintf("delete rule group failed %s", err))
return nil, err
}
return nil, fmt.Errorf("delete rule group failed, unexpected status code: `%s` - %s", err, resp.Status)
}
return &emptypb.Empty{}, nil
}
func (p *CortexAdminServer) GetSeriesMetrics(ctx context.Context, request *cortexadmin.SeriesRequest) (*cortexadmin.SeriesInfoList, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
resp, err := p.enumerateCortexSeries(ctx, request)
if err != nil {
return nil, err
}
set, err := p.parseCortexEnumerateSeries(resp)
if err != nil {
return nil, err
}
res := make([]*cortexadmin.SeriesInfo, 0, len(set))
for uniqueMetricName := range set {
// fetch metadata & handle empty
m := &cortexadmin.SeriesMetadata{}
resp, err := p.fetchCortexMetricMetadata(ctx, request.Tenant, uniqueMetricName)
if err == nil { // parse response, otherwise skip and return empty metadata
mapVal, err := p.parseCortexSeriesMetadata(resp, uniqueMetricName)
if err == nil {
if metricHelp, ok := mapVal["help"]; ok {
m.Description = metricHelp.String()
}
if metricType, ok := mapVal["type"]; ok {
m.Type = metricType.String()
}
if metricUnit, ok := mapVal["unit"]; ok && metricUnit.String() == "" {
m.Unit = metricUnit.String()
}
}
}
res = append(res, &cortexadmin.SeriesInfo{
SeriesName: uniqueMetricName,
Metadata: m,
})
}
return &cortexadmin.SeriesInfoList{
Items: res,
}, nil
}
func (p *CortexAdminServer) ExtractRawSeries(ctx context.Context, request *cortexadmin.MatcherRequest) (*cortexadmin.QueryResponse, error) {
lg := p.Logger.With("series matcher", request.MatchExpr)
lg.Debug("fetching raw series")
return p.Query(ctx, &cortexadmin.QueryRequest{
Tenants: []string{request.Tenant},
Query: fmt.Sprintf("{__name__=~\"%s\"}", request.MatchExpr),
})
}
func (p *CortexAdminServer) GetMetricLabelSets(ctx context.Context, request *cortexadmin.LabelRequest) (*cortexadmin.MetricLabels, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
resp, err := p.enumerateCortexSeries(ctx, &cortexadmin.SeriesRequest{
Tenant: request.Tenant,
JobId: request.JobId,
})
if err != nil {
return nil, err
}
labelSets, err := p.parseCortexLabelsOnSeriesJob(resp, request.MetricName, request.JobId)
if err != nil {
return nil, err
}
resultSets := []*cortexadmin.LabelSet{}
for labelName, labelValues := range labelSets {
item := &cortexadmin.LabelSet{
Name: labelName,
Items: []string{},
}
for labelVal := range labelValues {
item.Items = append(item.Items, labelVal)
}
resultSets = append(resultSets, item)
}
return &cortexadmin.MetricLabels{
Items: resultSets,
}, nil
}
func formatTime(t time.Time) string {
return strconv.FormatFloat(float64(t.Unix())+float64(t.Nanosecond())/1e9, 'f', -1, 64)
}
// subset of response types from cortex pkg/ring/http.go
type ingesterDesc struct {
ID string `json:"id"`
State string `json:"state"`
Address string `json:"address"`
}
type httpResponse struct {
Ingesters []ingesterDesc `json:"shards"`
}
func (p *CortexAdminServer) FlushBlocks(
ctx context.Context,
_ *emptypb.Empty,
) (*emptypb.Empty, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
// look up all healthy ingesters
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
fmt.Sprintf("https://%s/ingester/ring", p.Config.Cortex.Distributor.HTTPAddress), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
resp, err := p.CortexClientSet.HTTP().Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to get ingester ring: %s", resp.Status)
}
var ring httpResponse
body, _ := io.ReadAll(resp.Body)
err = resp.Body.Close()
if err != nil {
p.Logger.Error("failed to close response body")
}
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&ring); err != nil {
return nil, err
}
// flush all active ingesters
wg := errgroup.Group{}
for _, ingester := range ring.Ingesters {
lg := p.Logger.With(
"id", ingester.ID,
)
if ingester.State != "ACTIVE" {
lg.Warn("not flushing inactive ingester")
continue
}
// the ring only shows the grpc port, so switch it with the http port
// from the cortex config
host, _, err := net.SplitHostPort(ingester.Address)
if err != nil {
return nil, err
}
// set the hostname from the cortex config as the tls server name, since
// the server's cert won't have a san for its ip in the pod cidr
hostSan, port, err := net.SplitHostPort(p.Config.Cortex.Distributor.HTTPAddress)
if err != nil {
return nil, err
}
address := fmt.Sprintf("%s:%s", host, port)
httpClient := p.CortexClientSet.HTTP(WithServerNameOverride(hostSan))
wg.Go(func() error {
// adding ?wait=true will cause this request to block
values := url.Values{}
values.Add("wait", "true")
req, err := http.NewRequestWithContext(ctx, http.MethodPost,
fmt.Sprintf("https://%s/ingester/flush", address), strings.NewReader(values.Encode()))
if err != nil {
return err
}
resp, err := httpClient.Do(req)
if err != nil {
lg.With(
zap.Error(err),
).Error("failed to flush ingester")
return err
}
if resp.StatusCode != http.StatusNoContent {
body, _ := io.ReadAll(resp.Body)
err := resp.Body.Close()
if err != nil {
lg.Error(
"failed to close response body",
)
}
lg.With(
"code", resp.StatusCode,
"error", string(body),
).Errorf("failed to flush ingester")
}
lg.Info("flushed ingester successfully")
return nil
})
}
err = wg.Wait()
if err != nil {
return nil, err
}
return &emptypb.Empty{}, nil
}
func (p *CortexAdminServer) GetCortexStatus(ctx context.Context, _ *emptypb.Empty) (*cortexadmin.CortexStatus, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
stat := &cortexadmin.CortexStatus{}
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() (err error) {
stat.Distributor, err = p.CortexClientSet.Distributor().Status(ctx)
return
})
eg.Go(func() (err error) {
stat.Ingester, err = p.CortexClientSet.Ingester().Status(ctx)
return
})
eg.Go(func() (err error) {
stat.Ruler, err = p.CortexClientSet.Ruler().Status(ctx)
return
})
eg.Go(func() (err error) {
stat.Purger, err = p.CortexClientSet.Purger().Status(ctx)
return
})
eg.Go(func() (err error) {
stat.Compactor, err = p.CortexClientSet.Compactor().Status(ctx)
return
})
eg.Go(func() (err error) {
stat.StoreGateway, err = p.CortexClientSet.StoreGateway().Status(ctx)
return
})
eg.Go(func() (err error) {
stat.QueryFrontend, err = p.CortexClientSet.QueryFrontend().Status(ctx)
return
})
eg.Go(func() (err error) {
stat.Querier, err = p.CortexClientSet.Querier().Status(ctx)
return
})
if err := eg.Wait(); err != nil {
p.Logger.With(
zap.Error(err),
).Error("failed to get cluster status")
return nil, err
}
stat.Timestamp = timestamppb.Now()
return stat, nil
}
func (p *CortexAdminServer) GetCortexConfig(ctx context.Context, req *cortexadmin.ConfigRequest) (*cortexadmin.ConfigResponse, error) {
if !p.Initialized() {
return nil, util.StatusError(codes.Unavailable)
}
resp := &cortexadmin.ConfigResponse{
ConfigYaml: make([]string, len(req.ConfigModes)),
}
eg, ctx := errgroup.WithContext(ctx)
for i, mode := range req.ConfigModes {
i := i
mode := mode
eg.Go(func() (err error) {
resp.ConfigYaml[i], err = p.CortexClientSet.Distributor().Config(ctx, ConfigMode(mode))
return
})
}
if err := eg.Wait(); err != nil {
return nil, err
}
return resp, nil
}
func (p *CortexAdminServer) proxyCortexToPrometheus(
ctx context.Context,
tenant string,
method string,
url string,
values url.Values,
body io.Reader,
) (*http.Response, error) {
req, err := http.NewRequestWithContext(
ctx,
method,
url,
body,
)
if values != nil {
req.URL.RawQuery = values.Encode()
}
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set(orgIDCodec.Key(), orgIDCodec.Encode([]string{tenant}))
resp, err := p.CortexClientSet.HTTP().Do(req)
if err != nil {
p.Logger.With(
"request", url,
).Errorf("failed with %v", err)
return nil, err
}
if resp.StatusCode != http.StatusOK {
p.Logger.With(
"request", url,
).Errorf("request failed with %s", resp.Status)
return nil, fmt.Errorf("request failed with: %s", resp.Status)
}
return resp, nil
}
// returns duplicate metric names, since labels uniquely identify series but not metrics
func (p *CortexAdminServer) enumerateCortexSeries(ctx context.Context, request *cortexadmin.SeriesRequest) (*http.Response, error) {
values := url.Values{}
values.Add("match[]", fmt.Sprintf("{job=\"%s\"}", request.JobId))
reqUrl := fmt.Sprintf(
"https://%s/prometheus/api/v1/series?",
p.Config.Cortex.QueryFrontend.HTTPAddress,
)
resp, err := p.proxyCortexToPrometheus(ctx, request.Tenant, "GET", reqUrl, values, nil)
return resp, err
}
func (p *CortexAdminServer) parseCortexEnumerateSeries(resp *http.Response) (set map[string]struct{}, err error) {
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
if !gjson.Valid(string(b)) {
return nil, fmt.Errorf("invalid json in response")
}
set = make(map[string]struct{})
result := gjson.Get(string(b), "data.#.__name__")
if !result.Exists() {
return nil, fmt.Errorf("Empty series response from cortex")
}
for _, name := range result.Array() {
set[name.String()] = struct{}{}
}
return set, nil
}
// parseCortexLabelsOnSeriesJob parses the cortex response and returns a map labelNames -> set of labelValues
func (p *CortexAdminServer) parseCortexLabelsOnSeriesJob(
resp *http.Response,
metricName string,
jobName string,
) (map[string]map[string]struct{}, error) {
labelSets := map[string]map[string]struct{}{} // labelName -> set of labelValues
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
if !gjson.Valid(string(b)) {
return nil, fmt.Errorf("invalid json in response")
}
//labelSets := make(map[string]map[string]struct{})
result := gjson.Get(string(b), "data")
if !result.Exists() {
return nil, fmt.Errorf("no data in cortex response")
}
for _, val := range result.Array() {
valToMap := val.Map()
if valToMap["__name__"].String() != metricName || valToMap["job"].String() != jobName {
continue
}
for k, v := range valToMap {
if (k == "__name__" && valToMap[k].String() == metricName) || (k == "job" && valToMap[k].String() == jobName) {
continue
}
if _, ok := labelSets[k]; !ok {
labelSets[k] = make(map[string]struct{})
}
labelSets[k][v.String()] = struct{}{}
}
}
return labelSets, nil
}
func (p *CortexAdminServer) fetchCortexMetricMetadata(ctx context.Context, tenant string, metricName string) (*http.Response, error) {
values := url.Values{}
values.Add("metric", metricName)
reqUrl := fmt.Sprintf(
"https://%s/prometheus/api/v1/metadata?",
p.Config.Cortex.QueryFrontend.HTTPAddress,
)
resp, err := p.proxyCortexToPrometheus(ctx, tenant, "GET", reqUrl, values, nil)
return resp, err
}
func (p *CortexAdminServer) parseCortexSeriesMetadata(resp *http.Response, metricName string) (map[string]gjson.Result, error) {
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
if !gjson.Valid(string(b)) {
return nil, fmt.Errorf("invalid json in response")
}
result := gjson.Get(string(b), fmt.Sprintf("data.%s", metricName))
if !result.Exists() {
return nil, fmt.Errorf("no metadata in cortex response")
}
metadata := result.Array()[0].Map()
return metadata, err
}
func (p *CortexAdminServer) getCortexMetricLabels(ctx context.Context, request *cortexadmin.LabelRequest) (*http.Response, error) {
values := url.Values{}
values.Add("match[]", fmt.Sprintf("%s{job=\"%s\"}", request.MetricName, request.JobId)) // encode the input metric
reqUrl := fmt.Sprintf(
"https://%s/prometheus/api/v1/labels",
p.Config.Cortex.QueryFrontend.HTTPAddress,
)
resp, err := p.proxyCortexToPrometheus(ctx, request.Tenant, "GET", reqUrl, values, nil)
return resp, err
}
func (p *CortexAdminServer) parseCortexMetricLabels(resp *http.Response) ([]string, error) {
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
labelNames := []string{}
result := gjson.Get(string(b), "data")
for _, name := range result.Array() {
if name.String() == "__name__" || name.String() == "job" {
continue
}
labelNames = append(labelNames, name.String())
}
return labelNames, nil
}
func (p *CortexAdminServer) getCortexLabelValues(ctx context.Context, request *cortexadmin.LabelRequest, labelName string) (*http.Response, error) {
reqUrl := fmt.Sprintf(
"https://%s/prometheus/api/v1/label/%s/values",
p.Config.Cortex.QueryFrontend.HTTPAddress,
labelName,
)
resp, err := p.proxyCortexToPrometheus(ctx, request.Tenant, "GET", reqUrl, nil, nil)
return resp, err
}
// The proxy to the prometheus API rules returns a response in the form:
// https://github.com/cortexproject/cortex/blob/c0e4545fd26f33ca5cc3323ee48e4c2ccd182b83/pkg/ruler/api.go#L215
func (p *CortexAdminServer) listCortexRules(ctx context.Context, clusterId string) (*http.Response, error) {
reqUrl := fmt.Sprintf(
"https://%s/prometheus/api/v1/rules",
p.Config.Cortex.Ruler.HTTPAddress,
)
resp, err := p.proxyCortexToPrometheus(ctx, clusterId, "GET", reqUrl, nil, nil)
return resp, err
}