Documentation
¶
Overview ¶
Package traceid provide a simple mechanism to save/extract a Trace ID HTTP header to/from a context.Context and http.Request.
Index ¶
- Constants
- func FromContext(ctx context.Context, defaultValue string) string
- func FromHTTPRequestHeader(r *http.Request, header, defaultValue string) string
- func NewContext(ctx context.Context, id string) context.Context
- func SetHTTPRequestHeaderFromContext(ctx context.Context, r *http.Request, header, defaultValue string) string
Examples ¶
Constants ¶
const ( // DefaultHeader is the default header name for the trace ID. DefaultHeader = "X-Request-ID" // DefaultValue is the default trace ID value. DefaultValue = "" // DefaultLogKey is the default log field key for the Trace ID. DefaultLogKey = "traceid" )
Variables ¶
This section is empty.
Functions ¶
func FromContext ¶
FromContext returns the trace ID associated with the context. If no trace ID is associated, then the default value returned.
func FromHTTPRequestHeader ¶
FromHTTPRequestHeader retrieves the trace ID from an HTTP Request. If not found the default value is returned instead.
func NewContext ¶
NewContext stores the trace ID value in the context if not already present.
Example ¶
Output: test-1-218549 context.Background.WithValue(type traceid.ctxKey, val test-1-218549)
func SetHTTPRequestHeaderFromContext ¶
func SetHTTPRequestHeaderFromContext(ctx context.Context, r *http.Request, header, defaultValue string) string
SetHTTPRequestHeaderFromContext set the trace ID HTTP Request Header with the value retrieved from the context. If the traceid is not found in the context, then the default value is set. Returns the set ID.
Types ¶
This section is empty.