package jetstream import ( "context" "github.com/nats-io/nats.go" ) func NewContext(msg *nats.Msg) context.Context { ctx := context.Background() ctx = context.WithValue(ctx, "req_id", msg.Header.Get("req_id")) return ctx } func NewMsg(ctx context.Context, subject string, data []byte) *nats.Msg { header := nats.Header{} header.Set("req_id", getContextValue(ctx, "req_id")) return &nats.Msg{ Subject: subject, Data: data, Header: header, } } func getContextValue(ctx context.Context, keyword string) string { if ctx.Value(keyword) != nil { if v, ok := ctx.Value(keyword).(string); ok { return v } } return "" }