Skip to content

Commit 30bbd3d

Browse files
committed
fix tests
Signed-off-by: Justin Jung <[email protected]>
1 parent 9efbbd9 commit 30bbd3d

File tree

2 files changed

+31
-51
lines changed

2 files changed

+31
-51
lines changed

pkg/frontend/transport/handler.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -490,37 +490,39 @@ func (f *Handler) reportQueryStats(r *http.Request, source, userID string, query
490490
}
491491
}
492492

493-
// We are unable to use errors.As to compare since body string from the http response is wrapped as an error
494493
var reason string
495-
errMsg := error.Error()
496-
if statusCode == http.StatusTooManyRequests {
497-
var resourceExhaustedErr resource.ExhaustedError
498-
if strings.Contains(errMsg, resourceExhaustedErr.Error()) {
499-
reason = reasonResourceExhausted
500-
} else {
501-
reason = reasonTooManyRequests
502-
}
503-
} else if statusCode == http.StatusRequestEntityTooLarge {
494+
if statusCode == http.StatusRequestEntityTooLarge {
504495
reason = reasonResponseBodySizeExceeded
505-
} else if statusCode == http.StatusUnprocessableEntity {
506-
if strings.Contains(errMsg, limitTooManySamples) {
507-
reason = reasonTooManySamples
508-
} else if strings.Contains(errMsg, limitTimeRangeExceeded) {
509-
reason = reasonTimeRangeExceeded
510-
} else if strings.Contains(errMsg, limitSeriesFetched) {
511-
reason = reasonSeriesFetched
512-
} else if strings.Contains(errMsg, limitChunksFetched) {
513-
reason = reasonChunksFetched
514-
} else if strings.Contains(errMsg, limitChunkBytesFetched) {
515-
reason = reasonChunkBytesFetched
516-
} else if strings.Contains(errMsg, limitDataBytesFetched) {
517-
reason = reasonDataBytesFetched
518-
} else if strings.Contains(errMsg, limitSeriesStoreGateway) {
519-
reason = reasonSeriesLimitStoreGateway
520-
} else if strings.Contains(errMsg, limitChunksStoreGateway) {
521-
reason = reasonChunksLimitStoreGateway
522-
} else if strings.Contains(errMsg, limitBytesStoreGateway) {
523-
reason = reasonBytesLimitStoreGateway
496+
} else if error != nil {
497+
// We are unable to use errors.As to compare since body string from the http response is wrapped as an error
498+
errMsg := error.Error()
499+
if statusCode == http.StatusTooManyRequests {
500+
var resourceExhaustedErr resource.ExhaustedError
501+
if strings.Contains(errMsg, resourceExhaustedErr.Error()) {
502+
reason = reasonResourceExhausted
503+
} else {
504+
reason = reasonTooManyRequests
505+
}
506+
} else if statusCode == http.StatusUnprocessableEntity {
507+
if strings.Contains(errMsg, limitTooManySamples) {
508+
reason = reasonTooManySamples
509+
} else if strings.Contains(errMsg, limitTimeRangeExceeded) {
510+
reason = reasonTimeRangeExceeded
511+
} else if strings.Contains(errMsg, limitSeriesFetched) {
512+
reason = reasonSeriesFetched
513+
} else if strings.Contains(errMsg, limitChunksFetched) {
514+
reason = reasonChunksFetched
515+
} else if strings.Contains(errMsg, limitChunkBytesFetched) {
516+
reason = reasonChunkBytesFetched
517+
} else if strings.Contains(errMsg, limitDataBytesFetched) {
518+
reason = reasonDataBytesFetched
519+
} else if strings.Contains(errMsg, limitSeriesStoreGateway) {
520+
reason = reasonSeriesLimitStoreGateway
521+
} else if strings.Contains(errMsg, limitChunksStoreGateway) {
522+
reason = reasonChunksLimitStoreGateway
523+
} else if strings.Contains(errMsg, limitBytesStoreGateway) {
524+
reason = reasonBytesLimitStoreGateway
525+
}
524526
}
525527
}
526528
if len(reason) > 0 {

pkg/util/resource/monitor_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package resource
22

33
import (
44
"context"
5-
"fmt"
65
"testing"
76
"time"
87

@@ -71,27 +70,6 @@ func TestMonitor(t *testing.T) {
7170
}
7271
}
7372

74-
func TestMonitor_GetCPUUtilization(t *testing.T) {
75-
limits := configs.Resources{CPU: 10}
76-
thresholds := configs.Resources{}
77-
scanner := mockScanner{CPU: 0}
78-
79-
monitor, err := NewMonitor(thresholds, limits, &scanner, nil)
80-
require.NoError(t, err)
81-
require.NoError(t, monitor.StartAsync(context.Background()))
82-
t.Cleanup(func() {
83-
require.NoError(t, services.StopAndAwaitTerminated(context.Background(), monitor))
84-
})
85-
86-
for i := 0; i < 10; i++ {
87-
scanner.CPU++
88-
time.Sleep(time.Second) // let scanner store values
89-
}
90-
91-
fmt.Println(monitor.GetCPUUtilization())
92-
require.InDelta(t, 0.1, monitor.GetCPUUtilization(), 1e-5)
93-
}
94-
9573
type mockScanner struct {
9674
CPU float64
9775
Heap uint64

0 commit comments

Comments
 (0)