reqid: github.com/gomiddleware/reqid Index | Files | Directories

package reqid

import "github.com/gomiddleware/reqid"

Package reqid provides standard Go middleware to read the X-Request-ID header on the incoming request and store it in the request's context. If no header exists, then one is generated. You may optionally scrub the incoming header so that one is always generated (such as on externally facing servers).

Also provided are some convenience functions to be able to read this RequestId back out in later middleware.

Like every other https://gomiddleware.github.io/ middleware, it uses both the http.Handler interface and the context package to store each request's ID. Since Go v1.7, this has been in the Go standard library and therefore we should reduce our dependence on external packages such as gorilla/context.

All of the functions such as RandomId, exported by this package work in the same way as noted above. Explicitly:

* if the incoming request already has a X-Request-Id header, it is read and stored in the request's context * if there is no X-Request-Id header, then one is generated and stored in the request's context (as above)

See https://golang.org/pkg/context/ for more information on the context package. And see https://blog.golang.org/context for a run-down of some code that uses Contexts.

Index

Package Files

doc.go reqid.go

func RandomId Uses

func RandomId(next http.Handler) http.Handler

RandomId will read the incoming X-Request-ID header and store it in the request's context and can be used in later middleware. If the incoming request does not have an associated X-Request-ID, then one will be generated instead.

Note: for internal services, it is safe to just use this middleware. However, for externally facing servers you must use ScrubRequestIdHeader first so that external clients can't set something which is an internal artifact of your services.

func ReqIdFromContext Uses

func ReqIdFromContext(ctx context.Context) string

ReqIdFromContext can be used to obtain the ReqId from the context (if you already have it handy). You are most likely to use ReqIdFromRequest rather than this, but either does the same job.

func ReqIdFromRequest Uses

func ReqIdFromRequest(r *http.Request) string

ReqIdFromRequest can be used to obtain the ReqId from the request. This is given as a convenience method, though at the same time, you must use it because the key value used to store the ReqId in the context is not exported from this package.

func ScrubRequestIdHeader Uses

func ScrubRequestIdHeader(next http.Handler) http.Handler

ScrubRequestIdHeader should be used on externally facing servers when you want to add your own X-Request-ID. Use this before RandomId. If an externally facing server is hitting internal microservices and you want the X-Request-ID to be passed along to the other services, then just use the RandomId since it will keep any existing value.

Directories

PathSynopsis
example

Package reqid imports 3 packages (graph) and is imported by 3 packages. Updated 2017-07-16. Refresh now. Tools for package owners.