Documentation
¶
Overview ¶
Package requestid provides a middleware that sets a unique request ID for each incoming HTTP request. And it also provides a logger that logs the request ID for each incoming HTTP request.
Example ¶
logger := requestid.NewLogger(slog.NewTextHandler(os.Stdout, testOptions)) helloHandler := func(w http.ResponseWriter, req *http.Request) { ctx := req.Context() logger.InfoContext(ctx, "Start") defer logger.InfoContext(ctx, "End") io.WriteString(w, "Hello, world!\n") //nolint:errcheck } ts := httptest.NewServer( requestid.Wrap(http.HandlerFunc(helloHandler)), ) defer ts.Close() resp, _ := http.Get(ts.URL) //nolint:errcheck defer resp.Body.Close() //nolint:govet b, _ := io.ReadAll(resp.Body) //nolint:errcheck fmt.Println(string(b)) // Output logs with req_id with generated ID // level=INFO msg=Start req_id=k3pGQp5T // level=INFO msg=End req_id=k3pGQp5T // Hello, world!
Index ¶
- Variables
- func ErrorLoggingRecoveryFunc(logLevel slog.Level, alt string) func(error) string
- func IDGenErrorSuppressor(idGen func() (string, error), recoveryFunc func(error) string) func() string
- func NewLogger(h slog.Handler) *slog.Logger
- func RandIntIDGenerator(randInt func() int, letters []byte, length int) func() string
- func RandReadIDGenerator(randRead func(b []byte) (n int, err error), letters []byte, length int) func() (string, error)
- func ResetDefault()
- func SetDefault(ns *Middleware)
- func Wrap(next http.Handler) http.Handler
- func WrapSlogHandler(h slog.Handler) slog.Handler
- type Header
- type HeaderOption
- type HeaderOptions
- type Middleware
- type Option
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var IDGeneratorDefault = IDGenErrorSuppressor( RandReadIDGenerator(rand.Read, defaultIDLetters, defaultIDLength), ErrorLoggingRecoveryFunc(slog.LevelWarn, "id-gen-error"), )
IDGeneratorDefault is the default ID generator.
Functions ¶
func ErrorLoggingRecoveryFunc ¶ added in v0.3.0
ErrorLoggingRecoveryFunc returns a recovery function that logs an error with the specified log level.
func IDGenErrorSuppressor ¶ added in v0.3.0
func IDGenErrorSuppressor(idGen func() (string, error), recoveryFunc func(error) string) func() string
IDGenErrorSuppressor returns an ID generator that suppresses errors. If an error occurs, the recover function is called with the error and the result is returned.
func NewLogger ¶ added in v0.3.0
NewLogger returns a new logger with the default Namespace.
Example ¶
handler := slog.NewTextHandler(os.Stdout, testOptions) logger := slog.New(requestid.WrapSlogHandler(handler)) helloHandler := func(w http.ResponseWriter, req *http.Request) { ctx := req.Context() logger.InfoContext(ctx, "Start") defer logger.InfoContext(ctx, "End") io.WriteString(w, "Hello, world!\n") //nolint:errcheck } ts := httptest.NewServer( requestid.Wrap(http.HandlerFunc(helloHandler)), ) defer ts.Close() resp, _ := http.Get(ts.URL) //nolint:errcheck defer resp.Body.Close() //nolint:govet b, _ := io.ReadAll(resp.Body) //nolint:errcheck fmt.Println(string(b)) // Output logs with req_id with generated ID // level=INFO msg=Start req_id=k3pGQp5T // level=INFO msg=End req_id=k3pGQp5T // Hello, world!
func RandIntIDGenerator ¶ added in v0.3.0
Returns a random ID generator that generates a string of length characters using randInt to generate random integers such as Int function from math/rand/v2 package.
func RandReadIDGenerator ¶ added in v0.3.0
func RandReadIDGenerator( randRead func(b []byte) (n int, err error), letters []byte, length int, ) func() (string, error)
Returns a random ID generator that generates a string of length characters using randRead to generate random bytes such as Read function from crypto/rand package.
func ResetDefault ¶ added in v0.3.0
func ResetDefault()
ResetDefault resets the default Namespace to the initial state.
func SetDefault ¶ added in v0.3.0
func SetDefault(ns *Middleware)
SetDefault sets the default Namespace.
Types ¶
type HeaderOption ¶ added in v0.3.0
type HeaderOption = func(*HeaderOptions)
type HeaderOptions ¶ added in v0.3.0
type HeaderOptions struct {
// contains filtered or unexported fields
}
type Middleware ¶ added in v0.3.0
type Middleware struct {
// contains filtered or unexported fields
}
Middleware provides a middleware for generating and setting request ID.
Example ¶
initializeSlogctx() mw := requestid.New( requestid.LogAttr("req-id"), requestid.Generator(func() string { return "(id-for-test)" }), ) logger := mw.NewLogger(slog.NewTextHandler(os.Stdout, testOptions)) helloHandler := func(w http.ResponseWriter, req *http.Request) { ctx := req.Context() logger.InfoContext(ctx, "Start") defer logger.InfoContext(ctx, "End") io.WriteString(w, "Hello, world!\n") //nolint:errcheck } ts := httptest.NewServer( mw.Wrap(http.HandlerFunc(helloHandler)), ) defer ts.Close() resp, _ := http.Get(ts.URL) //nolint:errcheck defer resp.Body.Close() //nolint:govet b, _ := io.ReadAll(resp.Body) //nolint:errcheck fmt.Println(string(b))
Output: level=INFO msg=Start req-id=(id-for-test) level=INFO msg=End req-id=(id-for-test) Hello, world!
func New ¶ added in v0.2.0
func New(opts ...Option) *Middleware
New returns a new Namespace from the given options.
func (*Middleware) GetNamespace ¶ added in v0.3.0
func (f *Middleware) GetNamespace() *slogctx.Namespace
GetNamespace returns the slogctx.Namespace for logging with request ID.
func (*Middleware) NewLogger ¶ added in v0.3.0
func (f *Middleware) NewLogger(h slog.Handler) *slog.Logger
NewLogger returns a new logger with the Middleware.
func (*Middleware) Wrap ¶ added in v0.3.0
func (f *Middleware) Wrap(h http.Handler) http.Handler
Wrap wraps the given http.Handler with the middleware. The middleware generates a request ID and sets it to the request context.
func (*Middleware) WrapSlogHandler ¶ added in v0.3.0
func (f *Middleware) WrapSlogHandler(h slog.Handler) slog.Handler
WrapSlogHandler wraps the given slog.Handler with the Middleware.
type Option ¶
type Option func(o *Options)
Option represents the option for the request ID middleware and logging request ID with slog.
func RequestHeader ¶
RequestHeader sets the request header name for the request ID. Set "X-Cloud-Trace-Context" for Google Cloud https://cloud.google.com/trace/docs/trace-context?hl=ja#http-requests Set "X-Amzn-Trace-Id" for AWS https://docs.aws.amazon.com/ja_jp/elasticloadbalancing/latest/application/load-balancer-request-tracing.html
func ResponseHeader ¶
ResponseHeader sets the response header name for the request ID. Skip setting the response header by setting an empty string.
func SlogwNamespace ¶ added in v0.2.0
SlogwNamespace sets the slogctx.Namespace for logging request ID with slog.