build(deps): 更新依赖并调整日志记录方式

- 更新 go-sql-driver/mysql 从 v1.9.2 到 v1.9.3
- 替换 github.com/uptrace/opentelemetry-go-extra/otelzap 为 go.opentelemetry.io/contrib/bridges/otelzap
- 更新多个 OpenTelemetry 相关依赖- 移除 github.com/uptrace/opentelemetry-go-extra/otelutil
- 调整 CustomLogger 结构和方法,以适应新的日志记录方式
- 优化日志记录流程,提高性能和可读性
This commit is contained in:
danial
2025-06-20 19:26:39 +08:00
parent b8287adff6
commit 5de4ccece4
6 changed files with 56 additions and 36 deletions

5
go.mod
View File

@@ -14,7 +14,7 @@ require (
github.com/duke-git/lancet/v2 v2.3.6
github.com/forgoer/openssl v1.6.0
github.com/go-resty/resty/v2 v2.16.5
github.com/go-sql-driver/mysql v1.9.2
github.com/go-sql-driver/mysql v1.9.3
github.com/go-stomp/stomp/v3 v3.1.3
github.com/google/uuid v1.6.0
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
@@ -24,8 +24,8 @@ require (
github.com/rs/xid v1.6.0
github.com/shopspring/decimal v1.4.0
github.com/stretchr/testify v1.10.0
github.com/uptrace/opentelemetry-go-extra/otelzap v0.3.2
github.com/widuu/gojson v0.0.0-20170212122013-7da9d2cd949b
go.opentelemetry.io/contrib/bridges/otelzap v0.11.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0
go.opentelemetry.io/otel v1.36.0
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.12.2
@@ -68,7 +68,6 @@ require (
github.com/prometheus/procfs v0.16.1 // indirect
github.com/shiena/ansicolor v0.0.0-20230509054315-a9deabde6e02 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/uptrace/opentelemetry-go-extra/otelutil v0.3.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel/metric v1.36.0 // indirect

10
go.sum
View File

@@ -53,6 +53,8 @@ github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptd
github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
github.com/go-sql-driver/mysql v1.9.2 h1:4cNKDYQ1I84SXslGddlsrMhc8k4LeDVj6Ad6WRjiHuU=
github.com/go-sql-driver/mysql v1.9.2/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo=
github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
github.com/go-stomp/stomp/v3 v3.1.3 h1:5/wi+bI38O1Qkf2cc7Gjlw7N5beHMWB/BxpX+4p/MGI=
github.com/go-stomp/stomp/v3 v3.1.3/go.mod h1:ztzZej6T2W4Y6FlD+Tb5n7HQP3/O5UNQiuC169pIp10=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
@@ -128,10 +130,6 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/uptrace/opentelemetry-go-extra/otelutil v0.3.2 h1:3/aHKUq7qaFMWxyQV0W2ryNgg8x8rVeKVA20KJUkfS0=
github.com/uptrace/opentelemetry-go-extra/otelutil v0.3.2/go.mod h1:Zit4b8AQXaXvA68+nzmbyDzqiyFRISyw1JiD5JqUBjw=
github.com/uptrace/opentelemetry-go-extra/otelzap v0.3.2 h1:cj/Z6FKTTYBnstI0Lni9PA+k2foounKIPUmj1LBwNiQ=
github.com/uptrace/opentelemetry-go-extra/otelzap v0.3.2/go.mod h1:LDaXk90gKEC2nC7JH3Lpnhfu+2V7o/TsqomJJmqA39o=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/widuu/gojson v0.0.0-20170212122013-7da9d2cd949b h1:ieRJ8K7QAPWWltEOv7rzMruuPd7gbeAqTaBFhUECIy0=
@@ -140,6 +138,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
go.opentelemetry.io/contrib/bridges/otelzap v0.11.0 h1:u2E32P7j1a/gRgZDWhIXC+Shd4rLg70mnE7QLI/Ssnw=
go.opentelemetry.io/contrib/bridges/otelzap v0.11.0/go.mod h1:pJPCLM8gzX4ASqLlyAXjHBEYxgbOQJ/9bidWxD6PEPQ=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0 h1:F7Jx+6hwnZ41NSFTO5q4LYDtJRXBf2PD0rNBkeB/lus=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0/go.mod h1:UHB22Z8QsdRDrnAtX4PntOl36ajSxcdUMt1sF7Y6E7Q=
go.opentelemetry.io/otel v1.36.0 h1:UumtzIklRBY6cI/lllNZlALOF5nNIzJVb16APdvgTXg=
@@ -154,6 +154,8 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.36.0 h1:nRVXX
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.36.0/go.mod h1:r49hO7CgrxY9Voaj3Xe8pANWtr0Oq916d0XAmOoCZAQ=
go.opentelemetry.io/otel/log v0.12.2 h1:yob9JVHn2ZY24byZeaXpTVoPS6l+UrrxmxmPKohXTwc=
go.opentelemetry.io/otel/log v0.12.2/go.mod h1:ShIItIxSYxufUMt+1H5a2wbckGli3/iCfuEbVZi/98E=
go.opentelemetry.io/otel/log/logtest v0.0.0-20250521073539-a85ae98dcedc h1:TU7eU/nib68C+4ZMQ5t4em5Jhf50kRorSCV4w+v65vo=
go.opentelemetry.io/otel/log/logtest v0.0.0-20250521073539-a85ae98dcedc/go.mod h1:4AsFc5k1BDLWm5jt0yagrodTEA9xS9McwcnYm+Jf73A=
go.opentelemetry.io/otel/metric v1.36.0 h1:MoWPKVhQvJ+eeXWHFBOPoBOi20jh6Iq2CcCREuTYufE=
go.opentelemetry.io/otel/metric v1.36.0/go.mod h1:zC7Ks+yeyJt4xig9DEw9kuUFe5C3zLbVjV2PzT6qzbs=
go.opentelemetry.io/otel/sdk v1.36.0 h1:b6SYIuLRs88ztox4EyrvRti80uXIFy+Sqzoh9kFULbs=

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"os"
"github.com/uptrace/opentelemetry-go-extra/otelzap"
"go.opentelemetry.io/contrib/bridges/otelzap"
"github.com/beego/beego/v2/core/config/env"
"github.com/natefinch/lumberjack"
@@ -39,11 +39,26 @@ var (
)
type CustomLogger struct {
*otelzap.Logger
logger *zap.Logger
}
func (l *CustomLogger) WithContext(ctx context.Context) otelzap.LoggerWithCtx {
return l.Ctx(ctx)
func (l *CustomLogger) WithContext(ctx context.Context) *zap.Logger {
// 如果上下文为空,直接返回日志记录器,不进行任何修改。
if ctx == nil {
return l.logger
}
span := trace.SpanFromContext(ctx)
if !span.SpanContext().IsValid() {
if l.logger == nil {
return zap.NewNop()
}
return l.logger
}
return l.logger.With(
zap.Reflect("ctx", ctx),
zap.String("trace_id", span.SpanContext().TraceID().String()),
zap.String("span_id", span.SpanContext().SpanID().String()),
)
}
var (
@@ -158,6 +173,7 @@ func InitTracer() (func(context.Context) error, func(context.Context) error, fun
// 日志需要保存在本地,并且每日更新
core := zapcore.NewTee(
otelzap.NewCore(serviceName, otelzap.WithLoggerProvider(loggerProvider)),
zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderConfig),
zapcore.NewMultiWriteSyncer(
@@ -167,15 +183,11 @@ func InitTracer() (func(context.Context) error, func(context.Context) error, fun
zap.InfoLevel,
),
)
// zap设置标准输出流
// 设置全局 logger
Logger = CustomLogger{
Logger: otelzap.New(zap.New(core,
zap.AddCaller(),
zap.AddStacktrace(zap.ErrorLevel),
), otelzap.WithLoggerProvider(loggerProvider), otelzap.WithStackTrace(true)),
}
Logger = CustomLogger{logger: zap.New(core,
zap.AddCaller(),
zap.AddStacktrace(zap.ErrorLevel),
)}
// 确保设置 TextMapPropagator
otel.SetTextMapPropagator(
@@ -219,7 +231,7 @@ func Middleware(ctx *beecontext.Context, next web.FilterFunc) {
if err := recover(); err != nil {
span.RecordError(err.(error))
span.SetAttributes(attribute.String("error", "true"))
Logger.Ctx(ctx.Request.Context()).Error("全局错误", zap.Any("error", err))
Logger.WithContext(ctx.Request.Context()).Error("全局错误", zap.Any("error", err))
// 结束 span
span.End()
// 重新抛出异常
@@ -296,7 +308,14 @@ func init() {
zap.InfoLevel,
),
)
logger := zap.New(core,
zap.AddCaller(),
// zap.AddCallerSkip(1),
zap.AddStacktrace(zap.ErrorLevel),
)
// zap设置标准输出流
// 设置全局 logger
Logger = CustomLogger{
Logger: otelzap.New(zap.New(core)),
logger: logger,
}
}

View File

@@ -80,11 +80,11 @@ type HeepayProduct struct {
func (c *HeepayImpl) SendCard(ctx context.Context, jsonStr string, cardInfo supplier.RedeemCardInfo, attach string, merchantId string, orderInfo order.OrderInfo, roadInfo road.RoadInfo) (bool, string, string) {
orderAmount, err := (&cardTypeQuery{
QueryType: gojson.Json(roadInfo.Params).Get("queryType").Tostring(),
CardNo: cardInfo.CardNo,
ChannelCode: gojson.Json(roadInfo.Params).Get("channelCode").Tostring(),
CardPwd: cardInfo.Data,
Balance: cardInfo.GetFaceTypeFloat(ctx),
QueryType: gojson.Json(roadInfo.Params).Get("queryType").Tostring(),
CardNo: cardInfo.CardNo,
ChannelCode: gojson.Json(roadInfo.Params).Get("channelCode").Tostring(),
CardPwd: cardInfo.Data,
Balance: cardInfo.GetFaceTypeFloat(ctx),
}).GetBalance(ctx)
if err != nil {
return false, err.Error(), err.Error()

View File

@@ -40,11 +40,11 @@ func (c *LubanImpl) HasDependencyHTML() bool {
func (c *LubanImpl) SendCard(ctx context.Context, jsonStr string, cardInfo supplier.RedeemCardInfo, attach string, merchantInfo merchant.MerchantInfo, roadInfo road.RoadInfo) (bool, string) {
otelTrace.Logger.WithContext(ctx).Info("鲁班发送卡", zap.Any("cardNo", cardInfo), zap.Any("orderNo", attach))
orderAmount, err := (&cardTypeQuery{
QueryType: gojson.Json(roadInfo.Params).Get("queryType").Tostring(),
CardNo: cardInfo.CardNo,
ChannelCode: gojson.Json(roadInfo.Params).Get("channelCode").Tostring(),
CardPwd: cardInfo.Data,
Balance: cardInfo.GetFaceTypeFloat(ctx),
QueryType: gojson.Json(roadInfo.Params).Get("queryType").Tostring(),
CardNo: cardInfo.CardNo,
ChannelCode: gojson.Json(roadInfo.Params).Get("channelCode").Tostring(),
CardPwd: cardInfo.Data,
Balance: cardInfo.GetFaceTypeFloat(ctx),
}).GetBalance(ctx)
if err != nil {
return false, err.Error()

View File

@@ -42,11 +42,11 @@ func (c *MyselfCardImpl) HasDependencyHTML() bool {
func (c *MyselfCardImpl) SendCard(ctx context.Context, jsonStr string, cardInfo supplier.RedeemCardInfo, orderInfo *order.OrderInfo, merchantInfo merchant.MerchantInfo, roadInfo road.RoadInfo) (bool, string) {
orderAmount, err := (&cardTypeQuery{
QueryType: gojson.Json(roadInfo.Params).Get("queryType").Tostring(),
CardNo: cardInfo.CardNo,
ChannelCode: gojson.Json(roadInfo.Params).Get("channelCode").Tostring(),
CardPwd: cardInfo.Data,
Balance: cardInfo.GetFaceTypeFloat(ctx),
QueryType: gojson.Json(roadInfo.Params).Get("queryType").Tostring(),
CardNo: cardInfo.CardNo,
ChannelCode: gojson.Json(roadInfo.Params).Get("channelCode").Tostring(),
CardPwd: cardInfo.Data,
Balance: cardInfo.GetFaceTypeFloat(ctx),
}).GetBalance(ctx)
if err != nil {
return false, err.Error()