7 Commits

Author SHA1 Message Date
4b926bb1bd fixup metadata
Some checks failed
build / test (push) Failing after 1m14s
build / lint (push) Successful in 9m18s
codeql / analyze (go) (push) Failing after 1m52s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-12 01:58:03 +03:00
a424fd9722 fixup logger field and metadata handling
Some checks failed
build / test (push) Failing after 56s
codeql / analyze (go) (push) Failing after 1m50s
build / lint (push) Successful in 9m19s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-11 23:52:29 +03:00
a3f79a7bed update go.mod
Some checks failed
build / test (push) Failing after 14m39s
codeql / analyze (go) (push) Failing after 3m16s
build / lint (push) Successful in 9m13s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-10 22:36:58 +03:00
72c01fe7c9 optimize
Some checks failed
build / test (push) Failing after 59s
codeql / analyze (go) (push) Failing after 1m41s
build / lint (push) Has been cancelled
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-10 22:28:34 +03:00
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
7 changed files with 71 additions and 65 deletions

View File

@@ -12,7 +12,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
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: cache - name: cache

View File

@@ -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

View File

@@ -12,7 +12,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
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: cache - name: cache

2
go.mod
View File

@@ -2,4 +2,4 @@ module go.unistack.org/micro-wrapper-requestid/v4
go 1.20 go 1.20
require go.unistack.org/micro/v4 v4.0.1 require go.unistack.org/micro/v4 v4.0.17

4
go.sum
View File

@@ -1,2 +1,2 @@
go.unistack.org/micro/v4 v4.0.1 h1:xo1IxbVfgh8i0eY0VeYa3cbb13u5n/Mxnp3FOgWD4Jo= go.unistack.org/micro/v4 v4.0.17 h1:mF7uM+J4ILdG+1fcwzKYCwDlxhdbF/e1WnGzKKLnIXc=
go.unistack.org/micro/v4 v4.0.1/go.mod h1:p/J5UcSJjfHsWGT31uKoghQ5rUQZzQJBAFy+Z4+ZVMs= go.unistack.org/micro/v4 v4.0.17/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=

View File

@@ -1,48 +1,84 @@
package requestid // import "go.unistack.org/micro-wrapper-requestid/v4" package requestid
import ( import (
"context" "context"
"net/textproto" "net/textproto"
"strings"
"go.unistack.org/micro/v4/client" "go.unistack.org/micro/v4/client"
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/metadata" "go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/server" "go.unistack.org/micro/v4/server"
"go.unistack.org/micro/v4/util/id" "go.unistack.org/micro/v4/util/id"
) )
var XRequestIDKey struct{} 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 x-request-id // 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 {
cid, cok := ctx.Value(XRequestIDKey{}).(string)
if cok && cid != "" {
xid = cid
}
imd, iok := metadata.FromIncomingContext(ctx)
if !iok || imd == nil {
imd = metadata.New(1) 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) omd = metadata.New(1)
ctx = metadata.NewOutgoingContext(ctx, omd)
} }
v, iok := imd.Get(DefaultMetadataKey)
if iok { if xid == "" {
if _, ook := omd.Get(DefaultMetadataKey); ook { var id string
return ctx, nil 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 { if err != nil {
return ctx, err return ctx, err
} }
v = uid
} }
imd.Set(DefaultMetadataKey, v)
omd.Set(DefaultMetadataKey, v) if !cok {
ctx = context.WithValue(ctx, XRequestIDKey, v) ctx = context.WithValue(ctx, XRequestIDKey{}, xid)
ctx = metadata.NewIncomingContext(ctx, imd) }
ctx = metadata.NewOutgoingContext(ctx, omd)
if !iok {
imd.Set(DefaultMetadataKey, xid)
}
if !ook {
omd.Set(DefaultMetadataKey, xid)
}
return ctx, nil return ctx, nil
} }
@@ -71,7 +107,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
@@ -79,7 +115,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
@@ -87,14 +123,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 {
@@ -106,29 +134,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 = context.WithValue(ctx, XRequestIDKey, 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

@@ -9,25 +9,29 @@ import (
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")
}
} }