ctxutil

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package ctxutil provides small, domain-agnostic helpers for threading values through context.Context. It currently carries an HTTP request id so a single id can be propagated across handler, service, and worker boundaries without callers threading it explicitly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestIDFrom

func RequestIDFrom(ctx context.Context) string

RequestIDFrom returns the request id stamped on ctx, or "" when none is present. Services and workers use this to read the id without taking a dependency on the HTTP layer that originally set it.

func RequestIDFromMetadata added in v0.3.2

func RequestIDFromMetadata(raw []byte) string

RequestIDFromMetadata extracts the request id from a JSON metadata blob, or returns "" when none is present. It tolerates an empty or malformed blob and returns "" rather than erroring, so a partial upgrade across an old stored row does not crash the consumer.

func RequestIDToMetadata added in v0.3.2

func RequestIDToMetadata(base []byte, requestID string) []byte

RequestIDToMetadata returns a JSON object blob (always non-empty) carrying the request id under the "request_id" key. base, when non-empty, is merged in first so a caller-supplied metadata blob can carry additional tags alongside the request id; an empty or malformed base is ignored. When requestID is empty the id is simply not stamped. The result is never empty: a marshal failure (or an empty result) falls back to "{}", so callers can persist the return value into a NOT NULL JSON column without branching.

It is the write side of the request-id-over-metadata seam: a producer copies the id from context into a row's metadata at enqueue time, and a consumer reads it back with RequestIDFromMetadata across the storage boundary.

func WithRequestID

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

WithRequestID returns ctx tagged with id. An empty id is a no-op so callers can pass the value through without branching.

Types

This section is empty.

Jump to

Keyboard shortcuts

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