2021-10-26 23:47:53 +03:00
|
|
|
package requestid // import "go.unistack.org/micro-wrapper-requestid/v3"
|
2020-12-16 14:05:53 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/textproto"
|
2024-03-11 23:53:43 +03:00
|
|
|
"strings"
|
2020-12-16 14:05:53 +03:00
|
|
|
|
2021-10-26 23:47:53 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
2024-03-10 22:33:21 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2021-10-26 23:47:53 +03:00
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
|
|
|
"go.unistack.org/micro/v3/util/id"
|
2020-12-16 14:05:53 +03:00
|
|
|
)
|
|
|
|
|
2024-03-10 22:33:21 +03:00
|
|
|
func init() {
|
2024-03-11 23:53:43 +03:00
|
|
|
requestIDLog := strings.ToLower(DefaultMetadataKey)
|
2024-03-10 22:33:21 +03:00
|
|
|
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, func(ctx context.Context) []interface{} {
|
|
|
|
if v, ok := ctx.Value(XRequestIDKey{}).(string); ok {
|
2024-03-11 23:53:43 +03:00
|
|
|
return []interface{}{requestIDLog, v}
|
2024-03-10 22:33:21 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type XRequestIDKey struct{}
|
2023-08-23 16:30:47 +03:00
|
|
|
|
2022-12-18 20:06:49 +03:00
|
|
|
// DefaultMetadataKey contains metadata key
|
|
|
|
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:24:03 +03:00
|
|
|
var xid string
|
|
|
|
|
2024-03-12 01:56:23 +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:24:03 +03:00
|
|
|
xid, err = id.New()
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
2024-03-12 01:56:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !cok {
|
2024-03-10 22:33:21 +03:00
|
|
|
ctx = context.WithValue(ctx, XRequestIDKey{}, xid)
|
2023-08-24 12:24:03 +03:00
|
|
|
}
|
|
|
|
|
2024-03-12 01:56:23 +03:00
|
|
|
if !iok {
|
2023-08-24 12:24:03 +03:00
|
|
|
imd.Set(DefaultMetadataKey, xid)
|
2021-09-06 18:21:01 +03:00
|
|
|
}
|
2023-08-24 12:24:03 +03:00
|
|
|
|
2024-03-12 01:56:23 +03:00
|
|
|
if !ook {
|
2023-08-24 12:24:03 +03:00
|
|
|
omd.Set(DefaultMetadataKey, xid)
|
2021-09-06 18:21:01 +03:00
|
|
|
}
|
2023-08-23 16:30:47 +03:00
|
|
|
|
2023-08-23 16:32:14 +03:00
|
|
|
return ctx, nil
|
2021-09-06 18:21:01 +03:00
|
|
|
}
|
2020-12-16 14:05:53 +03:00
|
|
|
|
2024-04-23 10:24:09 +03:00
|
|
|
type hook struct{}
|
2020-12-16 14:05:53 +03:00
|
|
|
|
2024-04-23 22:32:49 +03:00
|
|
|
func NewHook() *hook {
|
|
|
|
return &hook{}
|
|
|
|
}
|
2020-12-16 14:05:53 +03:00
|
|
|
|
2024-04-23 10:24:09 +03:00
|
|
|
func (w *hook) ServerSubscriber(next server.FuncSubHandler) server.FuncSubHandler {
|
2024-04-23 09:51:43 +03:00
|
|
|
return func(ctx context.Context, msg server.Message) error {
|
|
|
|
var err error
|
|
|
|
if xid, ok := msg.Header()[DefaultMetadataKey]; ok {
|
|
|
|
ctx = context.WithValue(ctx, XRequestIDKey{}, xid)
|
|
|
|
}
|
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
|
|
|
return err
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
2024-04-23 09:51:43 +03:00
|
|
|
return next(ctx, msg)
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 10:24:09 +03:00
|
|
|
func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler {
|
2024-04-23 09:51:43 +03:00
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
var err error
|
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return next(ctx, req, rsp)
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 10:24:09 +03:00
|
|
|
func (w *hook) ClientBatchPublish(next client.FuncBatchPublish) client.FuncBatchPublish {
|
2024-04-23 09:51:43 +03:00
|
|
|
return func(ctx context.Context, msgs []client.Message, opts ...client.PublishOption) error {
|
|
|
|
var err error
|
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return next(ctx, msgs, opts...)
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 10:24:09 +03:00
|
|
|
func (w *hook) ClientPublish(next client.FuncPublish) client.FuncPublish {
|
2024-04-23 09:51:43 +03:00
|
|
|
return func(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
|
|
|
|
var err error
|
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return next(ctx, msg, opts...)
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 10:24:09 +03:00
|
|
|
func (w *hook) ClientCall(next client.FuncCall) client.FuncCall {
|
2024-04-23 09:51:43 +03:00
|
|
|
return func(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
|
|
|
var err error
|
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
|
|
|
return err
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
2024-04-23 09:51:43 +03:00
|
|
|
return next(ctx, req, rsp, opts...)
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 10:24:09 +03:00
|
|
|
func (w *hook) ClientStream(next client.FuncStream) client.FuncStream {
|
2024-04-23 09:51:43 +03:00
|
|
|
return func(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
|
|
|
var err error
|
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
|
|
|
return nil, err
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
2024-04-23 09:51:43 +03:00
|
|
|
return next(ctx, req, opts...)
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
|
|
|
}
|