Skip to content

Commit f0f9325

Browse files
committed
add request ID injection to context for ruler queries to track split queries
Signed-off-by: Erlan Zholdubai uulu <[email protected]>
1 parent c50634c commit f0f9325

File tree

7 files changed

+11
-6
lines changed

7 files changed

+11
-6
lines changed

pkg/api/middlewares.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (h HTTPHeaderMiddleware) InjectRequestIdHeader(r *http.Request) *http.Reque
4646
if reqId == "" {
4747
reqId = uuid.NewString()
4848
}
49-
ctx := requestutil.ContextWithRequestIdHeader(r.Context(), reqId)
49+
ctx := requestutil.ContextWithRequestId(r.Context(), reqId)
5050
return r.WithContext(ctx)
5151
}
5252

pkg/api/middlewares_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestExistingRequestIdIsPreserved(t *testing.T) {
122122
RequestIdHeader: "X-Request-ID",
123123
}
124124

125-
ctx := requestutil.ContextWithRequestIdHeader(context.Background(), existingID)
125+
ctx := requestutil.ContextWithRequestId(context.Background(), existingID)
126126

127127
h := http.Header{}
128128
h.Add("X-Request-ID", "should-be-ignored")

pkg/querier/worker/frontend_processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (fp *frontendProcessor) runRequest(ctx context.Context, request *httpgrpc.H
143143
}
144144
ctx = util_log.ContextWithHeaderMap(ctx, headerMap)
145145
requestId := headers[textproto.CanonicalMIMEHeaderKey(requestutil.RequestIdPropagationString)]
146-
ctx = requestutil.ContextWithRequestIdHeader(ctx, requestId)
146+
ctx = requestutil.ContextWithRequestId(ctx, requestId)
147147

148148
logger := util_log.WithContext(ctx, fp.log)
149149
if statsEnabled {

pkg/querier/worker/scheduler_processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (sp *schedulerProcessor) querierLoop(c schedulerpb.SchedulerForQuerier_Quer
151151
}
152152
ctx = util_log.ContextWithHeaderMap(ctx, headerMap)
153153
requestId := headers[textproto.CanonicalMIMEHeaderKey(requestutil.RequestIdPropagationString)]
154-
ctx = requestutil.ContextWithRequestIdHeader(ctx, requestId)
154+
ctx = requestutil.ContextWithRequestId(ctx, requestId)
155155

156156
tracer := opentracing.GlobalTracer()
157157
// Ignore errors here. If we cannot get parent span, we just don't create new one.

pkg/ruler/compat.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"time"
88

9+
"github.com/google/uuid"
910
"github.com/go-kit/log"
1011
"github.com/go-kit/log/level"
1112
"github.com/prometheus/client_golang/prometheus"
@@ -27,6 +28,7 @@ import (
2728
"github.com/cortexproject/cortex/pkg/ring/client"
2829
util_log "github.com/cortexproject/cortex/pkg/util/log"
2930
promql_util "github.com/cortexproject/cortex/pkg/util/promql"
31+
requestutil "github.com/cortexproject/cortex/pkg/util/request"
3032
"github.com/cortexproject/cortex/pkg/util/validation"
3133
)
3234

@@ -183,6 +185,9 @@ func EngineQueryFunc(engine promql.QueryEngine, frontendClient *frontendClient,
183185
}
184186
}
185187

188+
// Add request ID to the context so that it can be used in logs and metrics for split queries.
189+
ctx = requestutil.ContextWithRequestId(ctx, uuid.NewString())
190+
186191
if frontendClient != nil {
187192
v, err := frontendClient.InstantQuery(ctx, qs, t)
188193
if err != nil {

pkg/util/grpcutil/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func extractRequestIdFromMetadata(ctx context.Context) context.Context {
7171
if len(requestIds) == 0 {
7272
return ctx
7373
}
74-
return requestutil.ContextWithRequestIdHeader(ctx, requestIds[0])
74+
return requestutil.ContextWithRequestId(ctx, requestIds[0])
7575
}
7676

7777
// HTTPHeaderPropagationClientInterceptor allows for propagation of HTTP Request headers across gRPC calls - works

pkg/util/request/request_id.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const (
1313
RequestIdPropagationString string = "x-request-id-forwarding-string"
1414
)
1515

16-
func ContextWithRequestIdHeader(ctx context.Context, reqId string) context.Context {
16+
func ContextWithRequestId(ctx context.Context, reqId string) context.Context {
1717
return context.WithValue(ctx, RequestIdKey, reqId)
1818
}
1919

0 commit comments

Comments
 (0)