reqctx

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package reqctx carries request-scoped metadata — a request/correlation ID, the client IP, and the User-Agent — through a context.Context, independent of any HTTP router or logging framework. It depends on nothing beyond the standard library's context package.

reqctx exists as neutral ground between two layers that must never import each other:

  • httpx/middleware (the transport layer) legitimately knows about the inbound *http.Request and chi's request-ID middleware, and is the writer: it populates a request ID, IP, and User-Agent here for every inbound request.
  • logger and audit (application-facing layers) read this metadata without depending on httpx or chi. logger stamps every log line with the request ID; audit stamps every audit_log row with it. Because both read the exact same context key owned by this package, the request ID that reaches a log line and the one that reaches an audit row are provably the same value — see audit.Recorder.Record.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestIDFromContext

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext returns the request/correlation ID previously stored via WithRequestID, or "" if none is present.

func WithRequestID

func WithRequestID(ctx context.Context, id string) context.Context

WithRequestID returns a copy of ctx carrying id as the request/correlation ID. Callers that bridge a specific transport's request ID (e.g. an HTTP middleware reading chi's request ID) call this to make the ID available to logger.Logger.WithContext and audit.Recorder.Record without either of those packages depending on that transport.

func WithRequestInfo

func WithRequestInfo(ctx context.Context, info RequestInfo) context.Context

WithRequestInfo returns a copy of ctx carrying info as the request's client IP and User-Agent.

This is the only way to inject RequestInfo into a context: no context key is exported. Production code populates it via httpx/middleware.RequestContext; tests in any package call this directly instead of reaching into a leaky exported key.

Types

type RequestInfo

type RequestInfo struct {
	IP        string
	UserAgent string
}

RequestInfo holds the client IP and User-Agent extracted from an inbound HTTP request.

func RequestInfoFromContext

func RequestInfoFromContext(ctx context.Context) (RequestInfo, bool)

RequestInfoFromContext returns the RequestInfo previously stored via WithRequestInfo, and whether one was present.

Jump to

Keyboard shortcuts

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