Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
a3f79a7bed | |||
72c01fe7c9 | |||
bd6ebc31d6 | |||
9c5c08c188 |
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
|
||||||
|
2
go.mod
2
go.mod
@@ -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.7
|
require go.unistack.org/micro/v4 v4.0.17
|
||||||
|
6
go.sum
6
go.sum
@@ -1,4 +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=
|
||||||
go.unistack.org/micro/v4 v4.0.7 h1:2lwtZlHcSwgkahhFbkI4x1lOS79lw8uLHtcEhlFF+AM=
|
|
||||||
go.unistack.org/micro/v4 v4.0.7/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
|
|
||||||
|
32
requestid.go
32
requestid.go
@@ -1,17 +1,27 @@
|
|||||||
package requestid // import "go.unistack.org/micro-wrapper-requestid/v4"
|
package requestid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
|
|
||||||
"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/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() {
|
||||||
|
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, func(ctx context.Context) []interface{} {
|
||||||
|
if v, ok := ctx.Value(XRequestIDKey{}).(string); ok {
|
||||||
|
return []interface{}{DefaultMetadataKey, 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")
|
||||||
@@ -20,41 +30,31 @@ var DefaultMetadataKey = textproto.CanonicalMIMEHeaderKey("x-request-id")
|
|||||||
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
|
var err error
|
||||||
var ook, iok bool
|
|
||||||
|
|
||||||
if _, ok := ctx.Value(XRequestIDKey).(string); !ok {
|
if _, ok := ctx.Value(XRequestIDKey{}).(string); !ok {
|
||||||
xid, err = id.New()
|
xid, err = id.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctx, err
|
return ctx, err
|
||||||
}
|
}
|
||||||
ctx = context.WithValue(ctx, XRequestIDKey, xid)
|
ctx = context.WithValue(ctx, XRequestIDKey{}, xid)
|
||||||
}
|
}
|
||||||
|
|
||||||
imd, ok := metadata.FromIncomingContext(ctx)
|
imd, ok := metadata.FromIncomingContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
imd = metadata.New(1)
|
imd = metadata.New(1)
|
||||||
imd.Set(DefaultMetadataKey, xid)
|
imd.Set(DefaultMetadataKey, xid)
|
||||||
|
ctx = metadata.NewIncomingContext(ctx, imd)
|
||||||
} else if _, ok = imd.Get(DefaultMetadataKey); !ok {
|
} else if _, ok = imd.Get(DefaultMetadataKey); !ok {
|
||||||
imd.Set(DefaultMetadataKey, xid)
|
imd.Set(DefaultMetadataKey, xid)
|
||||||
} else {
|
|
||||||
iok = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
omd, ok := metadata.FromOutgoingContext(ctx)
|
omd, ok := metadata.FromOutgoingContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
omd = metadata.New(1)
|
omd = metadata.New(1)
|
||||||
omd.Set(DefaultMetadataKey, xid)
|
omd.Set(DefaultMetadataKey, xid)
|
||||||
|
ctx = metadata.NewOutgoingContext(ctx, imd)
|
||||||
} else if _, ok = omd.Get(DefaultMetadataKey); !ok {
|
} else if _, ok = omd.Get(DefaultMetadataKey); !ok {
|
||||||
omd.Set(DefaultMetadataKey, xid)
|
omd.Set(DefaultMetadataKey, xid)
|
||||||
} else {
|
|
||||||
ook = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if !iok {
|
|
||||||
ctx = metadata.NewIncomingContext(ctx, imd)
|
|
||||||
}
|
|
||||||
if !ook {
|
|
||||||
ctx = metadata.NewOutgoingContext(ctx, omd)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
|
@@ -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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user