Compare commits

...

16 Commits
master ... v3

Author SHA1 Message Date
f85cbcdca2 rework
Some checks failed
build / test (push) Failing after 1m16s
codeql / analyze (go) (push) Failing after 1m37s
build / lint (push) Successful in 9m16s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-04-23 22:32:49 +03:00
8edb53d141 update for latest micro
Some checks failed
build / test (push) Failing after 1m17s
codeql / analyze (go) (push) Failing after 1m37s
build / lint (push) Successful in 9m15s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-04-23 10:24:09 +03:00
376dd1ad3a update for latest micro
Some checks failed
build / test (push) Failing after 1m15s
codeql / analyze (go) (push) Failing after 1m35s
build / lint (push) Successful in 9m22s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-04-23 09:51:43 +03:00
50c1147563 fixup metadata
Some checks failed
build / test (push) Failing after 1m11s
codeql / analyze (go) (push) Failing after 1m32s
build / lint (push) Successful in 9m14s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-12 01:56:23 +03:00
25efb945dc fixup logger field and metadata handling
Some checks failed
build / test (push) Failing after 1m35s
codeql / analyze (go) (push) Failing after 2m6s
build / lint (push) Successful in 9m35s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-11 23:53:43 +03:00
c4b4fc187e update workflow
Some checks failed
build / test (push) Failing after 1m20s
codeql / analyze (go) (push) Failing after 1m50s
build / lint (push) Failing after 16m37s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-10 22:35:00 +03:00
c2e8e53630 update go.mod
Some checks failed
build / test (push) Waiting to run
build / lint (push) Waiting to run
codeql / analyze (go) (push) Has been cancelled
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-10 22:34:01 +03:00
adb7e70dc6 optimize
Some checks failed
build / test (push) Has been cancelled
build / lint (push) Has been cancelled
codeql / analyze (go) (push) Has been cancelled
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-10 22:33:21 +03:00
3c345f4e22 Merge pull request 'xid fix' (#104) from xid into v3
Some checks failed
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m27s
codeql / analyze (go) (push) Failing after 3m8s
Reviewed-on: #104
2023-08-24 12:24:26 +03:00
30cc426c91 xid fix
Some checks failed
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m29s
prbuild / lint (pull_request) Failing after 2m40s
autoapprove / autoapprove (pull_request) Failing after 1m25s
automerge / automerge (pull_request) Failing after 5s
codeql / analyze (go) (pull_request) Failing after 3m9s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-24 12:24:03 +03:00
c1fa0f991d Merge pull request 'fix server subscriber' (#102) from idfix into v3
Some checks failed
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m44s
codeql / analyze (go) (push) Failing after 3m4s
Reviewed-on: #102
2023-08-24 09:25:28 +03:00
a90681e18d fix server subscriber
Some checks failed
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 1m31s
prbuild / lint (pull_request) Failing after 2m32s
autoapprove / autoapprove (pull_request) Failing after 1m25s
automerge / automerge (pull_request) Failing after 4s
codeql / analyze (go) (pull_request) Failing after 3m5s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-24 09:25:03 +03:00
f163a2de72 Merge pull request 'insert request id in context' (#100) from rq into v3
Some checks failed
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m45s
codeql / analyze (go) (push) Failing after 3m13s
Reviewed-on: #100
2023-08-23 16:32:38 +03:00
9170371647 insert request id in context
Some checks failed
autoapprove / autoapprove (pull_request) Failing after 1m25s
automerge / automerge (pull_request) Failing after 4s
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 2m30s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-23 16:32:14 +03:00
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
5 changed files with 131 additions and 97 deletions

View File

@ -12,7 +12,7 @@ jobs:
- name: setup
uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.20
- name: checkout
uses: actions/checkout@v3
- name: cache

View File

@ -12,7 +12,7 @@ jobs:
- name: setup
uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.20
- name: checkout
uses: actions/checkout@v3
- name: cache

11
go.mod
View File

@ -1,5 +1,12 @@
module go.unistack.org/micro-wrapper-requestid/v3
go 1.16
go 1.20
require go.unistack.org/micro/v3 v3.10.19
require go.unistack.org/micro/v3 v3.10.66
require (
golang.org/x/sys v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)

20
go.sum
View File

@ -1,7 +1,13 @@
github.com/imdario/mergo v0.3.14/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
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=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
go.unistack.org/micro/v3 v3.10.66 h1:tiG8HnyTC71IZWSC2qT/DmLhJinZJL9qvw+4Fvpm3d4=
go.unistack.org/micro/v3 v3.10.66/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be h1:LG9vZxsWGOmUKieR8wPAUR3u3MpnYFQZROPIMaXh7/A=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

View File

@ -3,128 +3,149 @@ package requestid // import "go.unistack.org/micro-wrapper-requestid/v3"
import (
"context"
"net/textproto"
"strings"
"go.unistack.org/micro/v3/client"
"go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/metadata"
"go.unistack.org/micro/v3/server"
"go.unistack.org/micro/v3/util/id"
)
func init() {
requestIDLog := strings.ToLower(DefaultMetadataKey)
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, func(ctx context.Context) []interface{} {
if v, ok := ctx.Value(XRequestIDKey{}).(string); ok {
return []interface{}{requestIDLog, v}
}
return nil
})
}
type XRequestIDKey struct{}
// DefaultMetadataKey contains metadata key
var DefaultMetadataKey = textproto.CanonicalMIMEHeaderKey("x-request-id")
// DefaultMetadataFunc wil be used if user not provide own func to fill metadata
var DefaultMetadataFunc = func(ctx context.Context) (context.Context, error) {
imd, ok := metadata.FromIncomingContext(ctx)
if !ok {
var xid string
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, ok := metadata.FromOutgoingContext(ctx)
if !ok {
omd, ook := metadata.FromOutgoingContext(ctx)
if !ook || omd == nil {
omd = metadata.New(1)
ctx = metadata.NewOutgoingContext(ctx, omd)
}
v, iok := imd.Get(DefaultMetadataKey)
if iok {
if _, ook := omd.Get(DefaultMetadataKey); ook {
return ctx, nil
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 !iok {
uid, err := id.New()
if xid == "" {
var err error
xid, err = id.New()
if err != nil {
return ctx, err
}
v = uid
}
imd.Set(DefaultMetadataKey, v)
omd.Set(DefaultMetadataKey, v)
ctx = metadata.NewIncomingContext(ctx, imd)
ctx = metadata.NewOutgoingContext(ctx, omd)
if !cok {
ctx = context.WithValue(ctx, XRequestIDKey{}, xid)
}
if !iok {
imd.Set(DefaultMetadataKey, xid)
}
if !ook {
omd.Set(DefaultMetadataKey, xid)
}
return ctx, nil
}
type wrapper struct {
client.Client
type hook struct{}
func NewHook() *hook {
return &hook{}
}
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 {
func (w *hook) ServerSubscriber(next server.FuncSubHandler) server.FuncSubHandler {
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
}
return fn(ctx, addr, req, rsp, opts)
}
return next(ctx, msg)
}
}
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
var err error
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
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
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
return nil, err
}
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 {
return func(fn server.HandlerFunc) server.HandlerFunc {
func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
var err error
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
return err
}
return fn(ctx, req, rsp)
}
return next(ctx, req, rsp)
}
}
func NewServerSubscriberWrapper() server.SubscriberWrapper {
return func(fn server.SubscriberFunc) server.SubscriberFunc {
return func(ctx context.Context, msg server.Message) error {
func (w *hook) ClientBatchPublish(next client.FuncBatchPublish) client.FuncBatchPublish {
return func(ctx context.Context, msgs []client.Message, opts ...client.PublishOption) 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 {
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
return err
}
return fn(ctx, msg)
}
return next(ctx, msgs, opts...)
}
}
func (w *hook) ClientPublish(next client.FuncPublish) client.FuncPublish {
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...)
}
}
func (w *hook) ClientCall(next client.FuncCall) client.FuncCall {
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
}
return next(ctx, req, rsp, opts...)
}
}
func (w *hook) ClientStream(next client.FuncStream) client.FuncStream {
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
}
return next(ctx, req, opts...)
}
}