diff --git a/glog/gorm/gorm.go b/glog/gorm/gorm.go new file mode 100644 index 0000000..cadaa11 --- /dev/null +++ b/glog/gorm/gorm.go @@ -0,0 +1,56 @@ +package gl + +import ( + "context" + "github.com/go-kratos/kratos/v2/log" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + semconv "go.opentelemetry.io/otel/semconv/v1.4.0" + "go.opentelemetry.io/otel/trace" + "gorm.io/gorm/logger" + "strings" + "time" +) + +type Logger struct { + *log.Helper +} + +func (l *Logger) LogMode(level logger.LogLevel) logger.Interface { + return l +} + +func (l *Logger) Info(ctx context.Context, s string, i ...interface{}) { + l.WithContext(ctx).Info(s, i) +} + +func (l *Logger) Warn(ctx context.Context, s string, i ...interface{}) { + l.WithContext(ctx).Warn(s, i) +} + +func (l *Logger) Error(ctx context.Context, s string, i ...interface{}) { + l.WithContext(ctx).Error(s, i) +} + +func (l *Logger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) { + sql, rowsAffected := fc() + MysqlTracer(ctx, begin, sql, int(rowsAffected)) + l.WithContext(ctx).Infof("[%s]db trace sql:%s, rowsAffected:%d, err:%v", begin, sql, rowsAffected, err) +} + +func MysqlTracer(ctx context.Context, begin time.Time, sql string, rowsAffected int) { + tracer := otel.Tracer("Mysql") + kind := trace.SpanKindInternal + strs := strings.Split(sql, " ") + ctx, span := tracer.Start(ctx, + strs[0], + trace.WithSpanKind(kind), + trace.WithTimestamp(begin), + ) + var attrs []attribute.KeyValue + attrs = append(attrs, semconv.DBSystemMySQL) + attrs = append(attrs, semconv.DBStatementKey.String(sql)) + attrs = append(attrs, attribute.Int("db.rows_affected", rowsAffected)) + span.SetAttributes(attrs...) + span.End(trace.WithTimestamp(time.Now())) +} diff --git a/go.mod b/go.mod index 6a1fb04..1dcf23b 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( go.opentelemetry.io/otel/trace v1.14.0 google.golang.org/grpc v1.53.0 google.golang.org/protobuf v1.28.1 + gorm.io/gorm v1.25.2 k8s.io/api v0.27.3 k8s.io/apimachinery v0.27.3 k8s.io/client-go v0.27.3 diff --git a/go.sum b/go.sum index b15c50d..0359bba 100644 --- a/go.sum +++ b/go.sum @@ -836,6 +836,9 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= @@ -1786,6 +1789,8 @@ gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho= +gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=