MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestHandlerMetricsCleanup

Function TestHandlerMetricsCleanup

pkg/frontend/transport/handler_test.go:730–843  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

728}
729
730func TestHandlerMetricsCleanup(t *testing.T) {
731 reg := prometheus.NewPedanticRegistry()
732 handler := NewHandler(HandlerConfig{QueryStatsEnabled: true}, tenantfederation.Config{}, http.DefaultTransport, log.NewNopLogger(), reg)
733
734 user1 := "user1"
735 user2 := "user2"
736 source := "api"
737
738 // Simulate activity for user1
739 handler.querySeconds.WithLabelValues(source, user1).Add(1.0)
740 handler.queryFetchedSeries.WithLabelValues(source, user1).Add(100)
741 handler.queryFetchedSamples.WithLabelValues(source, user1).Add(1000)
742 handler.queryScannedSamples.WithLabelValues(source, user1).Add(2000)
743 handler.queryPeakSamples.WithLabelValues(source, user1).Observe(500)
744 handler.queryChunkBytes.WithLabelValues(source, user1).Add(1024)
745 handler.queryDataBytes.WithLabelValues(source, user1).Add(2048)
746 handler.rejectedQueries.WithLabelValues(reasonTooManySamples, source, user1).Add(5)
747 handler.getOrCreateSlowQueryMetric().WithLabelValues(source, user1).Add(5)
748
749 // Simulate activity for user2
750 handler.querySeconds.WithLabelValues(source, user2).Add(2.0)
751 handler.queryFetchedSeries.WithLabelValues(source, user2).Add(200)
752 handler.queryFetchedSamples.WithLabelValues(source, user2).Add(2000)
753 handler.queryScannedSamples.WithLabelValues(source, user2).Add(4000)
754 handler.queryPeakSamples.WithLabelValues(source, user2).Observe(1000)
755 handler.queryChunkBytes.WithLabelValues(source, user2).Add(2048)
756 handler.queryDataBytes.WithLabelValues(source, user2).Add(4096)
757 handler.rejectedQueries.WithLabelValues(reasonTooManySamples, source, user2).Add(10)
758 handler.getOrCreateSlowQueryMetric().WithLabelValues(source, user2).Add(10)
759
760 // Verify initial state - both users should have metrics
761 require.NoError(t, promtest.GatherAndCompare(reg, strings.NewReader(`
762 # HELP cortex_query_seconds_total Total amount of wall clock time spend processing queries.
763 # TYPE cortex_query_seconds_total counter
764 cortex_query_seconds_total{source="api",user="user1"} 1
765 cortex_query_seconds_total{source="api",user="user2"} 2
766 # HELP cortex_query_fetched_series_total Number of series fetched to execute a query.
767 # TYPE cortex_query_fetched_series_total counter
768 cortex_query_fetched_series_total{source="api",user="user1"} 100
769 cortex_query_fetched_series_total{source="api",user="user2"} 200
770 # HELP cortex_query_samples_total Number of samples fetched to execute a query.
771 # TYPE cortex_query_samples_total counter
772 cortex_query_samples_total{source="api",user="user1"} 1000
773 cortex_query_samples_total{source="api",user="user2"} 2000
774 # HELP cortex_query_samples_scanned_total Number of samples scanned to execute a query.
775 # TYPE cortex_query_samples_scanned_total counter
776 cortex_query_samples_scanned_total{source="api",user="user1"} 2000
777 cortex_query_samples_scanned_total{source="api",user="user2"} 4000
778 # HELP cortex_query_peak_samples Highest count of samples considered to execute a query.
779 # TYPE cortex_query_peak_samples histogram
780 cortex_query_peak_samples_bucket{source="api",user="user1",le="+Inf"} 1
781 cortex_query_peak_samples_sum{source="api",user="user1"} 500
782 cortex_query_peak_samples_count{source="api",user="user1"} 1
783 cortex_query_peak_samples_bucket{source="api",user="user2",le="+Inf"} 1
784 cortex_query_peak_samples_sum{source="api",user="user2"} 1000
785 cortex_query_peak_samples_count{source="api",user="user2"} 1
786 # HELP cortex_query_fetched_chunks_bytes_total Size of all chunks fetched to execute a query in bytes.
787 # TYPE cortex_query_fetched_chunks_bytes_total counter

Callers

nothing calls this directly

Calls 4

NewHandlerFunction · 0.85
AddMethod · 0.45

Tested by

no test coverage detected