otto 12 months ago
parent
commit
5fb7bd9810
  1. 4
      context/context.go
  2. 8
      logger.go
  3. 3
      request/req_id.go

4
context/context.go

@ -1,14 +1,12 @@
package context
import (
"fmt"
"github.com/go-kratos/kratos/v2/metadata"
"golang.org/x/net/context"
)
func SetContext(ctx context.Context, key, value string) context.Context {
ctx = context.WithValue(ctx, key, value)
fmt.Println(key, " set:", value)
return metadata.AppendToClientContext(ctx, key, value)
}
@ -16,12 +14,10 @@ func GetContext(ctx context.Context, key string) string {
if ctx == nil {
return ""
}
//fmt.Println(key, " value:", ctx.Value(key))
if ctx.Value(key) != nil {
return ctx.Value(key).(string)
}
if md, ok := metadata.FromServerContext(ctx); ok {
fmt.Println(key, " metadata:", md.Get(key))
return md.Get(key)
}
return ""

8
logger.go

@ -3,12 +3,12 @@ package log
import (
"context"
"encoding/json"
"fmt"
"git.gz.internal.jumaiyx.cn/jm/jmproto/conf"
klog "git.gz.internal.jumaiyx.cn/pkg/log/klog"
"git.gz.internal.jumaiyx.cn/pkg/log/request"
"git.gz.internal.jumaiyx.cn/pkg/log/tracing"
"github.com/go-kratos/kratos/v2/middleware"
"log"
)
const (
@ -78,12 +78,12 @@ func (l *logger) Server() middleware.Middleware {
reqId string
spanId int
)
fmt.Println("===中间件==")
// 获取传递的id
ctx, reqId = request.GetReqId(ctx)
log.Printf("===== server reqId:%s=====:\n", reqId)
fmt.Printf("===== server reqId:%s=====:\n", reqId)
ctx, spanId = tracing.GetSpanId(ctx)
log.Printf("===== server spanId:%d=====:\n", spanId)
fmt.Printf("===== server spanId:%d=====:\n", spanId)
// 打印参数
l.WithContext(ctx)
l.Request(req)

3
request/req_id.go

@ -3,7 +3,6 @@ package request
import (
"context"
"encoding/hex"
"fmt"
lcontext "git.gz.internal.jumaiyx.cn/pkg/log/context"
"math/rand"
"time"
@ -35,9 +34,7 @@ func genReqIdMd5() string {
func GetReqId(ctx context.Context) (context.Context, string) {
reqId := lcontext.GetContext(ctx, ContextReqId)
//fmt.Println("获取:", reqId)
if reqId == "" {
fmt.Println("ctx:", ctx)
if ctx == nil {
ctx = context.TODO()
}

Loading…
Cancel
Save