-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_query.go
92 lines (81 loc) · 2.32 KB
/
type_query.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package telescope
import (
"fmt"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"os"
"runtime/debug"
"strings"
"time"
)
// Query @Bean
type Query struct {
Connection string `json:"connection,omitempty"`
Bindings []string `json:"bindings,omitempty"`
Sql string `json:"sql,omitempty"`
Time string `json:"time,omitempty"`
Slow bool `json:"slow,omitempty"`
File string `json:"file,omitempty"`
Line string `json:"line,omitempty"`
Hash string `json:"hash,omitempty"`
Hostname string `json:"hostname"`
}
func (q *Query) Init() {
q.Hostname, _ = os.Hostname()
}
func (q *Query) BindType() string {
return "query"
}
// QuerySplit 切割标识, 这个标识以后的代码才是业务的
var QuerySplit = "/app/entity/"
func (q *Query) Handler(entry *logrus.Entry) (*entries, []tag) {
if strings.Index(entry.Message, "telescope_") != -1 {
return nil, nil
}
b := *q
// 根据模型目录 /app/entity/ 定位业务调用
stack := string(debug.Stack())
arr := strings.Split(string(stack), "\n")
status := 0
for _, str := range arr {
if status <= 1 {
index := strings.Index(str, QuerySplit)
if index != -1 {
status++ // 模型调用自身,2次后 再下一层就是业务代码
b.Connection = str[index:] // 这里记录模型
}
} else if strings.Index(str, QuerySplit) == -1 {
// 第一个非模型目录
arr2 := strings.Split(str, "/")
if len(arr2) >= 4 {
for i := len(arr2) - 4; i < len(arr2); i++ {
b.File = b.File + "/" + arr2[i]
}
} else {
for i := 0; i < len(arr2); i++ {
b.File = b.File + "/" + arr2[i]
}
}
break
}
}
b.Sql = entry.Message
t, ok := entry.Data["t"]
if ok {
b.Time = fmt.Sprintf("%.2f", t)
}
if b.File == "" {
// 如果找不到业务级别的文件, 那就可能不是模型触发的, 这里是可见查询mysql.log
b.File, b.Line = GetStackCallFile(stack, "go-home-admin/home/bootstrap/services/logs/mysql")
}
id := uuid.NewV4().String()
return &entries{
Uuid: id,
BatchId: NewtelescopeHook().TelescopeUUID(),
FamilyHash: nil,
ShouldDisplayOnIndex: 1,
Type: b.BindType(),
Content: ToContent(b),
CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
}, nil
}