traceid

package
v1.81.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 3 Imported by: 0

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

Examples

Constants

View Source
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

func FromContext(ctx context.Context, defaultValue string) string

FromContext returns the trace ID associated with the context. If no trace ID is associated, then the default value returned.

Example
Output:

default-1-206951
default-2-616841

func FromHTTPRequestHeader

func FromHTTPRequestHeader(r *http.Request, header, defaultValue string) string

FromHTTPRequestHeader retrieves the trace ID from an HTTP Request. If not found the default value is returned instead.

Example
Output:

default-1-103993
test-1-413579

func NewContext

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

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.

Example
Output:


test-904117
test-904117

Types

This section is empty.

Jump to

Keyboard shortcuts

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