2 Commits

Author SHA1 Message Date
a12fb08b7e Merge pull request 'add request id in context' (#99) from rq into v3
Some checks failed
codeql / analyze (go) (push) Failing after 3m9s
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m38s
Reviewed-on: #99
2023-08-23 16:31:27 +03:00
d03bf1aa14 insert request id in context
Some checks failed
codeql / analyze (go) (pull_request) Failing after 3m15s
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m29s
prbuild / lint (pull_request) Failing after 2m37s
autoapprove / autoapprove (pull_request) Failing after 1m26s
automerge / automerge (pull_request) Failing after 4s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-23 16:30:47 +03:00
4 changed files with 19 additions and 15 deletions

6
go.mod
View File

@@ -1,5 +1,5 @@
module go.unistack.org/micro-wrapper-requestid/v4 module go.unistack.org/micro-wrapper-requestid/v3
go 1.20 go 1.16
require go.unistack.org/micro/v4 v4.0.1 require go.unistack.org/micro/v3 v3.10.19

9
go.sum
View File

@@ -1,2 +1,7 @@
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo= github.com/imdario/mergo v0.3.14/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
go.unistack.org/micro/v3 v3.10.19 h1:JtPacglETxHyb+GiuMdfxqGtnWSTYXPQm6PlDQfNIl8=
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,18 +1,18 @@
package requestid // import "go.unistack.org/micro-wrapper-requestid/v4" package requestid // import "go.unistack.org/micro-wrapper-requestid/v3"
import ( import (
"context" "context"
"net/textproto" "net/textproto"
"go.unistack.org/micro/v4/client" "go.unistack.org/micro/v3/client"
"go.unistack.org/micro/v4/metadata" "go.unistack.org/micro/v3/metadata"
"go.unistack.org/micro/v4/server" "go.unistack.org/micro/v3/server"
"go.unistack.org/micro/v4/util/id" "go.unistack.org/micro/v3/util/id"
) )
var XRequestIDKey struct{} var XRequestIDKey struct{}
// DefaultMetadataKey contains metadata key x-request-id // DefaultMetadataKey contains metadata key
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
@@ -40,10 +40,10 @@ var DefaultMetadataFunc = func(ctx context.Context) (context.Context, error) {
} }
imd.Set(DefaultMetadataKey, v) imd.Set(DefaultMetadataKey, v)
omd.Set(DefaultMetadataKey, v) omd.Set(DefaultMetadataKey, v)
ctx = context.WithValue(ctx, XRequestIDKey, v)
ctx = metadata.NewIncomingContext(ctx, imd) ctx = metadata.NewIncomingContext(ctx, imd)
ctx = metadata.NewOutgoingContext(ctx, omd) ctx = metadata.NewOutgoingContext(ctx, omd)
return ctx, nil
return context.WithValue(ctx, XRequestIDKey, v), nil
} }
type wrapper struct { type wrapper struct {
@@ -122,7 +122,6 @@ func NewServerSubscriberWrapper() server.SubscriberWrapper {
if id, ok := msg.Header()[DefaultMetadataKey]; ok { if id, ok := msg.Header()[DefaultMetadataKey]; ok {
imd.Set(DefaultMetadataKey, id) imd.Set(DefaultMetadataKey, id)
omd.Set(DefaultMetadataKey, id) omd.Set(DefaultMetadataKey, id)
ctx = context.WithValue(ctx, XRequestIDKey, id)
ctx = metadata.NewIncomingContext(ctx, imd) ctx = metadata.NewIncomingContext(ctx, imd)
ctx = metadata.NewOutgoingContext(ctx, omd) ctx = metadata.NewOutgoingContext(ctx, omd)
} else if ctx, err = DefaultMetadataFunc(ctx); err != nil { } else if ctx, err = DefaultMetadataFunc(ctx); err != nil {

View File

@@ -4,7 +4,7 @@ import (
"context" "context"
"testing" "testing"
"go.unistack.org/micro/v4/metadata" "go.unistack.org/micro/v3/metadata"
) )
func TestDefaultMetadataFunc(t *testing.T) { func TestDefaultMetadataFunc(t *testing.T) {