v12.2.11 Latest Latest

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

Go to latest
Published: Apr 24, 2024 License: BSD-3-Clause Imports: 5 Imported by: 8




This section is empty.


This section is empty.


func Get

func Get(ctx *context.Context) string

Get returns the Request ID or empty string.

A shortcut of `context.GetID().(string)`.

func Hash

func Hash(ctx *context.Context, includeBody bool) string

Hash returns the sha1 hash of the request. It does not capture error, instead it returns an empty string.

func New

func New(generator ...Generator) context.Handler

New returns a new request id middleware. It optionally accepts an ID Generator. The Generator can stop the handlers chain with an error or return a valid ID (string). If it's nil then the `DefaultGenerator` will be used instead.


type Generator

type Generator func(ctx *context.Context) string

Generator defines the function which should extract or generate a Request ID. See `DefaultGenerator` and `New` package-level functions.

var DefaultGenerator Generator = func(ctx *context.Context) string {
	id := ctx.ResponseWriter().Header().Get(xRequestIDHeaderKey)
	if id != "" {
		return id

	id = ctx.GetHeader(xRequestIDHeaderKey)
	if id == "" {
		uid, err := uuid.NewRandom()
		if err != nil {
			return ""

		id = uid.String()

	ctx.Header(xRequestIDHeaderKey, id)
	return id

DefaultGenerator is the default `Generator` that is used when nil is passed on `New` package-level function. It extracts the ID from the "X-Request-ID" request header value or, if missing, it generates a new UUID(v4) and sets the header and context value.

See `Get` package-level function too.

func HashGenerator

func HashGenerator(includeBody bool) Generator

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.

Jump to

Keyboard shortcuts

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