Skip to content

Commit

Permalink
Add request ID to gRPC and HTTP client spans
Browse files Browse the repository at this point in the history
This commit adds the request ID as a span tag to outgoing (client)
http and gRPC requests. This would allow easier correlation
of traces and logs using the request ID.

Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
  • Loading branch information
fpetkovski committed May 23, 2024
1 parent 2538562 commit e208f22
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
20 changes: 11 additions & 9 deletions cmd/thanos/query_frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,21 @@ 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(
logMiddleware.HTTPMiddleware(name, f),
logger,
ins.NewHandler(
name,
gzhttp.GzipHandler(
middleware.RequestID(
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 e208f22

Please sign in to comment.