Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/receive: add tracing to forwarded requests #1613

Merged
merged 1 commit into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/thanos/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func runReceive(
TenantHeader: tenantHeader,
ReplicaHeader: replicaHeader,
ReplicationFactor: replicationFactor,
Tracer: tracer,
})

statusProber := prober.NewProber(comp, logger, prometheus.WrapRegistererWithPrefix("thanos_", reg))
Expand Down
18 changes: 17 additions & 1 deletion pkg/receive/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
terrors "github.com/prometheus/prometheus/tsdb/errors"
extpromhttp "github.com/thanos-io/thanos/pkg/extprom/http"
"github.com/thanos-io/thanos/pkg/runutil"
"github.com/thanos-io/thanos/pkg/tracing"
)

// Options for the web Handler.
Expand All @@ -36,10 +37,12 @@ type Options struct {
TenantHeader string
ReplicaHeader string
ReplicationFactor uint64
Tracer opentracing.Tracer
}

// Handler serves a Prometheus remote write receiving HTTP endpoint.
type Handler struct {
client *http.Client
logger log.Logger
writer *Writer
router *route.Router
Expand All @@ -58,7 +61,13 @@ func NewHandler(logger log.Logger, o *Options) *Handler {
logger = log.NewNopLogger()
}

client := &http.Client{}
if o.Tracer != nil {
client.Transport = tracing.HTTPTripperware(logger, http.DefaultTransport)
}

h := &Handler{
client: client,
logger: logger,
writer: o.Writer,
router: route.New(),
Expand All @@ -79,6 +88,9 @@ func NewHandler(logger log.Logger, o *Options) *Handler {

readyf := h.testReady
instrf := func(name string, next func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
if o.Tracer != nil {
next = tracing.HTTPMiddleware(o.Tracer, name, logger, http.HandlerFunc(next))
}
return ins.NewHandler(name, http.HandlerFunc(next))
}

Expand Down Expand Up @@ -342,10 +354,14 @@ func (h *Handler) parallelizeRequests(ctx context.Context, tenant string, replic
h.forwardRequestsTotal.WithLabelValues("success").Inc()
}()

// Create a span to track the request made to another receive node.
span, ctx := tracing.StartSpan(ctx, "thanos_receive_forward")
defer span.Finish()

// Actually make the request against the endpoint
// we determined should handle these time series.
var res *http.Response
res, err = http.DefaultClient.Do(req.WithContext(ctx))
res, err = h.client.Do(req.WithContext(ctx))
if err != nil {
level.Error(h.logger).Log("msg", "forwarding request", "err", err, "endpoint", endpoint)
ec <- err
Expand Down
4 changes: 2 additions & 2 deletions pkg/tracing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/opentracing/opentracing-go/ext"
)

// HTTPMiddleware returns HTTP handler that injects given tracer and starts new server span. If any client span is fetched
// wire we include that as our parent.
// HTTPMiddleware returns an HTTP handler that injects the given tracer and starts a new server span.
// If any client span is fetched from the wire, we include that as our parent.
func HTTPMiddleware(tracer opentracing.Tracer, name string, logger log.Logger, next http.Handler) http.HandlerFunc {
operationName := fmt.Sprintf("/%s HTTP[server]", name)

Expand Down