Skip to content

Commit

Permalink
Code review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
  • Loading branch information
56quarters committed Sep 25, 2024
1 parent 4e5b283 commit c7c54d4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
36 changes: 19 additions & 17 deletions pkg/frontend/querymiddleware/error_caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,29 @@ func (e *errorCachingHandler) Do(ctx context.Context, request MetricsQueryReques

func (e *errorCachingHandler) loadErrorFromCache(ctx context.Context, key, hashedKey string, spanLog *spanlogger.SpanLogger) *apierror.APIError {
res := e.cache.GetMulti(ctx, []string{hashedKey})
if cached, ok := res[hashedKey]; ok {
var cachedError CachedError
if err := proto.Unmarshal(cached, &cachedError); err != nil {
level.Warn(spanLog).Log("msg", "unable to unmarshall cached error", "err", err)
return nil
}
cached, ok := res[hashedKey]
if !ok {
return nil
}

if cachedError.GetKey() != key {
spanLog.DebugLog(
"msg", "cached error key does not match",
"expected_key", key,
"actual_key", cachedError.GetKey(),
"hashed_key", hashedKey,
)
return nil
}
var cachedError CachedError
if err := proto.Unmarshal(cached, &cachedError); err != nil {
level.Warn(spanLog).Log("msg", "unable to unmarshall cached error", "err", err)
return nil
}

return apierror.New(apierror.Type(cachedError.ErrorType), cachedError.ErrorMessage)
if cachedError.GetKey() != key {
spanLog.DebugLog(
"msg", "cached error key does not match",
"expected_key", key,
"actual_key", cachedError.GetKey(),
"hashed_key", hashedKey,
)
return nil
}

return nil
return apierror.New(apierror.Type(cachedError.ErrorType), cachedError.ErrorMessage)

}

func (e *errorCachingHandler) storeErrorToCache(key, hashedKey string, ttl time.Duration, apiErr *apierror.APIError, spanLog *spanlogger.SpanLogger) {
Expand Down
2 changes: 0 additions & 2 deletions pkg/frontend/querymiddleware/error_caching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ func TestErrorCachingHandler_Do(t *testing.T) {
t.Run("cache hit", func(t *testing.T) {
c := cache.NewInstrumentedMockCache()

innerRes := newEmptyPrometheusResponse()
inner := &mockHandler{}
inner.On("Do", mock.Anything, mock.Anything).Return(innerRes, nil)

ctx := user.InjectOrgID(context.Background(), "1234")
req := newDefaultRequest()
Expand Down

0 comments on commit c7c54d4

Please sign in to comment.