getRequestID tries to read request id from headers or from lambda context or generates a new one if nothing found.
(req *http.Request)
| 206 | // getRequestID tries to read request id from headers or from lambda |
| 207 | // context or generates a new one if nothing found. |
| 208 | func (r *Router) getRequestID(req *http.Request) string { |
| 209 | // Get request ID from headers (if any) |
| 210 | reqID := req.Header.Get(httpheaders.XRequestID) |
| 211 | |
| 212 | if len(reqID) == 0 || !requestIDRe.MatchString(reqID) { |
| 213 | lambdaContextVal := req.Header.Get(httpheaders.XAmznRequestContextHeader) |
| 214 | |
| 215 | if len(lambdaContextVal) > 0 { |
| 216 | var lambdaContext struct { |
| 217 | RequestID string `json:"requestId"` |
| 218 | } |
| 219 | |
| 220 | err := json.Unmarshal([]byte(lambdaContextVal), &lambdaContext) |
| 221 | if err == nil && len(lambdaContext.RequestID) > 0 { |
| 222 | reqID = lambdaContext.RequestID |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if len(reqID) == 0 || !requestIDRe.MatchString(reqID) { |
| 228 | reqID, _ = nanoid.New() |
| 229 | } |
| 230 | |
| 231 | return reqID |
| 232 | } |
| 233 | |
| 234 | // replaceRemoteAddr rewrites the req.RemoteAddr property from request headers |
| 235 | func (r *Router) replaceRemoteAddr(req *http.Request) { |