github.com/go-graphite/carbonapi@v0.17.0/cmd/carbonapi/http/enrichcontext.go (about)

     1  package http
     2  
     3  import (
     4  	"net/http"
     5  
     6  	utilctx "github.com/go-graphite/carbonapi/util/ctx"
     7  )
     8  
     9  // TrackConnections exports via expvar a list of all currently executing requests
    10  func enrichContextWithHeaders(headersToPass, headersToLog []string, fn http.HandlerFunc) http.HandlerFunc {
    11  	return func(w http.ResponseWriter, req *http.Request) {
    12  		headersToPassMap := make(map[string]string)
    13  		for _, name := range headersToPass {
    14  			h := req.Header.Get(name)
    15  			if h != "" {
    16  				headersToPassMap[name] = h
    17  			}
    18  		}
    19  
    20  		headersToLogMap := make(map[string]string)
    21  		for _, name := range headersToLog {
    22  			h := req.Header.Get(name)
    23  			if h != "" {
    24  				headersToLogMap[name] = h
    25  			}
    26  		}
    27  
    28  		ctx := utilctx.SetPassHeaders(req.Context(), headersToPassMap)
    29  		ctx = utilctx.SetLogHeaders(ctx, headersToLogMap)
    30  		req = req.WithContext(ctx)
    31  
    32  		fn(w, req)
    33  	}
    34  }