func InjectCorrelationID()

in correlation/inbound_http.go [13:44]


func InjectCorrelationID(h http.Handler, opts ...InboundHandlerOption) http.Handler {
	config := applyInboundHandlerOptions(opts)

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		parent := r.Context()

		correlationID := ""
		clientName := ""

		if config.shouldPropagate(r) {
			correlationID = r.Header.Get(propagationHeader)
			clientName = r.Header.Get(clientNameHeader)
		}

		if correlationID == "" {
			correlationID = SafeRandomID()
		}

		ctx := ContextWithCorrelation(parent, correlationID)

		if clientName != "" {
			ctx = ContextWithClientName(ctx, clientName)
		}

		if config.sendResponseHeader {
			// Set the response header.
			w.Header().Set(propagationHeader, correlationID)
		}

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