Skip to content

Commit

Permalink
Update init id
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed May 28, 2020
1 parent 9181d19 commit 3fa39de
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
16 changes: 2 additions & 14 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"github.com/LyricTian/gin-admin/v6/internal/app/injector"
"github.com/LyricTian/gin-admin/v6/internal/app/iutil"
"github.com/LyricTian/gin-admin/v6/pkg/logger"
"github.com/LyricTian/gin-admin/v6/pkg/trace"
"github.com/LyricTian/gin-admin/v6/pkg/unique"
"github.com/go-redis/redis"
"github.com/google/gops/agent"

Expand Down Expand Up @@ -93,15 +91,8 @@ func Init(ctx context.Context, opts ...Option) (func(), error) {

logger.Printf(ctx, "服务启动,运行模式:%s,版本号:%s,进程号:%d", config.C.RunMode, o.Version, os.Getpid())

// 初始化 snowflake node
err := unique.SetSnowflakeNode(config.C.UniqueID.Snowflake.Node, config.C.UniqueID.Snowflake.Epoch)
if err != nil {
return nil, err
}

trace.SetIDFunc(func() string {
return unique.NewSnowflakeID().String()
})
// Initialize unique id
iutil.InitID()

// 初始化日志模块
loggerCleanFunc, err := InitLogger()
Expand All @@ -115,9 +106,6 @@ func Init(ctx context.Context, opts ...Option) (func(), error) {
// 初始化图形验证码
InitCaptcha()

// Initialize unique id
iutil.InitID()

// 初始化依赖注入器
injector, injectorCleanFunc, err := injector.BuildInjector()
if err != nil {
Expand Down
14 changes: 11 additions & 3 deletions internal/app/ginplus/ginplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ func SetUserID(c *gin.Context, userID string) {
c.Set(UserIDKey, userID)
}

// GetBody Get request body
func GetBody(c *gin.Context) []byte {
if v, ok := c.Get(ReqBodyKey); ok {
if b, ok := v.([]byte); ok {
return b
}
}
return nil
}

// ParseJSON 解析请求JSON
func ParseJSON(c *gin.Context, obj interface{}) error {
if err := c.ShouldBindJSON(obj); err != nil {
Expand Down Expand Up @@ -124,9 +134,7 @@ func ResError(c *gin.Context, err error, status ...int) {
if status := res.StatusCode; status >= 400 && status < 500 {
logger.StartSpan(ctx).Warnf(err.Error())
} else if status >= 500 {
span := logger.StartSpan(ctx)
span = span.WithField("stack", fmt.Sprintf("%+v", err))
span.Errorf(err.Error())
logger.ErrorStack(ctx, err)
}
}

Expand Down
16 changes: 16 additions & 0 deletions internal/app/iutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package iutil

import (
"github.com/LyricTian/gin-admin/v6/internal/app/config"
"github.com/LyricTian/gin-admin/v6/pkg/logger"
"github.com/LyricTian/gin-admin/v6/pkg/trace"
"github.com/LyricTian/gin-admin/v6/pkg/unique"
)

Expand All @@ -21,6 +23,20 @@ func InitID() {
return unique.NewObjectID().Hex()
}
default:
// Initialize snowflake node
err := unique.SetSnowflakeNode(config.C.UniqueID.Snowflake.Node, config.C.UniqueID.Snowflake.Epoch)
if err != nil {
panic(err)
}

logger.SetTraceIDFunc(func() string {
return unique.NewSnowflakeID().String()
})

trace.SetIDFunc(func() string {
return unique.NewSnowflakeID().String()
})

idFunc = func() string {
return unique.NewSnowflakeID().String()
}
Expand Down
1 change: 0 additions & 1 deletion internal/app/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func InitLogger() (func(), error) {

var hook *loggerhook.Hook
if c.EnableHook {

var hookLevels []logrus.Level
for _, lvl := range c.HookLevels {
plvl, err := logrus.ParseLevel(lvl)
Expand Down
18 changes: 12 additions & 6 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
SpanTitleKey = "span_title"
SpanFunctionKey = "span_function"
VersionKey = "version"
StackKey = "stack"
)

// TraceIDFunc 定义获取跟踪ID的函数
Expand Down Expand Up @@ -83,18 +84,18 @@ func AddHook(hook Hook) {
}

type (
traceIDContextKey struct{}
userIDContextKey struct{}
traceIDKey struct{}
userIDKey struct{}
)

// NewTraceIDContext 创建跟踪ID上下文
func NewTraceIDContext(ctx context.Context, traceID string) context.Context {
return context.WithValue(ctx, traceIDContextKey{}, traceID)
return context.WithValue(ctx, traceIDKey{}, traceID)
}

// FromTraceIDContext 从上下文中获取跟踪ID
func FromTraceIDContext(ctx context.Context) string {
v := ctx.Value(traceIDContextKey{})
v := ctx.Value(traceIDKey{})
if v != nil {
if s, ok := v.(string); ok {
return s
Expand All @@ -105,12 +106,12 @@ func FromTraceIDContext(ctx context.Context) string {

// NewUserIDContext 创建用户ID上下文
func NewUserIDContext(ctx context.Context, userID string) context.Context {
return context.WithValue(ctx, userIDContextKey{}, userID)
return context.WithValue(ctx, userIDKey{}, userID)
}

// FromUserIDContext 从上下文中获取用户ID
func FromUserIDContext(ctx context.Context) string {
v := ctx.Value(userIDContextKey{})
v := ctx.Value(userIDKey{})
if v != nil {
if s, ok := v.(string); ok {
return s
Expand Down Expand Up @@ -201,6 +202,11 @@ func Fatalf(ctx context.Context, format string, args ...interface{}) {
StartSpan(ctx).Fatalf(format, args...)
}

// ErrorStack 输出错误栈
func ErrorStack(ctx context.Context, err error) {
StartSpan(ctx).WithField(StackKey, fmt.Sprintf("%+v", err)).Errorf(err.Error())
}

func newEntry(entry *logrus.Entry) *Entry {
return &Entry{entry: entry}
}
Expand Down

0 comments on commit 3fa39de

Please sign in to comment.