@@ -21,6 +21,7 @@ import (
21
21
"github.com/prometheus/prometheus/model/labels"
22
22
"github.com/prometheus/prometheus/model/relabel"
23
23
"github.com/prometheus/prometheus/scrape"
24
+ "github.com/prometheus/prometheus/storage"
24
25
"github.com/weaveworks/common/httpgrpc"
25
26
"github.com/weaveworks/common/instrument"
26
27
"github.com/weaveworks/common/user"
@@ -671,7 +672,7 @@ func (d *Distributor) Push(ctx context.Context, req *cortexpb.WriteRequest) (*co
671
672
return nil , err
672
673
}
673
674
// 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
675
676
d .nonHASamples .WithLabelValues (userID ).Add (float64 (numFloatSamples + numHistogramSamples ))
676
677
}
677
678
}
@@ -1021,7 +1022,7 @@ func (d *Distributor) ForReplicationSet(ctx context.Context, replicationSet ring
1021
1022
})
1022
1023
}
1023
1024
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 ) {
1025
1026
span , ctx := opentracing .StartSpanFromContext (ctx , "Distributor.LabelValues" , opentracing.Tags {
1026
1027
"name" : labelName ,
1027
1028
"start" : from .Unix (),
@@ -1032,8 +1033,8 @@ func (d *Distributor) LabelValuesForLabelNameCommon(ctx context.Context, from, t
1032
1033
if err != nil {
1033
1034
return nil , err
1034
1035
}
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 )
1037
1038
if err != nil {
1038
1039
return nil , err
1039
1040
}
@@ -1053,13 +1054,16 @@ func (d *Distributor) LabelValuesForLabelNameCommon(ctx context.Context, from, t
1053
1054
if err != nil {
1054
1055
return nil , err
1055
1056
}
1057
+ if limit > 0 && len (r ) > limit {
1058
+ r = r [:limit ]
1059
+ }
1056
1060
span .SetTag ("result_length" , len (r ))
1057
1061
return r , nil
1058
1062
}
1059
1063
1060
1064
// 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 ) {
1063
1067
return d .ForReplicationSet (ctx , rs , d .cfg .ZoneResultsQuorumMetadata , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
1064
1068
resp , err := client .LabelValues (ctx , req )
1065
1069
if err != nil {
@@ -1071,8 +1075,8 @@ func (d *Distributor) LabelValuesForLabelName(ctx context.Context, from, to mode
1071
1075
}
1072
1076
1073
1077
// 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 ) {
1076
1080
return d .ForReplicationSet (ctx , rs , d .cfg .ZoneResultsQuorumMetadata , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
1077
1081
stream , err := client .LabelValuesStream (ctx , req )
1078
1082
if err != nil {
@@ -1096,7 +1100,7 @@ func (d *Distributor) LabelValuesForLabelNameStream(ctx context.Context, from, t
1096
1100
}, matchers ... )
1097
1101
}
1098
1102
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 ) {
1100
1104
span , ctx := opentracing .StartSpanFromContext (ctx , "Distributor.LabelNames" , opentracing.Tags {
1101
1105
"start" : from .Unix (),
1102
1106
"end" : to .Unix (),
@@ -1107,9 +1111,11 @@ func (d *Distributor) LabelNamesCommon(ctx context.Context, from, to model.Time,
1107
1111
return nil , err
1108
1112
}
1109
1113
1114
+ limit := getLimitFromLabelHints (hints )
1110
1115
req := & ingester_client.LabelNamesRequest {
1111
1116
StartTimestampMs : int64 (from ),
1112
1117
EndTimestampMs : int64 (to ),
1118
+ Limit : int64 (limit ),
1113
1119
}
1114
1120
resps , err := f (ctx , replicationSet , req )
1115
1121
if err != nil {
@@ -1126,13 +1132,17 @@ func (d *Distributor) LabelNamesCommon(ctx context.Context, from, to model.Time,
1126
1132
if err != nil {
1127
1133
return nil , err
1128
1134
}
1135
+ if limit > 0 && len (r ) > limit {
1136
+ r = r [:limit ]
1137
+ }
1138
+
1129
1139
span .SetTag ("result_length" , len (r ))
1130
1140
1131
1141
return r , nil
1132
1142
}
1133
1143
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 ) {
1136
1146
return d .ForReplicationSet (ctx , rs , d .cfg .ZoneResultsQuorumMetadata , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
1137
1147
stream , err := client .LabelNamesStream (ctx , req )
1138
1148
if err != nil {
@@ -1157,8 +1167,8 @@ func (d *Distributor) LabelNamesStream(ctx context.Context, from, to model.Time)
1157
1167
}
1158
1168
1159
1169
// 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 ) {
1162
1172
return d .ForReplicationSet (ctx , rs , d .cfg .ZoneResultsQuorumMetadata , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
1163
1173
resp , err := client .LabelNames (ctx , req )
1164
1174
if err != nil {
@@ -1170,8 +1180,8 @@ func (d *Distributor) LabelNames(ctx context.Context, from, to model.Time) ([]st
1170
1180
}
1171
1181
1172
1182
// 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 {
1175
1185
_ , err := d .ForReplicationSet (ctx , rs , false , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
1176
1186
resp , err := client .MetricsForLabelMatchers (ctx , req )
1177
1187
if err != nil {
@@ -1199,8 +1209,8 @@ func (d *Distributor) MetricsForLabelMatchers(ctx context.Context, from, through
1199
1209
}, matchers ... )
1200
1210
}
1201
1211
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 {
1204
1214
_ , err := d .ForReplicationSet (ctx , rs , false , func (ctx context.Context , client ingester_client.IngesterClient ) (interface {}, error ) {
1205
1215
stream , err := client .MetricsForLabelMatchersStream (ctx , req )
1206
1216
if err != nil {
@@ -1239,14 +1249,14 @@ func (d *Distributor) MetricsForLabelMatchersStream(ctx context.Context, from, t
1239
1249
}, matchers ... )
1240
1250
}
1241
1251
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 ) {
1243
1253
replicationSet , err := d .GetIngestersForMetadata (ctx )
1244
1254
queryLimiter := limiter .QueryLimiterFromContextWithFallback (ctx )
1245
1255
if err != nil {
1246
1256
return nil , err
1247
1257
}
1248
1258
1249
- req , err := ingester_client .ToMetricsForLabelMatchersRequest (from , through , matchers )
1259
+ req , err := ingester_client .ToMetricsForLabelMatchersRequest (from , through , getLimitFromSelectHints ( hints ), matchers )
1250
1260
if err != nil {
1251
1261
return nil , err
1252
1262
}
@@ -1438,3 +1448,17 @@ func findHALabels(replicaLabel, clusterLabel string, labels []cortexpb.LabelAdap
1438
1448
1439
1449
return cluster , replica
1440
1450
}
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