Skip to content

Commit

Permalink
Cleanup dangling request queue metrics (#6433)
Browse files Browse the repository at this point in the history
* cleanup dangling request queue metrics

Signed-off-by: Ben Ye <[email protected]>

* fix lint

Signed-off-by: Ben Ye <[email protected]>

---------

Signed-off-by: Ben Ye <[email protected]>
  • Loading branch information
yeya24 authored Dec 17, 2024
1 parent b72a536 commit 3269f9d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/frontend/v1/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func (f *Frontend) cleanupInactiveUserMetrics(user string) {
f.discardedRequests.DeletePartialMatch(prometheus.Labels{
"user": user,
})
f.requestQueue.CleanupInactiveUserMetrics(user)
}

// RoundTripGRPC round trips a proto (instead of a HTTP request).
Expand Down
7 changes: 5 additions & 2 deletions pkg/frontend/v1/frontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,14 @@ func TestFrontendMetricsCleanup(t *testing.T) {
# HELP cortex_query_frontend_queue_length Number of queries in the queue.
# TYPE cortex_query_frontend_queue_length gauge
cortex_query_frontend_queue_length{priority="0",type="fifo",user="1"} 0
`), "cortex_query_frontend_queue_length"))
# HELP cortex_request_queue_requests_total Total number of query requests going to the request queue.
# TYPE cortex_request_queue_requests_total counter
cortex_request_queue_requests_total{priority="0",user="1"} 1
`), "cortex_query_frontend_queue_length", "cortex_request_queue_requests_total"))

fr.cleanupInactiveUserMetrics("1")

require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(""), "cortex_query_frontend_queue_length"))
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(""), "cortex_query_frontend_queue_length", "cortex_request_queue_requests_total"))
}

testFrontend(t, defaultFrontendConfig(), handler, test, matchMaxConcurrency, nil, reg)
Expand Down
4 changes: 4 additions & 0 deletions pkg/scheduler/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,7 @@ func (q *RequestQueue) QuerierDisconnecting() {
func (q *RequestQueue) GetConnectedQuerierWorkersMetric() float64 {
return float64(q.connectedQuerierWorkers.Load())
}

func (q *RequestQueue) CleanupInactiveUserMetrics(user string) {
q.totalRequests.DeletePartialMatch(prometheus.Labels{"user": user})
}
1 change: 1 addition & 0 deletions pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ func (s *Scheduler) cleanupMetricsForInactiveUser(user string) {
s.discardedRequests.DeletePartialMatch(prometheus.Labels{
"user": user,
})
s.requestQueue.CleanupInactiveUserMetrics(user)
}

func (s *Scheduler) getConnectedFrontendClientsMetric() float64 {
Expand Down
11 changes: 9 additions & 2 deletions pkg/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,22 @@ func TestSchedulerMetrics(t *testing.T) {
# TYPE cortex_query_scheduler_queue_length gauge
cortex_query_scheduler_queue_length{priority="0",type="fifo",user="another"} 1
cortex_query_scheduler_queue_length{priority="0",type="fifo",user="test"} 1
`), "cortex_query_scheduler_queue_length"))
# HELP cortex_request_queue_requests_total Total number of query requests going to the request queue.
# TYPE cortex_request_queue_requests_total counter
cortex_request_queue_requests_total{priority="0",user="another"} 1
cortex_request_queue_requests_total{priority="0",user="test"} 1
`), "cortex_query_scheduler_queue_length", "cortex_request_queue_requests_total"))

scheduler.cleanupMetricsForInactiveUser("test")

require.NoError(t, promtest.GatherAndCompare(reg, strings.NewReader(`
# HELP cortex_query_scheduler_queue_length Number of queries in the queue.
# TYPE cortex_query_scheduler_queue_length gauge
cortex_query_scheduler_queue_length{priority="0",type="fifo",user="another"} 1
`), "cortex_query_scheduler_queue_length"))
# HELP cortex_request_queue_requests_total Total number of query requests going to the request queue.
# TYPE cortex_request_queue_requests_total counter
cortex_request_queue_requests_total{priority="0",user="another"} 1
`), "cortex_query_scheduler_queue_length", "cortex_request_queue_requests_total"))
}

func initFrontendLoop(t *testing.T, client schedulerpb.SchedulerForFrontendClient, frontendAddr string) schedulerpb.SchedulerForFrontend_FrontendLoopClient {
Expand Down

0 comments on commit 3269f9d

Please sign in to comment.