httpreqid

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

README

httpreqid

httpreqid is a net/http compatible middleware designed for generating a request ID for each request if it doesn't already have one, and propagates it into the request context and response.

By default, this middleware will look for headers such as X-Request-ID, X-Correlation-ID, X-Trace-ID, Request-ID, Correlation-ID, and Trace-ID.

Additionally, this middleware provides a handler for log/slog that automatically adds the request ID to the log record.

Install

go get github.com/josestg/httpreqid

Examples

  1. A Simple Example of Using crypto/rand

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultHeaders

func DefaultHeaders() []string

DefaultHeaders returns the common header keys used to propagate the request ID.

func FromContext

func FromContext(ctx context.Context) string

FromContext retrieves the request ID from the provided context. If not found, it returns an empty string.

func Handler

func Handler(h http.Handler, g Generator, headerNames ...string) http.Handler

Handler decorates the provided http.Handler with middleware that appends a request ID to the response header and the request context. If no header names are provided, it defaults to DefaultHeaders(). Otherwise, it uses the given header names. The first found header name in the request header is used to propagate the request ID. If none are found, it uses the first header name.

func LogHandler

func LogHandler(h slog.Handler) slog.Handler

LogHandler decorates the provided slog.Handler with request ID logging, appending the request ID to the log record if it's available in the context. By default, it uses "request_id" as the record key. To customize the key, use LogHandlerWithKey.

func LogHandlerWithKey

func LogHandlerWithKey(h slog.Handler, key string) slog.Handler

LogHandlerWithKey decorates the provided slog.Handler with request ID logging, using the specified key to append the request ID to the log record if available.

func Middleware added in v0.2.0

func Middleware(g Generator, headerNames ...string) func(http.Handler) http.Handler

Middleware creates a new middleware that appends a request ID to the response header and the request context.

Types

type Generator

type Generator interface {
	// Generate generates a unique request id for each call.
	Generate(ctx context.Context) string
}

Generator defines the interface for generating unique request IDs.

type GeneratorFunc

type GeneratorFunc func(ctx context.Context) string

GeneratorFunc is an adapter used to make an ordinary function with the same signature implement the Generator.

func (GeneratorFunc) Generate

func (f GeneratorFunc) Generate(ctx context.Context) string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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