injectRequestContext injects request related metadata into the request context
(r *http.Request)
| 16 | |
| 17 | // injectRequestContext injects request related metadata into the request context |
| 18 | func (h HTTPHeaderMiddleware) injectRequestContext(r *http.Request) *http.Request { |
| 19 | requestContextMap := make(map[string]string) |
| 20 | |
| 21 | // Check to make sure that request context have not already been injected |
| 22 | checkMapInContext := requestmeta.MapFromContext(r.Context()) |
| 23 | if checkMapInContext != nil { |
| 24 | return r |
| 25 | } |
| 26 | |
| 27 | for _, target := range h.TargetHeaders { |
| 28 | contents := r.Header.Get(target) |
| 29 | if contents != "" { |
| 30 | requestContextMap[target] = contents |
| 31 | } |
| 32 | } |
| 33 | requestContextMap[requestmeta.LoggingHeadersKey] = requestmeta.LoggingHeaderKeysToString(h.TargetHeaders) |
| 34 | |
| 35 | reqId := r.Header.Get(h.RequestIdHeader) |
| 36 | if reqId == "" { |
| 37 | reqId = uuid.NewString() |
| 38 | } |
| 39 | requestContextMap[requestmeta.RequestIdKey] = reqId |
| 40 | requestContextMap[requestmeta.RequestSourceKey] = requestmeta.SourceAPI |
| 41 | |
| 42 | ctx := requestmeta.ContextWithRequestMetadataMap(r.Context(), requestContextMap) |
| 43 | return r.WithContext(ctx) |
| 44 | } |
| 45 | |
| 46 | // Wrap implements Middleware |
| 47 | func (h HTTPHeaderMiddleware) Wrap(next http.Handler) http.Handler { |