reqid

package module
v0.0.0-...-2fb1e2c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2017 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandomId

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

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

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

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.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL