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
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) 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 w.Client.Call(ctx, req, rsp, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, 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 nil, err
|
|
|
|
}
|
|
|
|
return w.Client.Stream(ctx, req, opts...)
|
|
|
|
}
|
|
|
|
|
2020-12-17 01:57:53 +03:00
|
|
|
func (w *wrapper) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) 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
|
|
|
|
}
|
2020-12-17 01:57:53 +03:00
|
|
|
return w.Client.Publish(ctx, msg, opts...)
|
2020-12-16 14:05:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServerSubscriberWrapper() server.SubscriberWrapper {
|
|
|
|
return func(fn server.SubscriberFunc) server.SubscriberFunc {
|
|
|
|
return func(ctx context.Context, msg server.Message) error {
|
|
|
|
var err error
|
2024-03-10 22:33:21 +03:00
|
|
|
if xid, ok := msg.Header()[DefaultMetadataKey]; ok {
|
|
|
|
ctx = context.WithValue(ctx, XRequestIDKey{}, xid)
|
2022-12-18 20:06:49 +03:00
|
|
|
}
|
2024-03-10 22:33:21 +03:00
|
|
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
2020-12-16 14:05:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(ctx, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|