Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
047bc9eb86 | |||
4b926bb1bd | |||
a424fd9722 | |||
a3f79a7bed | |||
72c01fe7c9 | |||
bd6ebc31d6 | |||
9c5c08c188 | |||
6bb29d8d0e | |||
c069636bf3 | |||
a34c33e25b | |||
fdffd31ed3 | |||
1a3daf4263 | |||
a7932622b2 | |||
300d1fe705 |
2
.github/workflows/codeql-analysis.yml
vendored
2
.github/workflows/codeql-analysis.yml
vendored
@ -47,7 +47,7 @@ jobs:
|
|||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v3
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.17
|
go-version: 1.20
|
||||||
# Initializes the CodeQL tools for scanning.
|
# Initializes the CodeQL tools for scanning.
|
||||||
- name: init
|
- name: init
|
||||||
uses: github/codeql-action/init@v2
|
uses: github/codeql-action/init@v2
|
||||||
|
12
.gitignore
vendored
12
.gitignore
vendored
@ -1,9 +1,3 @@
|
|||||||
# Develop tools
|
|
||||||
/.vscode/
|
|
||||||
/.idea/
|
|
||||||
.idea
|
|
||||||
.vscode
|
|
||||||
|
|
||||||
# Binaries for programs and plugins
|
# Binaries for programs and plugins
|
||||||
*.exe
|
*.exe
|
||||||
*.exe~
|
*.exe~
|
||||||
@ -11,12 +5,6 @@
|
|||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
|
|
||||||
# Folders
|
|
||||||
_obj
|
|
||||||
_test
|
|
||||||
_build
|
|
||||||
.DS_Store
|
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
# Test binary, built with `go test -c`
|
||||||
*.test
|
*.test
|
||||||
|
|
||||||
|
22
go.mod
22
go.mod
@ -1,21 +1,5 @@
|
|||||||
module go.unistack.org/micro-wrapper-requestid/v3
|
module go.unistack.org/micro-wrapper-requestid/v4
|
||||||
|
|
||||||
go 1.22.0
|
go 1.20
|
||||||
|
|
||||||
toolchain go1.23.4
|
require go.unistack.org/micro/v4 v4.0.17
|
||||||
|
|
||||||
require go.unistack.org/micro/v3 v3.11.37
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/ash3in/uuidv8 v1.2.0 // indirect
|
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
|
||||||
github.com/kr/pretty v0.3.1 // indirect
|
|
||||||
github.com/matoous/go-nanoid v1.5.1 // indirect
|
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
|
|
||||||
golang.org/x/sys v0.28.0 // indirect
|
|
||||||
golang.org/x/text v0.21.0 // indirect
|
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 // indirect
|
|
||||||
google.golang.org/grpc v1.69.2 // indirect
|
|
||||||
google.golang.org/protobuf v1.36.1 // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
|
||||||
)
|
|
||||||
|
44
go.sum
44
go.sum
@ -1,42 +1,2 @@
|
|||||||
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
go.unistack.org/micro/v4 v4.0.17 h1:mF7uM+J4ILdG+1fcwzKYCwDlxhdbF/e1WnGzKKLnIXc=
|
||||||
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
go.unistack.org/micro/v4 v4.0.17/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
||||||
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI=
|
|
||||||
github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
|
|
||||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
|
||||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
|
||||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
|
||||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
|
||||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
|
||||||
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
|
|
||||||
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
|
|
||||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
|
||||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
|
||||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
|
||||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
|
||||||
go.unistack.org/micro/v3 v3.11.37 h1:ZcpnXAYEMcAwmnVb5b7o8/PylGnILxXMHaUlRrPmRI0=
|
|
||||||
go.unistack.org/micro/v3 v3.11.37/go.mod h1:POGU5hstnAT9LH70m8FalyQSNi2GfIew71K75JenIZk=
|
|
||||||
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
|
||||||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
|
||||||
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
|
||||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|
||||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
|
||||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 h1:Z7FRVJPSMaHQxD0uXU8WdgFh8PseLM8Q8NzhnpMrBhQ=
|
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484/go.mod h1:lcTa1sDdWEIHMWlITnIczmw5w60CF9ffkb8Z+DVmmjA=
|
|
||||||
google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
|
|
||||||
google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
|
|
||||||
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
|
|
||||||
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
|
108
requestid.go
108
requestid.go
@ -1,17 +1,31 @@
|
|||||||
package requestid // import "go.unistack.org/micro-wrapper-requestid/v3"
|
package requestid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"go.unistack.org/micro/v3/client"
|
|
||||||
"go.unistack.org/micro/v3/metadata"
|
|
||||||
"go.unistack.org/micro/v3/server"
|
|
||||||
"go.unistack.org/micro/v3/util/id"
|
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"go.unistack.org/micro/v4/client"
|
||||||
|
"go.unistack.org/micro/v4/logger"
|
||||||
|
"go.unistack.org/micro/v4/metadata"
|
||||||
|
"go.unistack.org/micro/v4/options"
|
||||||
|
"go.unistack.org/micro/v4/server"
|
||||||
|
"go.unistack.org/micro/v4/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{}
|
type XRequestIDKey struct{}
|
||||||
|
|
||||||
// DefaultMetadataKey contains metadata key
|
// 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
|
||||||
@ -68,71 +82,55 @@ var DefaultMetadataFunc = func(ctx context.Context) (context.Context, error) {
|
|||||||
return ctx, nil
|
return ctx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type hook struct{}
|
type wrapper struct {
|
||||||
|
client.Client
|
||||||
func NewHook() *hook {
|
|
||||||
return &hook{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *hook) ServerSubscriber(next server.FuncSubHandler) server.FuncSubHandler {
|
func NewClientWrapper() client.Wrapper {
|
||||||
return func(ctx context.Context, msg server.Message) error {
|
return func(c client.Client) client.Client {
|
||||||
var err error
|
handler := &wrapper{
|
||||||
if xid, ok := msg.Header()[DefaultMetadataKey]; ok {
|
Client: c,
|
||||||
ctx = context.WithValue(ctx, XRequestIDKey{}, xid)
|
|
||||||
}
|
}
|
||||||
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
return handler
|
||||||
return err
|
|
||||||
}
|
|
||||||
return next(ctx, msg)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler {
|
func NewClientCallWrapper() client.CallWrapper {
|
||||||
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
return func(fn client.CallFunc) client.CallFunc {
|
||||||
var err error
|
return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
||||||
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
var err error
|
||||||
return err
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return fn(ctx, addr, req, rsp, opts)
|
||||||
}
|
}
|
||||||
return next(ctx, req, rsp)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *hook) ClientBatchPublish(next client.FuncBatchPublish) client.FuncBatchPublish {
|
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...options.Option) error {
|
||||||
return func(ctx context.Context, msgs []client.Message, opts ...client.PublishOption) 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
|
|
||||||
}
|
|
||||||
return next(ctx, msgs, opts...)
|
|
||||||
}
|
}
|
||||||
|
return w.Client.Call(ctx, req, rsp, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *hook) ClientPublish(next client.FuncPublish) client.FuncPublish {
|
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...options.Option) (client.Stream, error) {
|
||||||
return func(ctx context.Context, msg client.Message, opts ...client.PublishOption) 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 err
|
|
||||||
}
|
|
||||||
return next(ctx, msg, opts...)
|
|
||||||
}
|
}
|
||||||
|
return w.Client.Stream(ctx, req, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *hook) ClientCall(next client.FuncCall) client.FuncCall {
|
func NewServerHandlerWrapper() server.HandlerWrapper {
|
||||||
return func(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
return func(fn server.HandlerFunc) server.HandlerFunc {
|
||||||
var err error
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
||||||
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
var err error
|
||||||
return err
|
if ctx, err = DefaultMetadataFunc(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return fn(ctx, req, rsp)
|
||||||
}
|
}
|
||||||
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...)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user