Skip to content

Commit 40eb3ed

Browse files
committed
Add support for Cache-Control: no-cache support for Loki queries.
Goal is, if that header is set, we gurantee that response user got for that query is never from results cache. Signed-off-by: Kaviraj <[email protected]>
1 parent da8ee7e commit 40eb3ed

File tree

5 files changed

+303
-163
lines changed

5 files changed

+303
-163
lines changed

cmd/logcli/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ func newQueryClient(app *kingpin.Application) client.Client {
475475
app.Flag("key", "Path to the client certificate key. Can also be set using LOKI_CLIENT_KEY_PATH env var.").Default("").Envar("LOKI_CLIENT_KEY_PATH").StringVar(&client.TLSConfig.KeyFile)
476476
app.Flag("org-id", "adds X-Scope-OrgID to API requests for representing tenant ID. Useful for requesting tenant data when bypassing an auth gateway. Can also be set using LOKI_ORG_ID env var.").Default("").Envar("LOKI_ORG_ID").StringVar(&client.OrgID)
477477
app.Flag("query-tags", "adds X-Query-Tags http header to API requests. This header value will be part of `metrics.go` statistics. Useful for tracking the query. Can also be set using LOKI_QUERY_TAGS env var.").Default("").Envar("LOKI_QUERY_TAGS").StringVar(&client.QueryTags)
478+
app.Flag("no-cache", "adds Cache-Control: no-store http header to API requests. Can also be set using LOKI_NO_CACHE env var.").Default("false").Envar("LOKI_NO_CACHE").BoolVar(&client.NoCache)
478479
app.Flag("bearer-token", "adds the Authorization header to API requests for authentication purposes. Can also be set using LOKI_BEARER_TOKEN env var.").Default("").Envar("LOKI_BEARER_TOKEN").StringVar(&client.BearerToken)
479480
app.Flag("bearer-token-file", "adds the Authorization header to API requests for authentication purposes. Can also be set using LOKI_BEARER_TOKEN_FILE env var.").Default("").Envar("LOKI_BEARER_TOKEN_FILE").StringVar(&client.BearerTokenFile)
480481
app.Flag("retries", "How many times to retry each query when getting an error response from Loki. Can also be set using LOKI_CLIENT_RETRIES env var.").Default("0").Envar("LOKI_CLIENT_RETRIES").IntVar(&client.Retries)

pkg/logcli/client/client.go

+5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type DefaultClient struct {
7777
BearerTokenFile string
7878
Retries int
7979
QueryTags string
80+
NoCache bool
8081
AuthHeader string
8182
ProxyURL string
8283
BackoffConfig BackoffConfig
@@ -375,6 +376,10 @@ func (c *DefaultClient) getHTTPRequestHeader() (http.Header, error) {
375376
h.Set("X-Scope-OrgID", c.OrgID)
376377
}
377378

379+
if c.NoCache {
380+
h.Set("Cache-Control", "no-cache")
381+
}
382+
378383
if c.QueryTags != "" {
379384
h.Set("X-Query-Tags", c.QueryTags)
380385
}

pkg/querier/queryrange/codec.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ func (r *LokiRequest) LogToSpan(sp opentracing.Span) {
9797
)
9898
}
9999

100-
func (*LokiRequest) GetCachingOptions() (res queryrangebase.CachingOptions) { return }
101-
102100
func (r *LokiInstantRequest) GetStep() int64 {
103101
return 0
104102
}
@@ -144,8 +142,6 @@ func (r *LokiInstantRequest) LogToSpan(sp opentracing.Span) {
144142
)
145143
}
146144

147-
func (*LokiInstantRequest) GetCachingOptions() (res queryrangebase.CachingOptions) { return }
148-
149145
func (r *LokiSeriesRequest) GetEnd() time.Time {
150146
return r.EndTs
151147
}
@@ -331,6 +327,14 @@ func (Codec) DecodeRequest(_ context.Context, r *http.Request, _ []string) (quer
331327
return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error())
332328
}
333329

330+
cacheControlHeader := "Cache-Control"
331+
noCacheVal := "no-cache"
332+
disableCacheReq := false
333+
334+
if strings.ToLower(strings.TrimSpace(r.Header.Get(cacheControlHeader))) == noCacheVal {
335+
disableCacheReq = true
336+
}
337+
334338
switch op := getOperation(r.URL.Path); op {
335339
case QueryRangeOp:
336340
rangeQuery, err := loghttp.ParseRangeQuery(r)
@@ -356,6 +360,9 @@ func (Codec) DecodeRequest(_ context.Context, r *http.Request, _ []string) (quer
356360
Plan: &plan.QueryPlan{
357361
AST: parsed,
358362
},
363+
CachingOptions: resultscache.CachingOptions{
364+
Disabled: disableCacheReq,
365+
},
359366
}, nil
360367
case InstantQueryOp:
361368
req, err := loghttp.ParseInstantQuery(r)
@@ -378,6 +385,9 @@ func (Codec) DecodeRequest(_ context.Context, r *http.Request, _ []string) (quer
378385
Plan: &plan.QueryPlan{
379386
AST: parsed,
380387
},
388+
CachingOptions: resultscache.CachingOptions{
389+
Disabled: disableCacheReq,
390+
},
381391
}, nil
382392
case SeriesOp:
383393
req, err := loghttp.ParseAndValidateSeriesQuery(r)

0 commit comments

Comments
 (0)