Documentation
¶
Index ¶
- Variables
- func FromContext(ctx context.Context) string
- func LogHandler(handler slog.Handler) slog.Handler
- func Middleware(next http.Handler) http.Handler
- func NewContext(ctx context.Context) context.Context
- func SetHeader(ctx context.Context, req *http.Request)
- func Transport(next http.RoundTripper) http.RoundTripper
- type RoundTripFunc
Constants ¶
This section is empty.
Variables ¶
View Source
var Header = http.CanonicalHeaderKey("TraceId")
View Source
var LogKey string = "traceId"
Functions ¶
func FromContext ¶
func LogHandler ¶ added in v0.3.0
You can pass slog.Handler, and all the logs would automatically log the traceId if logging with context is used.
slog.Log() slog.DebugContext() slog.InfoContext() slog.WarnContext() slog.ErrorContext()
Example:
ctx := traceid.NewContext(context.Background()) handler := traceid.LogHandler(slog.NewJSONHandler(os.Stdout, nil)) logger := slog.New(handler) logger.InfoContext(ctx, "message")
This would log
{"time":"2025-04-01T13:17:43.097789397Z","level":"INFO","msg":"message","traceId":"0195f180-2939-7bc4-bffe-838eb3c62526"}
func Transport ¶ added in v0.2.0
func Transport(next http.RoundTripper) http.RoundTripper
Types ¶
type RoundTripFunc ¶ added in v0.2.0
RoundTripFunc, similar to http.HandlerFunc, is an adapter to allow the use of ordinary functions as http.RoundTrippers.
Click to show internal directories.
Click to hide internal directories.