-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathsqlite_tx_logs.go
More file actions
67 lines (61 loc) · 1.71 KB
/
sqlite_tx_logs.go
File metadata and controls
67 lines (61 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package common
import (
"context"
"fmt"
"runtime"
"strings"
"time"
"github.com/1Panel-dev/1Panel/agent/global"
"gorm.io/gorm"
)
type contextKey string
func initializeTxWatch(db *gorm.DB) {
_ = db.Callback().Create().Before("gorm:begin_transaction").Register(
"my_plugin:before_begin",
func(db *gorm.DB) {
var caller []string
for i := 0; ; i++ {
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
funcName := runtime.FuncForPC(pc).Name()
if !strings.HasPrefix(funcName, "github.com/1Panel-dev/1Panel") {
continue
}
fileParts := strings.Split(file, "/")
fileName := fileParts[len(fileParts)-1]
caller = append(caller, fmt.Sprintf("%s/%s:%d", funcName, fileName, line))
}
txID := generateTransactionID()
db.Statement.Context = context.WithValue(
db.Statement.Context,
contextKey("tx_id"), txID,
)
db.Statement.Context = context.WithValue(
db.Statement.Context,
contextKey("tx_start"), time.Now(),
)
global.LOG.Debugf("[%s] tx start \n%s", txID, strings.Join(caller, "\n"))
},
)
_ = db.Callback().Create().After("gorm:commit_or_rollback_transaction").Register(
"my_plugin:after_commit_or_rollback",
func(db *gorm.DB) {
ctx := db.Statement.Context
txID, _ := ctx.Value(contextKey("tx_id")).(string)
startTime, _ := ctx.Value(contextKey("tx_start")).(time.Time)
if txID != "" {
duration := time.Since(startTime)
if db.Error != nil {
global.LOG.Debugf("[%s] tx rollback! time: %v, err: %v", txID, duration, db.Error)
} else {
global.LOG.Debugf("[%s] tx commit! time: %v", txID, duration)
}
}
},
)
}
func generateTransactionID() string {
return fmt.Sprintf("tx_%d", time.Now().UnixNano())
}