aboutsummaryrefslogtreecommitdiff
path: root/weed/util/request_id.go
blob: 7945e7cc413da61a52235db3536fe49f5f17daaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package util

import (
	"context"
	"net/http"
)

const (
	RequestIdHttpHeader = "X-Request-ID"
	RequestIDKey        = "x-request-id"
)

func GetRequestID(ctx context.Context) string {
	if ctx == nil {
		return ""
	}
	id, _ := ctx.Value(RequestIDKey).(string)
	return id
}

func WithRequestID(ctx context.Context, id string) context.Context {
	return context.WithValue(ctx, RequestIDKey, id)
}

func ReqWithRequestId(req *http.Request, ctx context.Context) {
	req.Header.Set(RequestIdHttpHeader, GetRequestID(ctx))
}