2024-03-10 00:10:33 +03:00
|
|
|
package wrappermetadata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-03-10 00:12:48 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2024-03-10 00:10:33 +03:00
|
|
|
)
|
|
|
|
|
2024-04-23 18:35:53 +03:00
|
|
|
var DefaultMetadataKeys = []string{"x-request-id"}
|
2024-03-10 00:10:33 +03:00
|
|
|
|
2024-04-23 18:35:53 +03:00
|
|
|
type hook struct {
|
|
|
|
keys []string
|
2024-03-10 00:10:33 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 18:35:53 +03:00
|
|
|
func NewHook(keys ...string) *hook {
|
|
|
|
return &hook{keys: keys}
|
2024-03-10 00:10:33 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 18:35:53 +03:00
|
|
|
func metadataCopy(ctx context.Context, keys []string) context.Context {
|
|
|
|
if keys == nil {
|
|
|
|
return ctx
|
2024-03-10 00:10:33 +03:00
|
|
|
}
|
|
|
|
if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil {
|
|
|
|
omd, ook := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ook || omd == nil {
|
2024-04-23 18:35:53 +03:00
|
|
|
omd = metadata.New(len(keys))
|
2024-03-10 00:10:33 +03:00
|
|
|
}
|
2024-04-23 18:35:53 +03:00
|
|
|
for _, k := range keys {
|
2024-03-10 16:31:57 +03:00
|
|
|
if v, ok := imd.Get(k); ok && v != "" {
|
|
|
|
omd.Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ook {
|
|
|
|
ctx = metadata.NewOutgoingContext(ctx, omd)
|
|
|
|
}
|
2024-03-10 00:10:33 +03:00
|
|
|
}
|
2024-04-23 18:35:53 +03:00
|
|
|
return ctx
|
2024-03-10 00:10:33 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 18:35:53 +03:00
|
|
|
func (w *hook) ClientCall(next client.FuncCall) client.FuncCall {
|
|
|
|
return func(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
|
|
|
return next(metadataCopy(ctx, w.keys), req, rsp, opts...)
|
2024-03-10 00:10:33 +03:00
|
|
|
}
|
2024-04-23 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hook) ClientStream(next client.FuncStream) client.FuncStream {
|
|
|
|
return func(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
|
|
|
return next(metadataCopy(ctx, w.keys), req, opts...)
|
2024-03-10 00:10:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 18:35:53 +03:00
|
|
|
func (w *hook) ClientPublish(next client.FuncPublish) client.FuncPublish {
|
|
|
|
return func(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
|
|
|
|
return next(metadataCopy(ctx, w.keys), msg, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hook) ClientBatchPublish(next client.FuncBatchPublish) client.FuncBatchPublish {
|
|
|
|
return func(ctx context.Context, msgs []client.Message, opts ...client.PublishOption) error {
|
|
|
|
return next(metadataCopy(ctx, w.keys), msgs, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
return next(metadataCopy(ctx, w.keys), req, rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *hook) ServerSubscriber(next server.FuncSubHandler) server.FuncSubHandler {
|
|
|
|
return func(ctx context.Context, msg server.Message) error {
|
|
|
|
return next(metadataCopy(ctx, w.keys), msg)
|
2024-03-10 00:10:33 +03:00
|
|
|
}
|
|
|
|
}
|