-
Notifications
You must be signed in to change notification settings - Fork 2
/
tracing.go
165 lines (139 loc) · 3.36 KB
/
tracing.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package tracing
import (
"bytes"
"io"
"time"
"github.com/asaskevich/EventBus"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/techquest-tech/gin-shared/pkg/core"
"github.com/techquest-tech/gin-shared/pkg/event"
"go.uber.org/zap"
)
const (
KeyTracingID = "tracingID"
)
// FullRequestDetails
type TracingDetails struct {
Optionname string `gorm:"size:64"`
Uri string `gorm:"size:256"`
Method string `gorm:"size:16"`
Body string
Durtion time.Duration
Status int
TargetID uint
Resp string
ClientIP string `gorm:"size:64"`
UserAgent string `gorm:"size:256"`
Device string `gorm:"size:64"`
// Props map[string]interface{}
}
type RespLogging struct {
gin.ResponseWriter
cache *bytes.Buffer
}
func (w RespLogging) Write(b []byte) (int, error) {
w.cache.Write(b)
return w.ResponseWriter.Write(b)
}
type TracingRequestService struct {
core.DefaultComponent
Bus EventBus.Bus
Log *zap.Logger
Console bool
Request bool
Resp bool
Included []string
Excluded []string
}
func (tr *TracingRequestService) Priority() int { return 5 }
func (tr *TracingRequestService) OnEngineInited(r *gin.Engine) error {
r.Use(tr.LogfullRequestDetails)
return nil
}
var InitTracingService = func(bus EventBus.Bus, logger *zap.Logger) *TracingRequestService {
sr := &TracingRequestService{
Bus: bus,
Log: logger,
}
settings := viper.Sub("tracing")
if settings == nil {
logger.Warn("tracing module loaded, but disabled.")
return nil
}
// if settings != nil {
settings.Unmarshal(sr)
// }
logger.Info("tracing service is enabled.")
if (sr.Request || sr.Resp) && sr.Console {
c := InitConsoleTracingService(sr.Log)
sr.Bus.SubscribeAsync(event.EventTracing, c.LogBody, false)
}
core.RegisterComponent(sr)
return sr
}
func (tr *TracingRequestService) LogfullRequestDetails(c *gin.Context) {
start := time.Now()
reqcache := make([]byte, 0)
uri := c.Request.RequestURI
method := c.Request.Method
matchedUrl := c.FullPath()
if matchedUrl == "" {
tr.Log.Warn("matched path failed. use uri as matched url", zap.String("uri", uri))
matchedUrl = uri
}
matched := len(tr.Included) == 0
for _, item := range tr.Included {
if matchedUrl == item {
matched = true
break
}
}
if matched {
for _, item := range tr.Excluded {
if matchedUrl == item {
matched = false
break
}
}
}
if tr.Request && matched {
if c.Request.Body != nil {
reqcache, _ = io.ReadAll(c.Request.Body)
c.Request.Body = io.NopCloser(bytes.NewBuffer(reqcache))
}
}
// respcache := make([]byte, 0)
writer := &RespLogging{
cache: bytes.NewBuffer([]byte{}),
ResponseWriter: c.Writer,
}
if tr.Resp && matched {
c.Writer = writer
}
c.Next()
dur := time.Since(start)
status := c.Writer.Status()
rawID := c.GetUint(KeyTracingID)
respcache := writer.cache.Bytes()
fullLogging := &TracingDetails{
Optionname: matchedUrl,
Uri: uri,
Method: method,
Body: string(reqcache),
Durtion: dur,
Status: status,
TargetID: rawID,
Resp: string(respcache),
ClientIP: c.ClientIP(),
UserAgent: c.Request.UserAgent(),
Device: c.GetHeader("deviceID"),
}
tr.Bus.Publish(event.EventTracing, fullLogging)
}
func EnabledTracing() {
core.Provide(InitTracingService)
core.ProvideStartup(func(t *TracingRequestService) core.Startup {
return t
})
}