requestcontext

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package requestcontext provides middleware that attaches a small request-scoped context to each request: a unique request ID and a start timestamp. The ID is also echoed in the X-Request-Id response header, easing log correlation across services.

Index

Constants

View Source
const HeaderName = "X-Request-Id"

HeaderName is the response header used to expose the request ID.

View Source
const Key = "ctx"

Key is the request value key under which the *Ctx is stored.

Variables

This section is empty.

Functions

func New

func New(opts ...Options) express.Handler

New returns middleware that attaches a *Ctx via req.Set(Key, ctx) and sets the X-Request-Id response header.

Types

type Ctx

type Ctx struct {
	// ID uniquely identifies the request.
	ID string

	// Start is the time the request entered the middleware.
	Start time.Time
}

Ctx is the per-request context attached by this middleware.

func From

func From(req *express.Request) *Ctx

From returns the *Ctx attached to the request, or nil if none is present.

func (*Ctx) Elapsed

func (c *Ctx) Elapsed() time.Duration

Elapsed returns the duration since the request started.

type Options

type Options struct {
	// TrustHeader, when true, reuses an incoming X-Request-Id header (if
	// present and non-empty) instead of generating a new ID. This is useful
	// behind a trusted proxy that assigns request IDs.
	TrustHeader bool

	// Generator overrides the default random ID generator.
	Generator func() string
}

Options configures the requestcontext middleware.

Jump to

Keyboard shortcuts

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