github.com/thanos-io/thanos@v0.32.5/internal/cortex/frontend/downstream_roundtripper.go (about) 1 // Copyright (c) The Cortex Authors. 2 // Licensed under the Apache License 2.0. 3 4 package frontend 5 6 import ( 7 "net/http" 8 "net/url" 9 "path" 10 11 "github.com/opentracing/opentracing-go" 12 ) 13 14 // RoundTripper that forwards requests to downstream URL. 15 type downstreamRoundTripper struct { 16 downstreamURL *url.URL 17 transport http.RoundTripper 18 } 19 20 func NewDownstreamRoundTripper(downstreamURL string, transport http.RoundTripper) (http.RoundTripper, error) { 21 u, err := url.Parse(downstreamURL) 22 if err != nil { 23 return nil, err 24 } 25 26 return &downstreamRoundTripper{downstreamURL: u, transport: transport}, nil 27 } 28 29 func (d downstreamRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { 30 tracer, span := opentracing.GlobalTracer(), opentracing.SpanFromContext(r.Context()) 31 if tracer != nil && span != nil { 32 carrier := opentracing.HTTPHeadersCarrier(r.Header) 33 err := tracer.Inject(span.Context(), opentracing.HTTPHeaders, carrier) 34 if err != nil { 35 return nil, err 36 } 37 } 38 39 r.URL.Scheme = d.downstreamURL.Scheme 40 r.URL.Host = d.downstreamURL.Host 41 r.URL.Path = path.Join(d.downstreamURL.Path, r.URL.Path) 42 r.Host = "" 43 return d.transport.RoundTrip(r) 44 }