gitlab.com/gitlab-org/labkit@v1.21.0/errortracking/capture_request.go (about)

     1  package errortracking
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/getsentry/sentry-go"
     7  	"gitlab.com/gitlab-org/labkit/mask"
     8  )
     9  
    10  // WithRequest will capture details of the request along with the error.
    11  func WithRequest(r *http.Request) CaptureOption {
    12  	return func(config *captureConfig, event *sentry.Event) {
    13  		event.Request = redactRequestInfo(r)
    14  		event.Request.URL = r.URL.String()
    15  		event.Request.Headers["host"] = r.URL.Hostname()
    16  		event.Request.Method = r.Method
    17  	}
    18  }
    19  
    20  // redactRequestInfo strips out information that shouldn't be send by the client.
    21  func redactRequestInfo(r *http.Request) *sentry.Request {
    22  	if r == nil {
    23  		return &sentry.Request{}
    24  	}
    25  
    26  	req := &sentry.Request{
    27  		Headers: make(map[string]string),
    28  	}
    29  
    30  	for header, v := range r.Header {
    31  		req.Headers[header] = v[0]
    32  		if mask.IsSensitiveHeader(header) {
    33  			req.Headers[header] = mask.RedactionString
    34  		}
    35  	}
    36  
    37  	params := r.URL.Query()
    38  	for paramName := range params {
    39  		if mask.IsSensitiveParam(paramName) {
    40  			for i := range params[paramName] {
    41  				params[paramName][i] = mask.RedactionString
    42  			}
    43  		}
    44  	}
    45  	req.QueryString = params.Encode()
    46  
    47  	return req
    48  }