Skip to content

Commit

Permalink
Extract query tags from HTTP requests. (grafana#11147)
Browse files Browse the repository at this point in the history
**What this PR does / why we need it**:
grafana#10858 removed the code that would
inject query tags into the context of the querier. This change adds an
extraction in the decode method.
  • Loading branch information
jeschkies authored and rhnasc committed Apr 12, 2024
1 parent b368964 commit f258d9a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions pkg/querier/queryrange/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ func (Codec) DecodeHTTPGrpcRequest(ctx context.Context, r *httpgrpc.HTTPRequest)
}
}

// Add query tags
if queryTags := httpreq.ExtractQueryTagsFromHTTP(httpReq); queryTags != "" {
ctx = httpreq.InjectQueryTags(ctx, queryTags)
}

// If there is not encoding flags in the context, we try the HTTP request.
if encFlags := httpreq.ExtractEncodingFlagsFromCtx(ctx); encFlags == nil {
encFlags = httpreq.ExtractEncodingFlagsFromProto(r)
Expand Down
16 changes: 12 additions & 4 deletions pkg/util/httpreq/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,26 @@ func ExtractQueryTagsMiddleware() middleware.Interface {
return middleware.Func(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
tags := req.Header.Get(string(QueryTagsHTTPHeader))
tags = safeQueryTags.ReplaceAllString(tags, "_")

if tags != "" {
ctx = context.WithValue(ctx, QueryTagsHTTPHeader, tags)
if tags := ExtractQueryTagsFromHTTP(req); tags != "" {
ctx = InjectQueryTags(ctx, tags)
req = req.WithContext(ctx)
}
next.ServeHTTP(w, req)
})
})
}

func ExtractQueryTagsFromHTTP(req *http.Request) string {
tags := req.Header.Get(string(QueryTagsHTTPHeader))
return safeQueryTags.ReplaceAllString(tags, "_")
}

func InjectQueryTags(ctx context.Context, tags string) context.Context {
tags = safeQueryTags.ReplaceAllString(tags, "_")
return context.WithValue(ctx, QueryTagsHTTPHeader, tags)
}

func ExtractQueryMetricsMiddleware() middleware.Interface {
return middleware.Func(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit f258d9a

Please sign in to comment.