Skip to content

Commit

Permalink
Trace all SQL statements and output with log
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytaxii committed May 21, 2024
1 parent 4cdc101 commit 0944b53
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
16 changes: 13 additions & 3 deletions api/server/middleware/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/gin-contrib/requestid"
"github.com/gin-gonic/gin"
klog "github.com/sirupsen/logrus"

"github.com/caoyingjunz/pixiu/pkg/db"
)

const (
Expand All @@ -31,7 +33,14 @@ const (
FailMsg = "FAIL"
)

func LoggerToFile() gin.HandlerFunc {
func DBLogger() gin.HandlerFunc {
return func(c *gin.Context) {
c.Set(db.SQLContextKey, new(db.SQLs))
c.Next()
}
}

func Logger() gin.HandlerFunc {
return func(c *gin.Context) {
startTime := time.Now()

Expand All @@ -45,8 +54,9 @@ func LoggerToFile() gin.HandlerFunc {
"status_code": c.Writer.Status(),
"latency": fmt.Sprintf("%dµs", time.Since(startTime).Microseconds()),
"client_ip": c.ClientIP(),
// TODO
// "sqls": []string{},
}
if sqls := db.GetSQLs(c); len(sqls) > 0 {
fields["sqls"] = sqls
}

if errs := c.Errors; len(errs) > 0 {
Expand Down
3 changes: 2 additions & 1 deletion api/server/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ func InstallMiddlewares(o *options.Options) {
requestid.New(requestid.WithGenerator(func() string {
return util.GenerateRequestID()
})),
DBLogger(),
Cors(),
LoggerToFile(),
UserRateLimiter(),
Limiter(),
Authentication(o.ComponentConfig.Default),
Authorization(o),
Admission(),
Logger(),
)
}
9 changes: 5 additions & 4 deletions cmd/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package options
import (
"fmt"
"os"
"time"

pixiuConfig "github.com/caoyingjunz/pixiulib/config"
"github.com/gin-gonic/gin"
Expand All @@ -40,6 +41,8 @@ const (
defaultTokenKey = "pixiu"
defaultConfigFile = "/etc/pixiu/config.yaml"
defaultLogFormat = config.LogFormatJson

defaultSlowSQLDuration = 1 * time.Second
)

// Options has all the params needed to run a pixiu
Expand Down Expand Up @@ -131,11 +134,9 @@ func (o *Options) registerDatabase() error {
sqlConfig.Port,
sqlConfig.Name)

opt := &gorm.Config{}
if o.ComponentConfig.Default.Mode == "debug" {
opt.Logger = logger.Default.LogMode(logger.Info)
opt := &gorm.Config{
Logger: db.NewLogger(logger.Info, defaultSlowSQLDuration),
}

DB, err := gorm.Open(mysql.Open(dsn), opt)
if err != nil {
return err
Expand Down
73 changes: 73 additions & 0 deletions pkg/db/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2024 The Pixiu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package db

import (
"context"
"time"

"gorm.io/gorm/logger"
)

type (
SQLs []string

DBLogger struct {
logger.LogLevel
SlowThreshold time.Duration // slow SQL queries
}
)

const SQLContextKey = "sqls"

func NewLogger(level logger.LogLevel, slowThreshold time.Duration) *DBLogger {
return &DBLogger{
LogLevel: level,
SlowThreshold: slowThreshold,
}
}

func (l *DBLogger) LogMode(level logger.LogLevel) logger.Interface {
l.LogLevel = level
return l
}

func (l *DBLogger) Info(ctx context.Context, msg string, data ...interface{}) {}

func (l *DBLogger) Warn(ctx context.Context, msg string, data ...interface{}) {}

func (l *DBLogger) Error(ctx context.Context, msg string, data ...interface{}) {}

func (l *DBLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
if l.LogLevel <= logger.Silent {
return
}

sql, _ := fc()
if v := ctx.Value(SQLContextKey); v != nil {
sqls := v.(*SQLs)
*sqls = append(*sqls, sql)
}
}

// GetSQLs returns all the SQL statements executed in the current context.
func GetSQLs(ctx context.Context) SQLs {
if v := ctx.Value(SQLContextKey); v != nil {
return *v.(*SQLs)
}
return SQLs{}
}

0 comments on commit 0944b53

Please sign in to comment.