Skip to content

Latest commit

 

History

History
29 lines (23 loc) · 708 Bytes

README.md

File metadata and controls

29 lines (23 loc) · 708 Bytes

Trace library

import "github.com/best-expendables/trace"
example in router:
func RequestID(prefix string) func(http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			requestID := trace.RequestIDFromHeader(r.Header)

			if requestID == "" {
				if prefix != "" {
					requestID = prefix + "-"
				}

				requestID += fmt.Sprintf("%06d", middleware.NextRequestID())
			}
			ctx := trace.ContextWithRequestID(r.Context(), requestID)
			// Send old or new response id back to the client
			trace.RequestIDToHeader(w.Header(), requestID)

			next.ServeHTTP(w, r.WithContext(ctx))
		})
	}
}