Skip to content

Commit

Permalink
Merge pull request #7384 from fpetkovski/grpc-request-id
Browse files Browse the repository at this point in the history
Add request ID to client grpc spans
  • Loading branch information
fpetkovski authored May 24, 2024
2 parents 2538562 + 1208806 commit 77c8864
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
16 changes: 8 additions & 8 deletions cmd/thanos/query_frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,19 @@ func runQueryFrontend(
if !cfg.webDisableCORS {
api.SetCORS(w)
}
tracing.HTTPMiddleware(
tracer,
name,
logger,
ins.NewHandler(
middleware.RequestID(
tracing.HTTPMiddleware(
tracer,
name,
gzhttp.GzipHandler(
middleware.RequestID(
logger,
ins.NewHandler(
name,
gzhttp.GzipHandler(
logMiddleware.HTTPMiddleware(name, f),
),
),
// Cortex frontend middlewares require orgID.
),
// Cortex frontend middlewares require orgID.
).ServeHTTP(w, r.WithContext(user.InjectOrgID(r.Context(), orgId)))
})
return hf
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ func GetInstr(
}
})

return tracing.HTTPMiddleware(tracer, name, logger,
ins.NewHandler(name,
gzhttp.GzipHandler(
middleware.RequestID(
return middleware.RequestID(
tracing.HTTPMiddleware(tracer, name, logger,
ins.NewHandler(name,
gzhttp.GzipHandler(
logMiddleware.HTTPMiddleware(name, hf),
),
),
Expand Down
25 changes: 15 additions & 10 deletions pkg/query/remote_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/tracing"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/model/labels"
Expand All @@ -25,6 +26,7 @@ import (

"github.com/thanos-io/thanos/pkg/api/query/querypb"
"github.com/thanos-io/thanos/pkg/info/infopb"
"github.com/thanos-io/thanos/pkg/server/http/middleware"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb/prompb"
)
Expand Down Expand Up @@ -231,17 +233,20 @@ func (r *remoteQuery) Exec(ctx context.Context) *promql.Result {
r.cancel = cancel
defer cancel()

queryRange := r.end.Sub(r.start)
span, qctx := opentracing.StartSpanFromContext(qctx, "remote_query_exec", opentracing.Tags{
"query": r.plan.String(),
"remote_address": r.remoteAddr,
"start": r.start.UTC().String(),
"end": r.end.UTC().String(),
"interval_seconds": r.interval.Seconds(),
"range_seconds": queryRange.Seconds(),
"range_human": queryRange,
var (
queryRange = r.end.Sub(r.start)
requestID, _ = middleware.RequestIDFromContext(qctx)
)
qctx = tracing.ClientAddContextTags(qctx, opentracing.Tags{
"query.expr": r.plan.String(),
"query.remote_address": r.remoteAddr,
"query.start": r.start.UTC().String(),
"query.end": r.end.UTC().String(),
"query.interval_seconds": r.interval.Seconds(),
"query.range_seconds": queryRange.Seconds(),
"query.range_human": queryRange,
"request_id": requestID,
})
defer span.Finish()

var maxResolution int64
if r.opts.AutoDownsample {
Expand Down
5 changes: 5 additions & 0 deletions pkg/tracing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/go-kit/log/level"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"

"github.com/thanos-io/thanos/pkg/server/http/middleware"
"github.com/thanos-io/thanos/pkg/tracing/migration"
)

Expand All @@ -32,6 +34,9 @@ func HTTPMiddleware(tracer opentracing.Tracer, name string, logger log.Logger, n
}

opts := []opentracing.StartSpanOption{ext.RPCServerOption(wireContext)}
if requestID, ok := middleware.RequestIDFromContext(r.Context()); ok {
opts = append(opts, opentracing.Tag{Key: "request_id", Value: requestID})
}
// Check for force tracing header and add it as a tag at the start of span.
// This is required for the OpenTelemetry sampler to force tracing.
if r.Header.Get(ForceTracingBaggageKey) != "" {
Expand Down

0 comments on commit 77c8864

Please sign in to comment.