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

Function Test_Ingester_AllUserStatsHandler

pkg/ingester/ingester_test.go:5452–5528  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

5450}
5451
5452func Test_Ingester_AllUserStatsHandler(t *testing.T) {
5453 series := []struct {
5454 user string
5455 lbls labels.Labels
5456 value float64
5457 timestamp int64
5458 }{
5459 {"user-1", labels.FromStrings("__name__", "test_1_1", "route", "get_user", "status", "200"), 1, 100000},
5460 {"user-1", labels.FromStrings("__name__", "test_1_1", "route", "get_user", "status", "500"), 1, 110000},
5461 {"user-1", labels.FromStrings("__name__", "test_1_2"), 2, 200000},
5462 {"user-2", labels.FromStrings("__name__", "test_2_1"), 2, 200000},
5463 {"user-2", labels.FromStrings("__name__", "test_2_2"), 2, 200000},
5464 }
5465
5466 // Create ingester
5467 i, err := prepareIngesterWithBlocksStorage(t, defaultIngesterTestConfig(t), prometheus.NewRegistry())
5468 require.NoError(t, err)
5469 require.NoError(t, services.StartAndAwaitRunning(context.Background(), i))
5470 defer services.StopAndAwaitTerminated(context.Background(), i) //nolint:errcheck
5471
5472 // Wait until it's ACTIVE
5473 test.Poll(t, 1*time.Second, ring.ACTIVE, func() any {
5474 return i.lifecycler.GetState()
5475 })
5476 for _, series := range series {
5477 ctx := user.InjectOrgID(context.Background(), series.user)
5478 req, _ := mockWriteRequest(t, series.lbls, series.value, series.timestamp)
5479 _, err := i.Push(ctx, req)
5480 require.NoError(t, err)
5481 }
5482
5483 // Force compaction to test loaded blocks
5484 compactionCallbackCh := make(chan struct{})
5485 i.TSDBState.forceCompactTrigger <- requestWithUsersAndCallback{users: nil, callback: compactionCallbackCh}
5486 <-compactionCallbackCh
5487
5488 // force update statistics
5489 for _, db := range i.TSDBState.dbs {
5490 db.ingestedAPISamples.Tick()
5491 db.ingestedRuleSamples.Tick()
5492 }
5493
5494 // Get label names
5495 response := httptest.NewRecorder()
5496 request := httptest.NewRequest("GET", "/all_user_stats", nil)
5497 request.Header.Add("Accept", "application/json")
5498 i.AllUserStatsHandler(response, request)
5499 var resp UserStatsByTimeseries
5500 err = json.Unmarshal(response.Body.Bytes(), &resp)
5501 require.NoError(t, err)
5502
5503 expect := UserStatsByTimeseries{
5504 {
5505 UserID: "user-1",
5506 UserStats: UserStats{
5507 IngestionRate: 0.2,
5508 NumSeries: 0,
5509 APIIngestionRate: 0.2,

Callers

nothing calls this directly

Calls 13

StartAndAwaitRunningFunction · 0.92
StopAndAwaitTerminatedFunction · 0.92
PollFunction · 0.92
mockWriteRequestFunction · 0.70
PushMethod · 0.65
AllUserStatsHandlerMethod · 0.65
BytesMethod · 0.65
GetStateMethod · 0.45
TickMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected