@@ -21,6 +21,7 @@ import (
2121 "github.com/prometheus/prometheus/model/labels"
2222 "github.com/prometheus/prometheus/model/relabel"
2323 "github.com/prometheus/prometheus/scrape"
24+ "github.com/prometheus/prometheus/storage"
2425 "github.com/weaveworks/common/httpgrpc"
2526 "github.com/weaveworks/common/instrument"
2627 "github.com/weaveworks/common/user"
@@ -671,7 +672,7 @@ func (d *Distributor) Push(ctx context.Context, req *cortexpb.WriteRequest) (*co
671672 return nil , err
672673 }
673674 // If there wasn't an error but removeReplica is false that means we didn't find both HA labels.
674- if ! removeReplica {
675+ if ! removeReplica { // False, Nil
675676 d .nonHASamples .WithLabelValues (userID ).Add (float64 (numFloatSamples + numHistogramSamples ))
676677 }
677678 }
@@ -1021,7 +1022,7 @@ func (d *Distributor) ForReplicationSet(ctx context.Context, replicationSet ring
10211022 })
10221023}
10231024
1024- 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 ) {
1025+ func (d * Distributor ) LabelValuesForLabelNameCommon (ctx context.Context , from , to model.Time , labelName model.LabelName , hints * storage. LabelHints , f func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelValuesRequest ) ([]interface {}, error ), matchers ... * labels.Matcher ) ([]string , error ) {
10251026 span , ctx := opentracing .StartSpanFromContext (ctx , "Distributor.LabelValues" , opentracing.Tags {
10261027 "name" : labelName ,
10271028 "start" : from .Unix (),
@@ -1032,8 +1033,8 @@ func (d *Distributor) LabelValuesForLabelNameCommon(ctx context.Context, from, t
10321033 if err != nil {
10331034 return nil , err
10341035 }
1035-
1036- req , err := ingester_client .ToLabelValuesRequest (labelName , from , to , matchers )
1036+ limit := getLimitFromLabelHints ( hints )
1037+ req , err := ingester_client .ToLabelValuesRequest (labelName , from , to , limit , matchers )
10371038 if err != nil {
10381039 return nil , err
10391040 }
@@ -1053,13 +1054,16 @@ func (d *Distributor) LabelValuesForLabelNameCommon(ctx context.Context, from, t
10531054 if err != nil {
10541055 return nil , err
10551056 }
1057+ if limit > 0 && len (r ) > limit {
1058+ r = r [:limit ]
1059+ }
10561060 span .SetTag ("result_length" , len (r ))
10571061 return r , nil
10581062}
10591063
10601064// LabelValuesForLabelName returns all the label values that are associated with a given label name.
1061- func (d * Distributor ) LabelValuesForLabelName (ctx context.Context , from , to model.Time , labelName model.LabelName , matchers ... * labels.Matcher ) ([]string , error ) {
1062- return d .LabelValuesForLabelNameCommon (ctx , from , to , labelName , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelValuesRequest ) ([]interface {}, error ) {
1065+ func (d * Distributor ) LabelValuesForLabelName (ctx context.Context , from , to model.Time , labelName model.LabelName , hint * storage. LabelHints , matchers ... * labels.Matcher ) ([]string , error ) {
1066+ return d .LabelValuesForLabelNameCommon (ctx , from , to , labelName , hint , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelValuesRequest ) ([]interface {}, error ) {
10631067 return d .ForReplicationSet (ctx , rs , d .cfg .ZoneResultsQuorumMetadata , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
10641068 resp , err := client .LabelValues (ctx , req )
10651069 if err != nil {
@@ -1071,8 +1075,8 @@ func (d *Distributor) LabelValuesForLabelName(ctx context.Context, from, to mode
10711075}
10721076
10731077// LabelValuesForLabelNameStream returns all the label values that are associated with a given label name.
1074- func (d * Distributor ) LabelValuesForLabelNameStream (ctx context.Context , from , to model.Time , labelName model.LabelName , matchers ... * labels.Matcher ) ([]string , error ) {
1075- return d .LabelValuesForLabelNameCommon (ctx , from , to , labelName , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelValuesRequest ) ([]interface {}, error ) {
1078+ func (d * Distributor ) LabelValuesForLabelNameStream (ctx context.Context , from , to model.Time , labelName model.LabelName , hint * storage. LabelHints , matchers ... * labels.Matcher ) ([]string , error ) {
1079+ return d .LabelValuesForLabelNameCommon (ctx , from , to , labelName , hint , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelValuesRequest ) ([]interface {}, error ) {
10761080 return d .ForReplicationSet (ctx , rs , d .cfg .ZoneResultsQuorumMetadata , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
10771081 stream , err := client .LabelValuesStream (ctx , req )
10781082 if err != nil {
@@ -1096,7 +1100,7 @@ func (d *Distributor) LabelValuesForLabelNameStream(ctx context.Context, from, t
10961100 }, matchers ... )
10971101}
10981102
1099- 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 ) {
1103+ func (d * Distributor ) LabelNamesCommon (ctx context.Context , from , to model.Time , hints * storage. LabelHints , f func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelNamesRequest ) ([]interface {}, error )) ([]string , error ) {
11001104 span , ctx := opentracing .StartSpanFromContext (ctx , "Distributor.LabelNames" , opentracing.Tags {
11011105 "start" : from .Unix (),
11021106 "end" : to .Unix (),
@@ -1107,9 +1111,11 @@ func (d *Distributor) LabelNamesCommon(ctx context.Context, from, to model.Time,
11071111 return nil , err
11081112 }
11091113
1114+ limit := getLimitFromLabelHints (hints )
11101115 req := & ingester_client.LabelNamesRequest {
11111116 StartTimestampMs : int64 (from ),
11121117 EndTimestampMs : int64 (to ),
1118+ Limit : int64 (limit ),
11131119 }
11141120 resps , err := f (ctx , replicationSet , req )
11151121 if err != nil {
@@ -1126,13 +1132,17 @@ func (d *Distributor) LabelNamesCommon(ctx context.Context, from, to model.Time,
11261132 if err != nil {
11271133 return nil , err
11281134 }
1135+ if limit > 0 && len (r ) > limit {
1136+ r = r [:limit ]
1137+ }
1138+
11291139 span .SetTag ("result_length" , len (r ))
11301140
11311141 return r , nil
11321142}
11331143
1134- func (d * Distributor ) LabelNamesStream (ctx context.Context , from , to model.Time ) ([]string , error ) {
1135- return d .LabelNamesCommon (ctx , from , to , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelNamesRequest ) ([]interface {}, error ) {
1144+ func (d * Distributor ) LabelNamesStream (ctx context.Context , from , to model.Time , hints * storage. LabelHints ) ([]string , error ) {
1145+ return d .LabelNamesCommon (ctx , from , to , hints , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelNamesRequest ) ([]interface {}, error ) {
11361146 return d .ForReplicationSet (ctx , rs , d .cfg .ZoneResultsQuorumMetadata , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
11371147 stream , err := client .LabelNamesStream (ctx , req )
11381148 if err != nil {
@@ -1157,8 +1167,8 @@ func (d *Distributor) LabelNamesStream(ctx context.Context, from, to model.Time)
11571167}
11581168
11591169// LabelNames returns all the label names.
1160- func (d * Distributor ) LabelNames (ctx context.Context , from , to model.Time ) ([]string , error ) {
1161- return d .LabelNamesCommon (ctx , from , to , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelNamesRequest ) ([]interface {}, error ) {
1170+ func (d * Distributor ) LabelNames (ctx context.Context , from , to model.Time , hint * storage. LabelHints ) ([]string , error ) {
1171+ return d .LabelNamesCommon (ctx , from , to , hint , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.LabelNamesRequest ) ([]interface {}, error ) {
11621172 return d .ForReplicationSet (ctx , rs , d .cfg .ZoneResultsQuorumMetadata , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
11631173 resp , err := client .LabelNames (ctx , req )
11641174 if err != nil {
@@ -1170,8 +1180,8 @@ func (d *Distributor) LabelNames(ctx context.Context, from, to model.Time) ([]st
11701180}
11711181
11721182// MetricsForLabelMatchers gets the metrics that match said matchers
1173- func (d * Distributor ) MetricsForLabelMatchers (ctx context.Context , from , through model.Time , matchers ... * labels.Matcher ) ([]model.Metric , error ) {
1174- 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 {
1183+ func (d * Distributor ) MetricsForLabelMatchers (ctx context.Context , from , through model.Time , hint * storage. SelectHints , matchers ... * labels.Matcher ) ([]model.Metric , error ) {
1184+ return d .metricsForLabelMatchersCommon (ctx , from , through , hint , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.MetricsForLabelMatchersRequest , metrics * map [model.Fingerprint ]model.Metric , mutex * sync.Mutex , queryLimiter * limiter.QueryLimiter ) error {
11751185 _ , err := d .ForReplicationSet (ctx , rs , false , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
11761186 resp , err := client .MetricsForLabelMatchers (ctx , req )
11771187 if err != nil {
@@ -1199,8 +1209,8 @@ func (d *Distributor) MetricsForLabelMatchers(ctx context.Context, from, through
11991209 }, matchers ... )
12001210}
12011211
1202- func (d * Distributor ) MetricsForLabelMatchersStream (ctx context.Context , from , through model.Time , matchers ... * labels.Matcher ) ([]model.Metric , error ) {
1203- 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 {
1212+ func (d * Distributor ) MetricsForLabelMatchersStream (ctx context.Context , from , through model.Time , hint * storage. SelectHints , matchers ... * labels.Matcher ) ([]model.Metric , error ) {
1213+ return d .metricsForLabelMatchersCommon (ctx , from , through , hint , func (ctx context.Context , rs ring.ReplicationSet , req * ingester_client.MetricsForLabelMatchersRequest , metrics * map [model.Fingerprint ]model.Metric , mutex * sync.Mutex , queryLimiter * limiter.QueryLimiter ) error {
12041214 _ , err := d .ForReplicationSet (ctx , rs , false , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
12051215 stream , err := client .MetricsForLabelMatchersStream (ctx , req )
12061216 if err != nil {
@@ -1239,14 +1249,14 @@ func (d *Distributor) MetricsForLabelMatchersStream(ctx context.Context, from, t
12391249 }, matchers ... )
12401250}
12411251
1242- 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 ) ([]model.Metric , error ) {
1252+ func (d * Distributor ) metricsForLabelMatchersCommon (ctx context.Context , from , through model.Time , hints * storage. SelectHints , f func (context.Context , ring.ReplicationSet , * ingester_client.MetricsForLabelMatchersRequest , * map [model.Fingerprint ]model.Metric , * sync.Mutex , * limiter.QueryLimiter ) error , matchers ... * labels.Matcher ) ([]model.Metric , error ) {
12431253 replicationSet , err := d .GetIngestersForMetadata (ctx )
12441254 queryLimiter := limiter .QueryLimiterFromContextWithFallback (ctx )
12451255 if err != nil {
12461256 return nil , err
12471257 }
12481258
1249- req , err := ingester_client .ToMetricsForLabelMatchersRequest (from , through , matchers )
1259+ req , err := ingester_client .ToMetricsForLabelMatchersRequest (from , through , getLimitFromSelectHints ( hints ), matchers )
12501260 if err != nil {
12511261 return nil , err
12521262 }
@@ -1438,3 +1448,17 @@ func findHALabels(replicaLabel, clusterLabel string, labels []cortexpb.LabelAdap
14381448
14391449 return cluster , replica
14401450}
1451+
1452+ func getLimitFromLabelHints (hints * storage.LabelHints ) int {
1453+ if hints != nil {
1454+ return hints .Limit
1455+ }
1456+ return 0
1457+ }
1458+
1459+ func getLimitFromSelectHints (hints * storage.SelectHints ) int {
1460+ if hints != nil {
1461+ return hints .Limit
1462+ }
1463+ return 0
1464+ }
0 commit comments