Skip to content

Commit 52fda66

Browse files
committed
feat:新增config项is_only_route,默认false,是否忽略记录路由以外的请求,如:404、静态文件请求
1 parent e948f6a commit 52fda66

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

telescope_provider.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
// Providers @Bean
1818
type Providers struct {
19+
//TODO 默认配置default不生效,config不声明telescope.connect会报错
1920
Mysql *gorm.DB `inject:"mysql, @config(telescope.connect, default)"`
2021
isOpen bool
2122

@@ -63,9 +64,15 @@ func (t *Providers) AddRoute(v Type) {
6364

6465
// @Bean
6566
type telescopeHook struct {
66-
mysql *gorm.DB
67-
CidToUUID sync.Map
68-
hostname string
67+
mysql *gorm.DB
68+
CidToUUID sync.Map
69+
hostname string
70+
isOnlyRoute bool
71+
}
72+
73+
// TODO 修正不声明config就报错后,可取消这func,改为依赖注入
74+
func (t *telescopeHook) Init() {
75+
t.isOnlyRoute = app.Config("telescope.is_only_route", false)
6976
}
7077

7178
func (t *telescopeHook) Levels() []logrus.Level {
@@ -78,6 +85,13 @@ func (t *telescopeHook) Fire(entry *logrus.Entry) error {
7885
m = "log"
7986
}
8087
mType := m.(string)
88+
//忽略路由以外的请求
89+
if mType == "request" && t.isOnlyRoute {
90+
ctx := entry.Context.(*gin.Context)
91+
if ctx.FullPath() == "" {
92+
return nil
93+
}
94+
}
8195
route, ok := Routes[mType]
8296
if ok {
8397
telescopeEntries, tags := route.Handler(entry)

z_inject_gen.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
gorm "gorm.io/gorm"
77
)
88

9-
var _ProvidersSingle *Providers
109
var _telescopeHookSingle *telescopeHook
10+
var _ProvidersSingle *Providers
1111
var _BatchSingle *Batch
1212
var _CacheSingle *Cache
1313
var _ClientRequestSingle *ClientRequest
@@ -18,6 +18,7 @@ var _ExceptionSingle *Exception
1818
var _JobSingle *Job
1919
var _LogSingle *Log
2020
var _ModelSingle *Model
21+
var _NotificationSingle *Notification
2122
var _QuerySingle *Query
2223
var _RedisSingle *Redis
2324
var _RequestSingle *Request
@@ -26,8 +27,8 @@ var _TcpSingle *Tcp
2627

2728
func GetAllProvider() []interface{} {
2829
return []interface{}{
29-
NewProviders(),
3030
NewtelescopeHook(),
31+
NewProviders(),
3132
NewBatch(),
3233
NewCache(),
3334
NewClientRequest(),
@@ -38,6 +39,7 @@ func GetAllProvider() []interface{} {
3839
NewJob(),
3940
NewLog(),
4041
NewModel(),
42+
NewNotification(),
4143
NewQuery(),
4244
NewRedis(),
4345
NewRequest(),
@@ -46,6 +48,13 @@ func GetAllProvider() []interface{} {
4648
}
4749
}
4850

51+
func NewtelescopeHook() *telescopeHook {
52+
if _telescopeHookSingle == nil {
53+
_telescopeHookSingle = &telescopeHook{}
54+
providers.AfterProvider(_telescopeHookSingle, "")
55+
}
56+
return _telescopeHookSingle
57+
}
4958
func NewProviders() *Providers {
5059
if _ProvidersSingle == nil {
5160
_ProvidersSingle = &Providers{}
@@ -54,13 +63,6 @@ func NewProviders() *Providers {
5463
}
5564
return _ProvidersSingle
5665
}
57-
func NewtelescopeHook() *telescopeHook {
58-
if _telescopeHookSingle == nil {
59-
_telescopeHookSingle = &telescopeHook{}
60-
providers.AfterProvider(_telescopeHookSingle, "")
61-
}
62-
return _telescopeHookSingle
63-
}
6466
func NewBatch() *Batch {
6567
if _BatchSingle == nil {
6668
_BatchSingle = &Batch{}
@@ -131,6 +133,13 @@ func NewModel() *Model {
131133
}
132134
return _ModelSingle
133135
}
136+
func NewNotification() *Notification {
137+
if _NotificationSingle == nil {
138+
_NotificationSingle = &Notification{}
139+
providers.AfterProvider(_NotificationSingle, "")
140+
}
141+
return _NotificationSingle
142+
}
134143
func NewQuery() *Query {
135144
if _QuerySingle == nil {
136145
_QuerySingle = &Query{}

0 commit comments

Comments
 (0)