Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrorHandler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) fmt.Fprint(w, http.StatusText(http.StatusInternalServerError)) })
ErrorHandler is the handler that is executed when a Generator returns an empty string.
Functions ¶
func Get ¶
Get returns the Request ID of this request. A prior call to the Handler or HandlerWithGenerator is required.
func Handler ¶
func Handler(next http.Handler) http.HandlerFunc
Handler wraps a handler with the requestid middleware using the `DefaultGenerator`. See `Get` package-level function to retrieve the generated-and-stored request id. See `HandlerWithGenerator` to use a custom request ID generator.
func HandlerWithGenerator ¶
func HandlerWithGenerator(next http.Handler, gen Generator) http.HandlerFunc
HandlerWithGenerator same as `Handler` function but it accepts a custom `Generator` to extract (and set) the request ID.
func Hash ¶
Hash returns the sha1 hash of the "r" request. It does not capture error, instead it returns an empty string.
func Set ¶
Set manually sets a Request ID for this request. Returns the shallow copy of given "r" request contains the new ID context value. Can be called before Handler execution to modify the method of extraction of the ID.
Note: Caller should manually set a response header for the client, if necessary.
See `Get` package-level function too.
Types ¶
type Generator ¶
type Generator func(w http.ResponseWriter, r *http.Request) string
Generator defines the function which should extract or generate a Request ID. See `DefaultGenerator` package-level function.
var DefaultGenerator Generator = func(w http.ResponseWriter, r *http.Request) string { id := w.Header().Get(xRequestIDHeaderKey) if id != "" { return id } id = r.Header.Get(xRequestIDHeaderKey) if id == "" { uid, err := uuid.NewRandom() if err != nil { return "" } id = uid.String() } setHeader(w, id) return id }
DefaultGenerator is the default `Generator`. It extracts the ID from the "X-Request-Id" request header value or, if missing, it generates a new UUID(v4) and sets the response header.
See `Get` package-level function too.
func HashGenerator ¶
HashGenerator uses the request's hash to generate a fixed-length Request ID. Note that one or many requests may contain the same ID, so it's not unique.