17830: Sets up X-Request-Id if not provided by client. Adds it to the response.
authorLucas Di Pentima <lucas.dipentima@curii.com>
Wed, 21 Jul 2021 23:51:10 +0000 (20:51 -0300)
committerLucas Di Pentima <lucas.dipentima@curii.com>
Thu, 22 Jul 2021 15:27:14 +0000 (12:27 -0300)
Uses the IDGenerator to explicitly set one request id if needed, so that it
can return it to the client without the need to dig it out from the railsAPI
response.

Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas.dipentima@curii.com>

lib/controller/router/response.go
lib/controller/router/router.go

index 03cdcf18d27e4fcf3df814ab3c652c3479456165..500fb307171c4f59442aca8cbf503092ee7232a6 100644 (file)
@@ -57,7 +57,7 @@ func applySelectParam(selectParam []string, orig map[string]interface{}) map[str
        return selected
 }
 
-func (rtr *router) sendResponse(w http.ResponseWriter, req *http.Request, resp interface{}, opts responseOptions) {
+func (rtr *router) sendResponse(w http.ResponseWriter, req *http.Request, resp interface{}, opts responseOptions, reqId string) {
        var tmp map[string]interface{}
 
        if resp, ok := resp.(http.Handler); ok {
@@ -67,6 +67,7 @@ func (rtr *router) sendResponse(w http.ResponseWriter, req *http.Request, resp i
                return
        }
 
+       w.Header().Set("X-Request-Id", reqId)
        err := rtr.transcode(resp, &tmp)
        if err != nil {
                rtr.sendError(w, err)
index 5ceabbfb1d56fab171d8d4a8dfabca585f1362f6..82e81d089b70625722ac99906ba5e4c74639fc56 100644 (file)
@@ -505,7 +505,12 @@ func (rtr *router) addRoute(endpoint arvados.APIEndpoint, defaultOpts func() int
                        }
                }
                ctx := auth.NewContext(req.Context(), creds)
-               ctx = arvados.ContextWithRequestID(ctx, req.Header.Get("X-Request-Id"))
+               var reqId string
+               if reqId = req.Header.Get("X-Request-Id"); reqId == "" {
+                       reqIDGen := httpserver.IDGenerator{Prefix: "req-"}
+                       reqId = reqIDGen.Next()
+               }
+               ctx = arvados.ContextWithRequestID(ctx, reqId)
                logger.WithFields(logrus.Fields{
                        "apiEndpoint": endpoint,
                        "apiOptsType": fmt.Sprintf("%T", opts),
@@ -517,7 +522,7 @@ func (rtr *router) addRoute(endpoint arvados.APIEndpoint, defaultOpts func() int
                        rtr.sendError(w, err)
                        return
                }
-               rtr.sendResponse(w, req, resp, respOpts)
+               rtr.sendResponse(w, req, resp, respOpts, reqId)
        })
 }