{ http.HandleFunc("/", Handler) http.ListenAndServe(":8001", nil) } func Handler(w http.ResponseWriter, r *http.Request) { log.Println("A user is doing something here") } 2017/11/04 11:21:07 A user is doing something here Yay… well, thanks stdlib, super useful.
http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { id := r.Header.Get("X-Request-ID") if len(id) == 0 { id = uuid.NewV4().String() r.Header.Set("X-Request-ID", id) } r = r.WithContext(context.WithValue(r.Context(), "request_id", id)) next(w, r) }) } And in the logging middleware: id, ok := r.Context().Value("request_id").(string) if ok { logger = logger.WithField("request_id", id) }
HAProxy, Apache have modules to add a X-Request-ID header. Also builtin in PaaS (Scalingo / Heroku) doc.scalingo.com/internals/x-request-id (http://doc.scalingo.com/internals/x-request-id) devcenter.heroku.com/articles/http-request-id (https://devcenter.heroku.com/articles/http-request-id)
the message: type Message struct { RequestID string `json:"request_id"` Type string `json:"type"` At int64 `json:"at"` Payload json.RawMessage `json:"payload"` }