Help us shape the future of Go! Take the 2026 Go Developer Survey

requestmeta

package
v1.20.1 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package requestmeta carries bounded HTTP request metadata across application layers and queue boundaries. It intentionally stores the raw User-Agent and the address resolved by the HTTP framework without parsing either value.

Index

Constants

View Source
const (
	MaxClientIPBytes       = 255
	MaxUserAgentBytes      = 512
	MaxCountryCodeBytes    = 8
	MaxCountryBytes        = 128
	MaxRegionBytes         = 128
	MaxCityBytes           = 128
	MaxASOrganizationBytes = 256
)

Variables

This section is empty.

Functions

func Bound

func Bound(value string, maxBytes int) string

Bound truncates an attacker-controlled value by bytes while preserving valid UTF-8. This keeps audit rows and access-log entries bounded.

func With

func With(ctx context.Context, metadata Metadata) context.Context

With stores request metadata in ctx. Empty metadata is still stored so an authenticated actor can be attached later by middleware.

func WithActor

func WithActor(ctx context.Context, actorID int64) context.Context

WithActor returns a copy of ctx metadata carrying the authenticated caller.

Types

type Enricher

type Enricher func(Metadata) Metadata

Enricher adds derived metadata to a request. Implementations must be best-effort: unavailable databases and lookup misses must not fail requests.

type IPMetadata

type IPMetadata struct {
	IPCountryCode    string `json:"ip_country_code,omitempty"`
	IPCountry        string `json:"ip_country,omitempty"`
	IPRegion         string `json:"ip_region,omitempty"`
	IPCity           string `json:"ip_city,omitempty"`
	IPASN            uint   `json:"ip_asn,omitempty"`
	IPASOrganization string `json:"ip_as_organization,omitempty"`
}

IPMetadata is derived locally from MaxMind databases. ASNOrganization is the organization registered for the autonomous system; it is useful for risk analysis but is not guaranteed to be the end user's retail ISP.

type Metadata

type Metadata struct {
	ClientIP  string `json:"client_ip,omitempty"`
	UserAgent string `json:"user_agent,omitempty"`
	ActorID   int64  `json:"actor_id,omitempty"`
	IPMetadata
}

Metadata identifies the request that caused an auditable operation. ActorID is the authenticated caller. It can differ from the object ID of an administrative log entry, which identifies the account being changed.

func From

func From(ctx context.Context) (Metadata, bool)

From returns request metadata when the context originated from HTTP or was explicitly restored from a durable queue payload.

func New

func New(clientIP, userAgent string) Metadata

New returns bounded metadata suitable for logs and durable queue payloads.

func Normalize

func Normalize(metadata Metadata) Metadata

Normalize bounds every string field before it reaches a process log, audit row or durable queue payload.

Jump to

Keyboard shortcuts

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