4
4
"context"
5
5
"flag"
6
6
"fmt"
7
+ io "io"
7
8
"net/http"
8
9
"sort"
9
10
"strings"
@@ -871,8 +872,7 @@ func (d *Distributor) ForReplicationSet(ctx context.Context, replicationSet ring
871
872
})
872
873
}
873
874
874
- // LabelValuesForLabelName returns all of the label values that are associated with a given label name.
875
- func (d * Distributor ) LabelValuesForLabelName (ctx context.Context , from , to model.Time , labelName model.LabelName , matchers ... * labels.Matcher ) ([]string , error ) {
875
+ func (d * Distributor ) LabelValuesForLabelNameCommon (ctx context.Context , from , to model.Time , labelName model.LabelName , f func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelValuesRequest ) ([]interface {}, error ), matchers ... * labels.Matcher ) ([]string , error ) {
876
876
replicationSet , err := d .GetIngestersForMetadata (ctx )
877
877
if err != nil {
878
878
return nil , err
@@ -883,16 +883,14 @@ func (d *Distributor) LabelValuesForLabelName(ctx context.Context, from, to mode
883
883
return nil , err
884
884
}
885
885
886
- resps , err := d .ForReplicationSet (ctx , replicationSet , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
887
- return client .LabelValues (ctx , req )
888
- })
886
+ resps , err := f (ctx , replicationSet , req )
889
887
if err != nil {
890
888
return nil , err
891
889
}
892
890
893
891
valueSet := map [string ]struct {}{}
894
892
for _ , resp := range resps {
895
- for _ , v := range resp .(* ingester_client. LabelValuesResponse ). LabelValues {
893
+ for _ , v := range resp .([] string ) {
896
894
valueSet [v ] = struct {}{}
897
895
}
898
896
}
@@ -908,8 +906,46 @@ func (d *Distributor) LabelValuesForLabelName(ctx context.Context, from, to mode
908
906
return values , nil
909
907
}
910
908
911
- // LabelNames returns all of the label names.
912
- func (d * Distributor ) LabelNames (ctx context.Context , from , to model.Time ) ([]string , error ) {
909
+ // LabelValuesForLabelName returns all of the label values that are associated with a given label name.
910
+ func (d * Distributor ) LabelValuesForLabelName (ctx context.Context , from , to model.Time , labelName model.LabelName , matchers ... * labels.Matcher ) ([]string , error ) {
911
+ return d .LabelValuesForLabelNameCommon (ctx , from , to , labelName , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelValuesRequest ) ([]interface {}, error ) {
912
+ return d .ForReplicationSet (ctx , rs , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
913
+ resp , err := client .LabelValues (ctx , req )
914
+ if err != nil {
915
+ return nil , err
916
+ }
917
+ return resp .LabelValues , nil
918
+ })
919
+ }, matchers ... )
920
+ }
921
+
922
+ // LabelValuesForLabelName returns all of the label values that are associated with a given label name.
923
+ func (d * Distributor ) LabelValuesForLabelNameStream (ctx context.Context , from , to model.Time , labelName model.LabelName , matchers ... * labels.Matcher ) ([]string , error ) {
924
+ return d .LabelValuesForLabelNameCommon (ctx , from , to , labelName , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelValuesRequest ) ([]interface {}, error ) {
925
+ return d .ForReplicationSet (ctx , rs , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
926
+ stream , err := client .LabelValuesStream (ctx , req )
927
+ if err != nil {
928
+ return nil , err
929
+ }
930
+ defer stream .CloseSend () //nolint:errcheck
931
+ allLabelValues := []string {}
932
+ for {
933
+ resp , err := stream .Recv ()
934
+
935
+ if err == io .EOF {
936
+ break
937
+ } else if err != nil {
938
+ return nil , err
939
+ }
940
+ allLabelValues = append (allLabelValues , resp .LabelValues ... )
941
+ }
942
+
943
+ return allLabelValues , nil
944
+ })
945
+ }, matchers ... )
946
+ }
947
+
948
+ func (d * Distributor ) LabelNamesCommon (ctx context.Context , from , to model.Time , f func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelNamesRequest ) ([]interface {}, error )) ([]string , error ) {
913
949
replicationSet , err := d .GetIngestersForMetadata (ctx )
914
950
if err != nil {
915
951
return nil , err
@@ -919,16 +955,14 @@ func (d *Distributor) LabelNames(ctx context.Context, from, to model.Time) ([]st
919
955
StartTimestampMs : int64 (from ),
920
956
EndTimestampMs : int64 (to ),
921
957
}
922
- resps , err := d .ForReplicationSet (ctx , replicationSet , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
923
- return client .LabelNames (ctx , req )
924
- })
958
+ resps , err := f (ctx , replicationSet , req )
925
959
if err != nil {
926
960
return nil , err
927
961
}
928
962
929
963
valueSet := map [string ]struct {}{}
930
964
for _ , resp := range resps {
931
- for _ , v := range resp .(* ingester_client. LabelNamesResponse ). LabelNames {
965
+ for _ , v := range resp .([] string ) {
932
966
valueSet [v ] = struct {}{}
933
967
}
934
968
}
@@ -943,8 +977,106 @@ func (d *Distributor) LabelNames(ctx context.Context, from, to model.Time) ([]st
943
977
return values , nil
944
978
}
945
979
980
+ func (d * Distributor ) LabelNamesStream (ctx context.Context , from , to model.Time ) ([]string , error ) {
981
+ return d .LabelNamesCommon (ctx , from , to , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelNamesRequest ) ([]interface {}, error ) {
982
+ return d .ForReplicationSet (ctx , rs , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
983
+ stream , err := client .LabelNamesStream (ctx , req )
984
+ if err != nil {
985
+ return nil , err
986
+ }
987
+ defer stream .CloseSend () //nolint:errcheck
988
+ allLabelNames := []string {}
989
+ for {
990
+ resp , err := stream .Recv ()
991
+
992
+ if err == io .EOF {
993
+ break
994
+ } else if err != nil {
995
+ return nil , err
996
+ }
997
+ allLabelNames = append (allLabelNames , resp .LabelNames ... )
998
+ }
999
+
1000
+ return allLabelNames , nil
1001
+ })
1002
+ })
1003
+ }
1004
+
1005
+ // LabelNames returns all of the label names.
1006
+ func (d * Distributor ) LabelNames (ctx context.Context , from , to model.Time ) ([]string , error ) {
1007
+ return d .LabelNamesCommon (ctx , from , to , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelNamesRequest ) ([]interface {}, error ) {
1008
+ return d .ForReplicationSet (ctx , rs , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
1009
+ resp , err := client .LabelNames (ctx , req )
1010
+ if err != nil {
1011
+ return nil , err
1012
+ }
1013
+ return resp .LabelNames , nil
1014
+ })
1015
+ })
1016
+ }
1017
+
946
1018
// MetricsForLabelMatchers gets the metrics that match said matchers
947
1019
func (d * Distributor ) MetricsForLabelMatchers (ctx context.Context , from , through model.Time , matchers ... * labels.Matcher ) ([]metric.Metric , error ) {
1020
+ return d .metricsForLabelMatchersCommon (ctx , from , through , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.MetricsForLabelMatchersRequest , metrics * map [model.Fingerprint ]model.Metric , mutex * sync.Mutex , queryLimiter * limiter.QueryLimiter ) error {
1021
+ _ , err := d .ForReplicationSet (ctx , rs , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
1022
+ resp , err := client .MetricsForLabelMatchers (ctx , req )
1023
+ if err != nil {
1024
+ return nil , err
1025
+ }
1026
+ ms := ingester_client .FromMetricsForLabelMatchersResponse (resp )
1027
+ for _ , m := range ms {
1028
+ if err := queryLimiter .AddSeries (cortexpb .FromMetricsToLabelAdapters (m )); err != nil {
1029
+ return nil , err
1030
+ }
1031
+ mutex .Lock ()
1032
+ (* metrics )[m .Fingerprint ()] = m
1033
+ mutex .Unlock ()
1034
+ }
1035
+
1036
+ return nil , nil
1037
+ })
1038
+
1039
+ return err
1040
+ }, matchers ... )
1041
+ }
1042
+
1043
+ func (d * Distributor ) MetricsForLabelMatchersStream (ctx context.Context , from , through model.Time , matchers ... * labels.Matcher ) ([]metric.Metric , error ) {
1044
+ return d .metricsForLabelMatchersCommon (ctx , from , through , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.MetricsForLabelMatchersRequest , metrics * map [model.Fingerprint ]model.Metric , mutex * sync.Mutex , queryLimiter * limiter.QueryLimiter ) error {
1045
+ _ , err := d .ForReplicationSet (ctx , rs , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
1046
+ stream , err := client .MetricsForLabelMatchersStream (ctx , req )
1047
+ if err != nil {
1048
+ return nil , err
1049
+ }
1050
+ defer stream .CloseSend () //nolint:errcheck
1051
+ for {
1052
+ resp , err := stream .Recv ()
1053
+
1054
+ if err == io .EOF {
1055
+ break
1056
+ } else if err != nil {
1057
+ return nil , err
1058
+ }
1059
+
1060
+ for _ , metric := range resp .Metric {
1061
+ m := cortexpb .FromLabelAdaptersToMetric (metric .Labels )
1062
+
1063
+ if err := queryLimiter .AddSeries (cortexpb .FromMetricsToLabelAdapters (m )); err != nil {
1064
+ return nil , err
1065
+ }
1066
+ mutex .Lock ()
1067
+ (* metrics )[m .Fingerprint ()] = m
1068
+ mutex .Unlock ()
1069
+ }
1070
+ }
1071
+
1072
+ return nil , nil
1073
+ })
1074
+
1075
+ return err
1076
+ }, matchers ... )
1077
+ }
1078
+
1079
+ func (d * Distributor ) metricsForLabelMatchersCommon (ctx context.Context , from , through model.Time , f func (context.Context , ring.ReplicationSet , * ingester_client.MetricsForLabelMatchersRequest , * map [model.Fingerprint ]model.Metric , * sync.Mutex , * limiter.QueryLimiter ) error , matchers ... * labels.Matcher ) ([]metric.Metric , error ) {
948
1080
replicationSet , err := d .GetIngestersForMetadata (ctx )
949
1081
queryLimiter := limiter .QueryLimiterFromContextWithFallback (ctx )
950
1082
if err != nil {
@@ -958,23 +1090,7 @@ func (d *Distributor) MetricsForLabelMatchers(ctx context.Context, from, through
958
1090
mutex := sync.Mutex {}
959
1091
metrics := map [model.Fingerprint ]model.Metric {}
960
1092
961
- _ , err = d .ForReplicationSet (ctx , replicationSet , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
962
- resp , err := client .MetricsForLabelMatchers (ctx , req )
963
- if err != nil {
964
- return nil , err
965
- }
966
- ms := ingester_client .FromMetricsForLabelMatchersResponse (resp )
967
- for _ , m := range ms {
968
- if err := queryLimiter .AddSeries (cortexpb .FromMetricsToLabelAdapters (m )); err != nil {
969
- return nil , err
970
- }
971
- mutex .Lock ()
972
- metrics [m .Fingerprint ()] = m
973
- mutex .Unlock ()
974
- }
975
-
976
- return resp , nil
977
- })
1093
+ err = f (ctx , replicationSet , req , & metrics , & mutex , queryLimiter )
978
1094
979
1095
if err != nil {
980
1096
return nil , err
0 commit comments