9 Commits

Author SHA1 Message Date
bd6ebc31d6 optimize
Some checks failed
build / test (push) Failing after 1m18s
codeql / analyze (go) (push) Failing after 2m24s
build / lint (push) Successful in 9m24s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-10 22:14:28 +03:00
9c5c08c188 update for latest micro
Some checks failed
build / test (push) Failing after 50s
build / lint (push) Successful in 39s
codeql / analyze (go) (push) Failing after 1m23s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-10-17 01:12:17 +03:00
6bb29d8d0e fix xid
Some checks failed
codeql / analyze (go) (push) Failing after 3m16s
build / test (push) Failing after 1m29s
build / lint (push) Failing after 2m35s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-24 12:29:47 +03:00
c069636bf3 Merge pull request 'fix server subscriber' (#103) from idfix into master
Some checks failed
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m40s
codeql / analyze (go) (push) Failing after 3m5s
Reviewed-on: #103
2023-08-24 09:32:34 +03:00
a34c33e25b fix server subscriber
Some checks failed
codeql / analyze (go) (pull_request) Failing after 3m17s
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m30s
prbuild / lint (pull_request) Failing after 2m34s
autoapprove / autoapprove (pull_request) Failing after 1m27s
automerge / automerge (pull_request) Failing after 4s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-24 09:26:45 +03:00
fdffd31ed3 Merge pull request 'insert request id in context' (#101) from rr into master
Some checks failed
build / test (push) Failing after 1m30s
build / lint (push) Failing after 2m34s
codeql / analyze (go) (push) Failing after 3m9s
Reviewed-on: #101
2023-08-23 16:34:27 +03:00
1a3daf4263 insert request id in context
Some checks failed
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m29s
prbuild / lint (pull_request) Failing after 2m38s
autoapprove / autoapprove (pull_request) Failing after 1m25s
automerge / automerge (pull_request) Failing after 4s
codeql / analyze (go) (pull_request) Failing after 3m12s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-23 16:34:04 +03:00
a7932622b2 Merge pull request 'move to micro v4' (#98) from v4 into master
Reviewed-on: #98
2023-05-07 19:41:34 +03:00
300d1fe705 move to micro v4
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-05-07 19:41:15 +03:00
4 changed files with 63 additions and 80 deletions

8
go.mod
View File

@@ -1,5 +1,7 @@
module go.unistack.org/micro-wrapper-requestid/v3 module go.unistack.org/micro-wrapper-requestid/v4
go 1.16 go 1.20
require go.unistack.org/micro/v3 v3.10.19 require go.unistack.org/micro/v4 v4.0.14
require golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect

11
go.sum
View File

@@ -1,7 +1,4 @@
github.com/imdario/mergo v0.3.14/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= go.unistack.org/micro/v4 v4.0.14 h1:zN+aVfTceei3TPzuA7f2wAXLYHoSHWpx2XgW8yqaTIA=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= go.unistack.org/micro/v4 v4.0.14/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
go.unistack.org/micro/v3 v3.10.19 h1:JtPacglETxHyb+GiuMdfxqGtnWSTYXPQm6PlDQfNIl8= golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
go.unistack.org/micro/v3 v3.10.19/go.mod h1:XIArw29f0b3uvF4cq96X/nQt2f0J2OGnjh8J+DBbC0s=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -1,48 +1,61 @@
package requestid // import "go.unistack.org/micro-wrapper-requestid/v3" package requestid
import ( import (
"context" "context"
"net/textproto" "net/textproto"
"go.unistack.org/micro/v3/client" "go.unistack.org/micro/v4/client"
"go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v3/server" "go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v3/util/id" "go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/server"
"go.unistack.org/micro/v4/util/id"
) )
var XRequestIDKey struct{} func init() {
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, func(ctx context.Context) []interface{} {
if v, ok := ctx.Value(XRequestIDKey{}).(string); ok {
return []interface{}{DefaultMetadataKey, v}
}
return nil
})
}
// DefaultMetadataKey contains metadata key type XRequestIDKey struct{}
// DefaultMetadataKey contains metadata key x-request-id
var DefaultMetadataKey = textproto.CanonicalMIMEHeaderKey("x-request-id") var DefaultMetadataKey = textproto.CanonicalMIMEHeaderKey("x-request-id")
// DefaultMetadataFunc wil be used if user not provide own func to fill metadata // DefaultMetadataFunc wil be used if user not provide own func to fill metadata
var DefaultMetadataFunc = func(ctx context.Context) (context.Context, error) { var DefaultMetadataFunc = func(ctx context.Context) (context.Context, error) {
imd, ok := metadata.FromIncomingContext(ctx) var xid string
if !ok { var err error
imd = metadata.New(1)
} if _, ok := ctx.Value(XRequestIDKey{}).(string); !ok {
omd, ok := metadata.FromOutgoingContext(ctx) xid, err = id.New()
if !ok {
omd = metadata.New(1)
}
v, iok := imd.Get(DefaultMetadataKey)
if iok {
if _, ook := omd.Get(DefaultMetadataKey); ook {
return ctx, nil
}
}
if !iok {
uid, err := id.New()
if err != nil { if err != nil {
return ctx, err return ctx, err
} }
v = uid ctx = context.WithValue(ctx, XRequestIDKey{}, xid)
}
imd, ok := metadata.FromIncomingContext(ctx)
if !ok {
imd = metadata.New(1)
imd.Set(DefaultMetadataKey, xid)
ctx = metadata.NewIncomingContext(ctx, imd)
} else if _, ok = imd.Get(DefaultMetadataKey); !ok {
imd.Set(DefaultMetadataKey, xid)
}
omd, ok := metadata.FromOutgoingContext(ctx)
if !ok {
omd = metadata.New(1)
omd.Set(DefaultMetadataKey, xid)
ctx = metadata.NewOutgoingContext(ctx, imd)
} else if _, ok = omd.Get(DefaultMetadataKey); !ok {
omd.Set(DefaultMetadataKey, xid)
} }
imd.Set(DefaultMetadataKey, v)
omd.Set(DefaultMetadataKey, v)
ctx = context.WithValue(ctx, XRequestIDKey, v)
ctx = metadata.NewIncomingContext(ctx, imd)
ctx = metadata.NewOutgoingContext(ctx, omd)
return ctx, nil return ctx, nil
} }
@@ -72,7 +85,7 @@ func NewClientCallWrapper() client.CallWrapper {
} }
} }
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...options.Option) error {
var err error var err error
if ctx, err = DefaultMetadataFunc(ctx); err != nil { if ctx, err = DefaultMetadataFunc(ctx); err != nil {
return err return err
@@ -80,7 +93,7 @@ func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{},
return w.Client.Call(ctx, req, rsp, opts...) return w.Client.Call(ctx, req, rsp, opts...)
} }
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...options.Option) (client.Stream, error) {
var err error var err error
if ctx, err = DefaultMetadataFunc(ctx); err != nil { if ctx, err = DefaultMetadataFunc(ctx); err != nil {
return nil, err return nil, err
@@ -88,14 +101,6 @@ func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client
return w.Client.Stream(ctx, req, opts...) return w.Client.Stream(ctx, req, opts...)
} }
func (w *wrapper) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
var err error
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
return err
}
return w.Client.Publish(ctx, msg, opts...)
}
func NewServerHandlerWrapper() server.HandlerWrapper { func NewServerHandlerWrapper() server.HandlerWrapper {
return func(fn server.HandlerFunc) server.HandlerFunc { return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error { return func(ctx context.Context, req server.Request, rsp interface{}) error {
@@ -107,28 +112,3 @@ func NewServerHandlerWrapper() server.HandlerWrapper {
} }
} }
} }
func NewServerSubscriberWrapper() server.SubscriberWrapper {
return func(fn server.SubscriberFunc) server.SubscriberFunc {
return func(ctx context.Context, msg server.Message) error {
var err error
imd, ok := metadata.FromIncomingContext(ctx)
if !ok {
imd = metadata.New(1)
}
omd, ok := metadata.FromOutgoingContext(ctx)
if !ok {
omd = metadata.New(1)
}
if id, ok := msg.Header()[DefaultMetadataKey]; ok {
imd.Set(DefaultMetadataKey, id)
omd.Set(DefaultMetadataKey, id)
ctx = metadata.NewIncomingContext(ctx, imd)
ctx = metadata.NewOutgoingContext(ctx, omd)
} else if ctx, err = DefaultMetadataFunc(ctx); err != nil {
return err
}
return fn(ctx, msg)
}
}
}

View File

@@ -4,30 +4,34 @@ import (
"context" "context"
"testing" "testing"
"go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v4/metadata"
) )
func TestDefaultMetadataFunc(t *testing.T) { func TestDefaultMetadataFunc(t *testing.T) {
ctx := context.TODO() ctx := context.TODO()
var err error
nctx, err := DefaultMetadataFunc(ctx) ctx, err = DefaultMetadataFunc(ctx)
if err != nil { if err != nil {
t.Fatalf("%v", err) t.Fatalf("%v", err)
} }
imd, ok := metadata.FromIncomingContext(nctx) imd, ok := metadata.FromIncomingContext(ctx)
if !ok { if !ok {
t.Fatalf("md missing in incoming context") t.Fatalf("md missing in incoming context")
} }
omd, ok := metadata.FromOutgoingContext(nctx) omd, ok := metadata.FromOutgoingContext(ctx)
if !ok { if !ok {
t.Fatalf("md missing in outgoing context") t.Fatalf("md missing in outgoing context")
} }
_, iok := imd.Get(DefaultMetadataKey) iv, iok := imd.Get(DefaultMetadataKey)
_, ook := omd.Get(DefaultMetadataKey) ov, ook := omd.Get(DefaultMetadataKey)
if !iok || !ook { if !iok || !ook {
t.Fatalf("missing metadata key value") t.Fatalf("missing metadata key value")
} }
if iv != ov {
t.Fatalf("invalid metadata key value")
}
} }