gitlab.com/gitlab-org/labkit@v1.21.0/correlation/outbound_http.go (about) 1 package correlation 2 3 import ( 4 "net/http" 5 ) 6 7 const ( 8 propagationHeader = "X-Request-ID" 9 clientNameHeader = "X-GitLab-Client-Name" 10 ) 11 12 type instrumentedRoundTripper struct { 13 delegate http.RoundTripper 14 config instrumentedRoundTripperConfig 15 } 16 17 // injectRequest will pass the CorrelationId through to a downstream http request 18 // for propagation. 19 func (c instrumentedRoundTripper) injectRequest(req *http.Request) { 20 correlationID := ExtractFromContext(req.Context()) 21 if correlationID != "" { 22 req.Header.Set(propagationHeader, correlationID) 23 } 24 if c.config.clientName != "" { 25 req.Header.Set(clientNameHeader, c.config.clientName) 26 } 27 } 28 29 func (c instrumentedRoundTripper) RoundTrip(req *http.Request) (res *http.Response, e error) { 30 c.injectRequest(req) 31 return c.delegate.RoundTrip(req) 32 } 33 34 // NewInstrumentedRoundTripper acts as a "client-middleware" for outbound http requests 35 // adding instrumentation to the outbound request and then delegating to the underlying 36 // transport. 37 // 38 // If will extract the current Correlation-ID from the request context and pass this via 39 // the X-Request-ID request header to downstream services. 40 func NewInstrumentedRoundTripper(delegate http.RoundTripper, opts ...InstrumentedRoundTripperOption) http.RoundTripper { 41 config := applyInstrumentedRoundTripperOptions(opts) 42 43 return &instrumentedRoundTripper{delegate: delegate, config: config} 44 }