Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
4b926bb1bd | |||
a424fd9722 | |||
a3f79a7bed | |||
72c01fe7c9 |
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@@ -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
.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
|
||||||
|
2
.github/workflows/pr.yml
vendored
2
.github/workflows/pr.yml
vendored
@@ -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
|
||||||
|
4
go.mod
4
go.mod
@@ -2,6 +2,4 @@ module go.unistack.org/micro-wrapper-requestid/v4
|
|||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require go.unistack.org/micro/v4 v4.0.14
|
require go.unistack.org/micro/v4 v4.0.17
|
||||||
|
|
||||||
require golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
|
|
||||||
|
6
go.sum
6
go.sum
@@ -1,4 +1,2 @@
|
|||||||
go.unistack.org/micro/v4 v4.0.14 h1:zN+aVfTceei3TPzuA7f2wAXLYHoSHWpx2XgW8yqaTIA=
|
go.unistack.org/micro/v4 v4.0.17 h1:mF7uM+J4ILdG+1fcwzKYCwDlxhdbF/e1WnGzKKLnIXc=
|
||||||
go.unistack.org/micro/v4 v4.0.14/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
go.unistack.org/micro/v4 v4.0.17/go.mod h1:ZDgU9931vm2l7X6RN/6UuwRIVp24GRdmQ7dKmegArk4=
|
||||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
|
||||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
|
||||||
|
52
requestid.go
52
requestid.go
@@ -3,6 +3,7 @@ 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/logger"
|
||||||
@@ -13,9 +14,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
requestIDLog := strings.ToLower(DefaultMetadataKey)
|
||||||
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, func(ctx context.Context) []interface{} {
|
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, func(ctx context.Context) []interface{} {
|
||||||
if v, ok := ctx.Value(XRequestIDKey{}).(string); ok {
|
if v, ok := ctx.Value(XRequestIDKey{}).(string); ok {
|
||||||
return []interface{}{DefaultMetadataKey, v}
|
return []interface{}{requestIDLog, v}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@@ -29,31 +31,51 @@ 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) {
|
||||||
var xid string
|
var xid string
|
||||||
var err error
|
|
||||||
|
|
||||||
if _, ok := ctx.Value(XRequestIDKey{}).(string); !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)
|
||||||
|
ctx = metadata.NewIncomingContext(ctx, imd)
|
||||||
|
}
|
||||||
|
|
||||||
|
omd, ook := metadata.FromOutgoingContext(ctx)
|
||||||
|
if !ook || omd == nil {
|
||||||
|
omd = metadata.New(1)
|
||||||
|
ctx = metadata.NewOutgoingContext(ctx, omd)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 xid == "" {
|
||||||
|
var err error
|
||||||
xid, err = id.New()
|
xid, err = id.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctx, err
|
return ctx, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !cok {
|
||||||
ctx = context.WithValue(ctx, XRequestIDKey{}, xid)
|
ctx = context.WithValue(ctx, XRequestIDKey{}, xid)
|
||||||
}
|
}
|
||||||
|
|
||||||
imd, ok := metadata.FromIncomingContext(ctx)
|
if !iok {
|
||||||
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)
|
imd.Set(DefaultMetadataKey, xid)
|
||||||
}
|
}
|
||||||
|
|
||||||
omd, ok := metadata.FromOutgoingContext(ctx)
|
if !ook {
|
||||||
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)
|
omd.Set(DefaultMetadataKey, xid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user