2024-03-10 22:14:28 +03:00
|
|
|
package requestid
|
2020-12-16 14:05:53 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/textproto"
|
2024-03-11 23:52:29 +03:00
|
|
|
"strings"
|
2020-12-16 14:05:53 +03:00
|
|
|
|
2023-05-07 19:41:15 +03:00
|
|
|
"go.unistack.org/micro/v4/client"
|
2023-10-17 01:12:17 +03:00
|
|
|
"go.unistack.org/micro/v4/logger"
|
2023-05-07 19:41:15 +03:00
|
|
|
"go.unistack.org/micro/v4/metadata"
|
2023-08-24 12:29:47 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2023-05-07 19:41:15 +03:00
|
|
|
"go.unistack.org/micro/v4/server"
|
|
|
|
"go.unistack.org/micro/v4/util/id"
|
2020-12-16 14:05:53 +03:00
|
|
|
)
|
|
|
|
|
2023-10-17 01:12:17 +03:00
|
|
|
func init() {
|
2024-03-11 23:52:29 +03:00
|
|
|
requestIDLog := strings.ToLower(DefaultMetadataKey)
|
2023-10-17 01:12:17 +03:00
|
|
|
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, func(ctx context.Context) []interface{} {
|
2024-03-10 22:14:28 +03:00
|
|
|
if v, ok := ctx.Value(XRequestIDKey{}).(string); ok {
|
2024-03-11 23:52:29 +03:00
|
|
|
return []interface{}{requestIDLog, v}
|
2023-10-17 01:12:17 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-10 22:14:28 +03:00
|
|
|
type XRequestIDKey struct{}
|
2023-08-23 16:34:04 +03:00
|
|
|
|
2023-05-07 19:41:15 +03:00
|
|
|
// DefaultMetadataKey contains metadata key x-request-id
|
2022-12-18 20:06:49 +03:00
|
|
|
var DefaultMetadataKey = textproto.CanonicalMIMEHeaderKey("x-request-id")
|
2020-12-16 14:05:53 +03:00
|
|
|
|
2022-12-18 20:06:49 +03:00
|
|
|
// DefaultMetadataFunc wil be used if user not provide own func to fill metadata
|
|
|
|
var DefaultMetadataFunc = func(ctx context.Context) (context.Context, error) {
|
2023-08-24 12:29:47 +03:00
|
|
|
var xid string
|
|
|
|
|
2024-03-12 01:58:03 +03:00
|
|
|
cid, cok := ctx.Value(XRequestIDKey{}).(string)
|
|
|
|
if cok && cid != "" {
|
|
|
|
xid = cid
|
|
|
|
}
|
|
|
|
|
|
|
|
imd, iok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !iok || imd == nil {
|
|
|
|
imd = metadata.New(1)
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, imd)
|
|
|
|
}
|
|
|
|
|
|
|
|
omd, ook := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ook || omd == nil {
|
|
|
|
omd = metadata.New(1)
|
|
|
|
ctx = metadata.NewOutgoingContext(ctx, omd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if xid == "" {
|
|
|
|
var id string
|
|
|
|
if id, iok = imd.Get(DefaultMetadataKey); iok && id != "" {
|
|
|
|
xid = id
|
|
|
|
}
|
|
|
|
if id, ook = omd.Get(DefaultMetadataKey); ook && id != "" {
|
|
|
|
xid = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if xid == "" {
|
|
|
|
var err error
|
2023-08-24 12:29:47 +03:00
|
|
|
xid, err = id.New()
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
2024-03-12 01:58:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !cok {
|
2024-03-10 22:14:28 +03:00
|
|
|
ctx = context.WithValue(ctx, XRequestIDKey{}, xid)
|
2023-08-24 12:29:47 +03:00
|
|
|
}
|
|
|
|
|
2024-03-12 01:58:03 +03:00
|
|
|
if !iok {
|
2023-08-24 12:29:47 +03:00
|
|
|
imd.Set(DefaultMetadataKey, xid)
|
2021-09-06 18:21:01 +03:00
|
|
|
}
|
2023-08-24 12:29:47 +03:00
|
|
|
|
2024-03-12 01:58:03 +03:00
|
|
|
if !ook {
|
2023-08-24 12:29:47 +03:00
|
|
|
omd.Set(DefaultMetadataKey, xid)
|
|
|
|
}
|
|
|
|
|
2021-09-06 18:21:01 +03:00
|
|
|
return ctx, nil
|
|
|
|
}
|
2020-12-16 14:05:53 +03:00
|
|
|
|
|
|
|
type wrapper struct {
|
|
|
|
client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientWrapper() client.Wrapper {
|
|
|
|
return func(c client.Client) client.Client {
|
|
|
|
handler := &wrapper{
|
|
|
|
Client: c,
|
|
|
|
}
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientCallWrapper() client.CallWrapper {
|
|
|
|
return func(fn client.CallFunc) client.CallFunc {
|
|
|
|
return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
|
|
|
var err error
|
2022-12-18 20:06:49 +03:00
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
2020-12-16 14:05:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(ctx, addr, req, rsp, opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 12:29:47 +03:00
|
|
|
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...options.Option) error {
|
2020-12-16 14:05:53 +03:00
|
|
|
var err error
|
2022-12-18 20:06:49 +03:00
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
2020-12-16 14:05:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return w.Client.Call(ctx, req, rsp, opts...)
|
|
|
|
}
|
|
|
|
|
2023-08-24 12:29:47 +03:00
|
|
|
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...options.Option) (client.Stream, error) {
|
2020-12-16 14:05:53 +03:00
|
|
|
var err error
|
2022-12-18 20:06:49 +03:00
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
2020-12-16 14:05:53 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return w.Client.Stream(ctx, req, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServerHandlerWrapper() server.HandlerWrapper {
|
|
|
|
return func(fn server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
var err error
|
2022-12-18 20:06:49 +03:00
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
2020-12-16 14:05:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|