Skip to content

Commit bac7b1f

Browse files
committed
address comment
Signed-off-by: Ben Ye <[email protected]>
1 parent 4436943 commit bac7b1f

File tree

4 files changed

+69
-80
lines changed

4 files changed

+69
-80
lines changed

pkg/frontend/transport/handler.go

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,30 @@ var (
4242
)
4343

4444
const (
45-
reasonRequestBodyTooLarge = "request_body_too_large"
46-
reasonResponseTooLarge = "response_too_large"
47-
reasonTooManyRequests = "too_many_requests"
48-
reasonTooLongRange = "too_long_range"
49-
reasonTooManySamples = "too_many_samples"
50-
reasonSeriesFetched = "series_fetched"
51-
reasonChunksFetched = "chunks_fetched"
52-
reasonChunkBytesFetched = "chunk_bytes_fetched"
53-
reasonDataBytesFetched = "data_bytes_fetched"
54-
reasonSeriesLimitStoreGateway = "series_limit_store_gateway"
55-
reasonChunksLimitStoreGateway = "chunks_limit_store_gateway"
56-
reasonBytesLimitStoreGateway = "bytes_limit_store_gateway"
57-
)
58-
59-
var (
60-
LimitTooManySamples = `query processing would load too many samples into memory`
61-
LimitTooLongRange = `the query time range exceeds the limit`
62-
LimitSeriesFetched = `the query hit the max number of series limit`
63-
LimitChunksFetched = `the query hit the max number of chunks limit`
64-
LimitChunkBytesFetched = `the query hit the aggregated chunks size limit`
65-
LimitDataBytesFetched = `the query hit the aggregated data size limit`
45+
reasonRequestBodySizeExceeded = "request_body_size_exceeded"
46+
reasonResponseBodySizeExceeded = "response_body_size_exceeded"
47+
reasonTooManyRequests = "too_many_requests"
48+
reasonTimeRangeExceeded = "time_range_exceeded"
49+
reasonTooManySamples = "too_many_samples"
50+
reasonSeriesFetched = "series_fetched"
51+
reasonChunksFetched = "chunks_fetched"
52+
reasonChunkBytesFetched = "chunk_bytes_fetched"
53+
reasonDataBytesFetched = "data_bytes_fetched"
54+
reasonSeriesLimitStoreGateway = "store_gateway_series_limit"
55+
reasonChunksLimitStoreGateway = "store_gateway_chunks_limit"
56+
reasonBytesLimitStoreGateway = "store_gateway_bytes_limit"
57+
58+
limitTooManySamples = `query processing would load too many samples into memory`
59+
limitTimeRangeExceeded = `the query time range exceeds the limit`
60+
limitSeriesFetched = `the query hit the max number of series limit`
61+
limitChunksFetched = `the query hit the max number of chunks limit`
62+
limitChunkBytesFetched = `the query hit the aggregated chunks size limit`
63+
limitDataBytesFetched = `the query hit the aggregated data size limit`
6664

6765
// Store gateway limits.
68-
LimitSeriesStoreGateway = `exceeded series limit`
69-
LimitChunksStoreGateway = `exceeded chunks limit`
70-
LimitBytesStoreGateway = `exceeded bytes limit`
66+
limitSeriesStoreGateway = `exceeded series limit`
67+
limitChunksStoreGateway = `exceeded chunks limit`
68+
limitBytesStoreGateway = `exceeded bytes limit`
7169
)
7270

7371
// Config for a Handler.
@@ -91,13 +89,13 @@ type Handler struct {
9189
roundTripper http.RoundTripper
9290

9391
// Metrics.
94-
queriesCount *prometheus.CounterVec
95-
querySeconds *prometheus.CounterVec
96-
querySeries *prometheus.CounterVec
97-
queryChunkBytes *prometheus.CounterVec
98-
queryDataBytes *prometheus.CounterVec
99-
discardedQueries *prometheus.CounterVec
100-
activeUsers *util.ActiveUsersCleanupService
92+
queriesCount *prometheus.CounterVec
93+
querySeconds *prometheus.CounterVec
94+
querySeries *prometheus.CounterVec
95+
queryChunkBytes *prometheus.CounterVec
96+
queryDataBytes *prometheus.CounterVec
97+
rejectedQueries *prometheus.CounterVec
98+
activeUsers *util.ActiveUsersCleanupService
10199
}
102100

103101
// NewHandler creates a new frontend handler.
@@ -134,10 +132,10 @@ func NewHandler(cfg HandlerConfig, roundTripper http.RoundTripper, log log.Logge
134132
Help: "Size of all data fetched to execute a query in bytes.",
135133
}, []string{"user"})
136134

137-
h.discardedQueries = prometheus.NewCounterVec(
135+
h.rejectedQueries = prometheus.NewCounterVec(
138136
prometheus.CounterOpts{
139-
Name: "cortex_discarded_queries_total",
140-
Help: "The total number of queries that were discarded.",
137+
Name: "cortex_rejected_queries_total",
138+
Help: "The total number of queries that were rejected.",
141139
},
142140
[]string{"reason", "user"},
143141
)
@@ -148,8 +146,8 @@ func NewHandler(cfg HandlerConfig, roundTripper http.RoundTripper, log log.Logge
148146
h.querySeries.DeleteLabelValues(user)
149147
h.queryChunkBytes.DeleteLabelValues(user)
150148
h.queryDataBytes.DeleteLabelValues(user)
151-
if err := util.DeleteMatchingLabels(h.discardedQueries, map[string]string{"user": user}); err != nil {
152-
level.Warn(log).Log("msg", "failed to remove cortex_discarded_queries_total metric for user", "user", user, "err", err)
149+
if err := util.DeleteMatchingLabels(h.rejectedQueries, map[string]string{"user": user}); err != nil {
150+
level.Warn(log).Log("msg", "failed to remove cortex_rejected_queries_total metric for user", "user", user, "err", err)
153151
}
154152
})
155153
// If cleaner stops or fail, we will simply not clean the metrics for inactive users.
@@ -198,7 +196,7 @@ func (f *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
198196
if err := r.ParseForm(); err != nil {
199197
writeError(w, err)
200198
if f.cfg.QueryStatsEnabled && util.IsRequestBodyTooLarge(err) {
201-
f.discardedQueries.WithLabelValues(reasonRequestBodyTooLarge, userID).Inc()
199+
f.rejectedQueries.WithLabelValues(reasonRequestBodySizeExceeded, userID).Inc()
202200
}
203201
return
204202
}
@@ -360,35 +358,34 @@ func (f *Handler) reportQueryStats(r *http.Request, userID string, queryString u
360358
}
361359

362360
var reason string
363-
// 413, 422 or 429.
364361
if statusCode == http.StatusTooManyRequests {
365362
reason = reasonTooManyRequests
366363
} else if statusCode == http.StatusRequestEntityTooLarge {
367-
reason = reasonResponseTooLarge
364+
reason = reasonResponseBodySizeExceeded
368365
} else if statusCode == http.StatusUnprocessableEntity {
369366
errMsg := error.Error()
370-
if strings.Contains(errMsg, LimitTooManySamples) {
367+
if strings.Contains(errMsg, limitTooManySamples) {
371368
reason = reasonTooManySamples
372-
} else if strings.Contains(errMsg, LimitTooLongRange) {
373-
reason = reasonTooLongRange
374-
} else if strings.Contains(errMsg, LimitSeriesFetched) {
369+
} else if strings.Contains(errMsg, limitTimeRangeExceeded) {
370+
reason = reasonTimeRangeExceeded
371+
} else if strings.Contains(errMsg, limitSeriesFetched) {
375372
reason = reasonSeriesFetched
376-
} else if strings.Contains(errMsg, LimitChunksFetched) {
373+
} else if strings.Contains(errMsg, limitChunksFetched) {
377374
reason = reasonChunksFetched
378-
} else if strings.Contains(errMsg, LimitChunkBytesFetched) {
375+
} else if strings.Contains(errMsg, limitChunkBytesFetched) {
379376
reason = reasonChunkBytesFetched
380-
} else if strings.Contains(errMsg, LimitDataBytesFetched) {
377+
} else if strings.Contains(errMsg, limitDataBytesFetched) {
381378
reason = reasonDataBytesFetched
382-
} else if strings.Contains(errMsg, LimitSeriesStoreGateway) {
379+
} else if strings.Contains(errMsg, limitSeriesStoreGateway) {
383380
reason = reasonSeriesLimitStoreGateway
384-
} else if strings.Contains(errMsg, LimitChunksStoreGateway) {
381+
} else if strings.Contains(errMsg, limitChunksStoreGateway) {
385382
reason = reasonChunksLimitStoreGateway
386-
} else if strings.Contains(errMsg, LimitBytesStoreGateway) {
383+
} else if strings.Contains(errMsg, limitBytesStoreGateway) {
387384
reason = reasonBytesLimitStoreGateway
388385
}
389386
}
390387
if len(reason) > 0 {
391-
f.discardedQueries.WithLabelValues(reason, userID).Inc()
388+
f.rejectedQueries.WithLabelValues(reason, userID).Inc()
392389
}
393390
}
394391

pkg/frontend/transport/handler_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestHandler_ServeHTTP(t *testing.T) {
8181
}, nil
8282
}),
8383
additionalMetricsCheckFunc: func(h *Handler) {
84-
v := promtest.ToFloat64(h.discardedQueries.WithLabelValues(reasonResponseTooLarge, userID))
84+
v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonRequestBodySizeExceeded, userID))
8585
assert.Equal(t, float64(1), v)
8686
},
8787
},
@@ -96,7 +96,7 @@ func TestHandler_ServeHTTP(t *testing.T) {
9696
}, nil
9797
}),
9898
additionalMetricsCheckFunc: func(h *Handler) {
99-
v := promtest.ToFloat64(h.discardedQueries.WithLabelValues(reasonTooManyRequests, userID))
99+
v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonTooManyRequests, userID))
100100
assert.Equal(t, float64(1), v)
101101
},
102102
},
@@ -107,11 +107,11 @@ func TestHandler_ServeHTTP(t *testing.T) {
107107
roundTripperFunc: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
108108
return &http.Response{
109109
StatusCode: http.StatusUnprocessableEntity,
110-
Body: io.NopCloser(strings.NewReader(LimitTooManySamples)),
110+
Body: io.NopCloser(strings.NewReader(limitTooManySamples)),
111111
}, nil
112112
}),
113113
additionalMetricsCheckFunc: func(h *Handler) {
114-
v := promtest.ToFloat64(h.discardedQueries.WithLabelValues(reasonTooManySamples, userID))
114+
v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonTooManySamples, userID))
115115
assert.Equal(t, float64(1), v)
116116
},
117117
},
@@ -122,11 +122,11 @@ func TestHandler_ServeHTTP(t *testing.T) {
122122
roundTripperFunc: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
123123
return &http.Response{
124124
StatusCode: http.StatusUnprocessableEntity,
125-
Body: io.NopCloser(strings.NewReader(LimitTooLongRange)),
125+
Body: io.NopCloser(strings.NewReader(limitTimeRangeExceeded)),
126126
}, nil
127127
}),
128128
additionalMetricsCheckFunc: func(h *Handler) {
129-
v := promtest.ToFloat64(h.discardedQueries.WithLabelValues(reasonTooLongRange, userID))
129+
v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonTimeRangeExceeded, userID))
130130
assert.Equal(t, float64(1), v)
131131
},
132132
},
@@ -137,11 +137,11 @@ func TestHandler_ServeHTTP(t *testing.T) {
137137
roundTripperFunc: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
138138
return &http.Response{
139139
StatusCode: http.StatusUnprocessableEntity,
140-
Body: io.NopCloser(strings.NewReader(LimitSeriesFetched)),
140+
Body: io.NopCloser(strings.NewReader(limitSeriesFetched)),
141141
}, nil
142142
}),
143143
additionalMetricsCheckFunc: func(h *Handler) {
144-
v := promtest.ToFloat64(h.discardedQueries.WithLabelValues(reasonSeriesFetched, userID))
144+
v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonSeriesFetched, userID))
145145
assert.Equal(t, float64(1), v)
146146
},
147147
},
@@ -152,11 +152,11 @@ func TestHandler_ServeHTTP(t *testing.T) {
152152
roundTripperFunc: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
153153
return &http.Response{
154154
StatusCode: http.StatusUnprocessableEntity,
155-
Body: io.NopCloser(strings.NewReader(LimitChunksFetched)),
155+
Body: io.NopCloser(strings.NewReader(limitChunksFetched)),
156156
}, nil
157157
}),
158158
additionalMetricsCheckFunc: func(h *Handler) {
159-
v := promtest.ToFloat64(h.discardedQueries.WithLabelValues(reasonChunksFetched, userID))
159+
v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonChunksFetched, userID))
160160
assert.Equal(t, float64(1), v)
161161
},
162162
},
@@ -167,11 +167,11 @@ func TestHandler_ServeHTTP(t *testing.T) {
167167
roundTripperFunc: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
168168
return &http.Response{
169169
StatusCode: http.StatusUnprocessableEntity,
170-
Body: io.NopCloser(strings.NewReader(LimitChunkBytesFetched)),
170+
Body: io.NopCloser(strings.NewReader(limitChunkBytesFetched)),
171171
}, nil
172172
}),
173173
additionalMetricsCheckFunc: func(h *Handler) {
174-
v := promtest.ToFloat64(h.discardedQueries.WithLabelValues(reasonChunkBytesFetched, userID))
174+
v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonChunkBytesFetched, userID))
175175
assert.Equal(t, float64(1), v)
176176
},
177177
},
@@ -182,11 +182,11 @@ func TestHandler_ServeHTTP(t *testing.T) {
182182
roundTripperFunc: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
183183
return &http.Response{
184184
StatusCode: http.StatusUnprocessableEntity,
185-
Body: io.NopCloser(strings.NewReader(LimitDataBytesFetched)),
185+
Body: io.NopCloser(strings.NewReader(limitDataBytesFetched)),
186186
}, nil
187187
}),
188188
additionalMetricsCheckFunc: func(h *Handler) {
189-
v := promtest.ToFloat64(h.discardedQueries.WithLabelValues(reasonDataBytesFetched, userID))
189+
v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonDataBytesFetched, userID))
190190
assert.Equal(t, float64(1), v)
191191
},
192192
},
@@ -197,11 +197,11 @@ func TestHandler_ServeHTTP(t *testing.T) {
197197
roundTripperFunc: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
198198
return &http.Response{
199199
StatusCode: http.StatusUnprocessableEntity,
200-
Body: io.NopCloser(strings.NewReader(LimitSeriesStoreGateway)),
200+
Body: io.NopCloser(strings.NewReader(limitSeriesStoreGateway)),
201201
}, nil
202202
}),
203203
additionalMetricsCheckFunc: func(h *Handler) {
204-
v := promtest.ToFloat64(h.discardedQueries.WithLabelValues(reasonSeriesLimitStoreGateway, userID))
204+
v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonSeriesLimitStoreGateway, userID))
205205
assert.Equal(t, float64(1), v)
206206
},
207207
},
@@ -212,11 +212,11 @@ func TestHandler_ServeHTTP(t *testing.T) {
212212
roundTripperFunc: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
213213
return &http.Response{
214214
StatusCode: http.StatusUnprocessableEntity,
215-
Body: io.NopCloser(strings.NewReader(LimitChunksStoreGateway)),
215+
Body: io.NopCloser(strings.NewReader(limitChunksStoreGateway)),
216216
}, nil
217217
}),
218218
additionalMetricsCheckFunc: func(h *Handler) {
219-
v := promtest.ToFloat64(h.discardedQueries.WithLabelValues(reasonChunksLimitStoreGateway, userID))
219+
v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonChunksLimitStoreGateway, userID))
220220
assert.Equal(t, float64(1), v)
221221
},
222222
},
@@ -227,11 +227,11 @@ func TestHandler_ServeHTTP(t *testing.T) {
227227
roundTripperFunc: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
228228
return &http.Response{
229229
StatusCode: http.StatusUnprocessableEntity,
230-
Body: io.NopCloser(strings.NewReader(LimitBytesStoreGateway)),
230+
Body: io.NopCloser(strings.NewReader(limitBytesStoreGateway)),
231231
}, nil
232232
}),
233233
additionalMetricsCheckFunc: func(h *Handler) {
234-
v := promtest.ToFloat64(h.discardedQueries.WithLabelValues(reasonBytesLimitStoreGateway, userID))
234+
v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonBytesLimitStoreGateway, userID))
235235
assert.Equal(t, float64(1), v)
236236
},
237237
},

pkg/querier/stats/stats.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ message Stats {
2626
uint64 fetched_chunks_count = 6;
2727
// The number of samples fetched for the query
2828
uint64 fetched_samples_count = 7;
29+
// The limit hit when executing the query
30+
string limit_hit = 8 [(gogoproto.nullable) = true];
2931
}

pkg/util/validation/validate.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,10 @@ var DiscardedMetadata = prometheus.NewCounterVec(
9595
[]string{discardReasonLabel, "user"},
9696
)
9797

98-
// DiscardedQueries is a metric of the number of discarded queries, by reason.
99-
var DiscardedQueries = prometheus.NewCounterVec(
100-
prometheus.CounterOpts{
101-
Name: "cortex_discarded_query_total",
102-
Help: "The total number of queries that were discarded.",
103-
},
104-
[]string{discardReasonLabel, "user"},
105-
)
106-
10798
func init() {
10899
prometheus.MustRegister(DiscardedSamples)
109100
prometheus.MustRegister(DiscardedExemplars)
110101
prometheus.MustRegister(DiscardedMetadata)
111-
prometheus.MustRegister(DiscardedQueries)
112102
}
113103

114104
// ValidateSample returns an err if the sample is invalid.

0 commit comments

Comments
 (0)