Skip to content

Commit 04e72a4

Browse files
addressed comments, removed total queries metric, and adjusted formatting of logs
1 parent 1100329 commit 04e72a4

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

cmd/thanos/query_frontend.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ func registerQueryFrontend(app *extkingpin.App) {
147147
cmd.Flag("query-frontend.log-queries-longer-than", "Log queries that are slower than the specified duration. "+
148148
"Set to 0 to disable. Set to < 0 to enable on all queries.").Default("0").DurationVar(&cfg.CortexHandlerConfig.LogQueriesLongerThan)
149149

150-
cmd.Flag("query-frontend.query-stats-enabled", "Enable query stats").Default("true").BoolVar(&cfg.CortexHandlerConfig.QueryStatsEnabled)
151-
152150
cmd.Flag("query-frontend.log-failed-queries", "Log failed queries due to any reason").Default("true").BoolVar(&cfg.CortexHandlerConfig.LogFailedQueries)
153151

154152
cmd.Flag("failed-query-cache-capacity", "Capacity of cache for failed queries. 0 means this feature is disabled.").

internal/cortex/frontend/transport/handler.go

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ type Handler struct {
7171
activeUsers *util.ActiveUsersCleanupService
7272
}
7373

74-
// CacheableError Returns true if response code is in pre-defined cacheable errors list, else returns false
75-
func CacheableError(statusCode int) bool {
74+
// isCacheableError Returns true if response code is in pre-defined cacheable errors list, else returns false
75+
func isCacheableError(statusCode int) bool {
7676
for _, errStatusCode := range cacheableResponseCodes {
7777
if errStatusCode == statusCode {
7878
return true
@@ -84,23 +84,23 @@ func CacheableError(statusCode int) bool {
8484
// NewHandler creates a new frontend handler.
8585
func NewHandler(cfg HandlerConfig, roundTripper http.RoundTripper, log log.Logger, reg prometheus.Registerer) http.Handler {
8686
var (
87-
LRU *lru.Cache
87+
LruCache *lru.Cache
88+
err error
8889
)
8990

9091
if cfg.FailedQueryCacheCapacity > 0 {
91-
LRU_res, err := lru.New(cfg.FailedQueryCacheCapacity)
92-
LRU = LRU_res
92+
LruCache, err = lru.New(cfg.FailedQueryCacheCapacity)
9393
if err != nil {
94-
LRU = nil
95-
level.Error(log).Log("msg", "failed to create lru cache", "err", err)
94+
LruCache = nil
95+
level.Error(log).Log("msg", "Failed to create LruCache", "err", err)
9696
}
9797
}
9898

9999
h := &Handler{
100100
cfg: cfg,
101101
log: log,
102102
roundTripper: roundTripper,
103-
lru: LRU,
103+
lru: LruCache,
104104
regex: regexp.MustCompile(`[\s\n\t]+`),
105105
errorExtract: regexp.MustCompile(`Code\((\d+)\)`),
106106
}
@@ -121,13 +121,8 @@ func NewHandler(cfg HandlerConfig, roundTripper http.RoundTripper, log log.Logge
121121
Help: "Size of all chunks fetched to execute a query in bytes.",
122122
}, []string{"user"})
123123

124-
h.totalQueries = promauto.With(reg).NewCounter(prometheus.CounterOpts{
125-
Name: "cortex_queries_total",
126-
Help: "Total number of queries.",
127-
})
128-
129124
h.cachedHits = promauto.With(reg).NewCounter(prometheus.CounterOpts{
130-
Name: "cortex_queries_hits_total",
125+
Name: "cached_failed_queries_count",
131126
Help: "Total number of queries that hit the cache.",
132127
})
133128

@@ -158,9 +153,7 @@ func (f *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
158153
var ctx context.Context
159154
stats, ctx = querier_stats.ContextWithEmptyStats(r.Context())
160155
r = r.WithContext(ctx)
161-
// Increment total queries
162-
f.totalQueries.Inc()
163-
level.Info(util_log.WithContext(r.Context(), f.log)).Log("msg", "QUERY STATS ENABLED, COUNTERS INSTANTIATED")
156+
level.Info(util_log.WithContext(r.Context(), f.log)).Log("msg", "Query Stats Enabled")
164157
}
165158

166159
defer func() {
@@ -190,7 +183,7 @@ func (f *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
190183
return end - start
191184
}
192185

193-
//Store query time range length
186+
// Store query time range length
194187
queryExpressionRangeLength = getQueryRangeSeconds()
195188

196189
// Check if query in cache and whether value exceeds time range length
@@ -224,12 +217,12 @@ func (f *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
224217
// Checking if error code extracted properly
225218
if strConvError != nil {
226219
level.Error(util_log.WithContext(r.Context(), f.log)).Log(
227-
"msg", "STRING TO INT CONVERSION ERROR ", "response ", resp,
220+
"msg", "String to int conversion error", "response ", resp,
228221
)
229222
}
230223

231224
// If error should be cached, store it in cache
232-
if CacheableError(errCode) {
225+
if isCacheableError(errCode) {
233226
//checks if queryExpression is already in cache, and updates time range length value if it is shorter
234227
if contains, _ := f.lru.ContainsOrAdd(queryExpressionNormalized, queryExpressionRangeLength); contains {
235228
if oldValue, ok := f.lru.Get(queryExpressionNormalized); ok {
@@ -239,16 +232,16 @@ func (f *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
239232
}
240233

241234
level.Info(util_log.WithContext(r.Context(), f.log)).Log(
242-
"msg", "CACHED QUERY: CACHABLE ERROR", "response ", resp,
235+
"msg", "Cached query due to cacheable error code", "response ", resp,
243236
)
244237
} else {
245238
level.Info(util_log.WithContext(r.Context(), f.log)).Log(
246-
"msg", "DID NOT CACHE QUERY: NOT CACHABLE ERROR", "response ", resp,
239+
"msg", "Did not cache query due to non-cacheable error code", "response ", resp,
247240
)
248241
}
249242
} else {
250243
level.Error(util_log.WithContext(r.Context(), f.log)).Log(
251-
"msg", "REGEX CONVERSION ERROR ", "response ", resp,
244+
"msg", "Error string regex conversion error", "response ", resp,
252245
)
253246
}
254247
}

0 commit comments

Comments
 (0)