This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
middleware_observation.go
158 lines (126 loc) · 3.24 KB
/
middleware_observation.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
package runtime
import (
"strconv"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/gin-gonic/gin"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/seal-io/walrus/utils/log"
)
// SkipLoggingPaths is a RouterOption to ignore logging for the given paths.
func SkipLoggingPaths(paths ...string) RouterOption {
return routerOption(func(r *Router) {
for i := range paths {
skipLoggingPath(paths[i])
}
})
}
var (
pathSkipLogging = sets.New[string]()
pathPrefixSkipLogging = sets.New[string]()
)
func skipLoggingPath(p string) {
pathSkipLogging.Insert(p)
lastIdx := strings.LastIndex(p, "/") + 1
if lastIdx <= 0 {
return
}
lastSeg := p[lastIdx:]
if !strings.HasPrefix(lastSeg, "*") {
return
}
pathPrefixSkipLogging.Insert(p[:lastIdx])
}
// observing is a gin middleware,
// it measures the http request/response with logging and monitoring.
func observing(c *gin.Context) {
logger := log.WithName("api")
// Validate to skip logging or not.
skipLogging := !logger.Enabled(log.DebugLevel)
reqPath := c.FullPath()
if reqPath == "" {
reqPath = c.Request.URL.Path
}
if pathSkipLogging.Has(reqPath) {
skipLogging = true
} else if i := strings.LastIndex(reqPath, "/") + 1; i > 0 {
if pathPrefixSkipLogging.Has(reqPath[:i]) {
skipLogging = true
}
}
reqProto := c.Request.Proto
reqMethod := c.Request.Method
switch {
case IsUnidiStreamRequest(c):
reqMethod = "US"
case IsBidiStreamRequest(c):
reqMethod = "BS"
}
// Record inflight request.
_statsCollector.requestInflight.
WithLabelValues(reqProto, reqPath, reqMethod).
Inc()
defer func() {
_statsCollector.requestInflight.
WithLabelValues(reqProto, reqPath, reqMethod).
Dec()
}()
start := time.Now()
c.Next()
reqLatency := time.Since(start)
reqSize := c.Request.ContentLength
if v := c.GetInt64("request_size"); v != 0 {
reqSize = v
}
respStatus := strconv.Itoa(c.Writer.Status())
if v := c.GetInt("response_status"); v != 0 {
respStatus = strconv.Itoa(v)
}
var respSize int64
if c.Writer.Written() {
respSize = int64(c.Writer.Size())
} else if v := c.GetInt64("response_size"); v != 0 {
respSize = v
}
// Record request latency.
_statsCollector.requestDurations.
WithLabelValues(reqProto, reqPath, reqMethod, respStatus).
Observe(reqLatency.Seconds())
// Record request time.
_statsCollector.requestCounter.
WithLabelValues(reqProto, reqPath, reqMethod, respStatus).
Inc()
// Record request size.
_statsCollector.requestSizes.
WithLabelValues(reqProto, reqPath, reqMethod).
Observe(float64(reqSize))
// Record response size.
_statsCollector.responseSizes.
WithLabelValues(reqProto, reqPath, reqMethod).
Observe(float64(respSize))
if !skipLogging {
// Complete logging info.
reqSize := humanize.IBytes(uint64(reqSize))
respSize := humanize.IBytes(uint64(respSize))
reqLatency := reqLatency
if reqLatency > time.Minute {
reqLatency -= reqLatency % time.Second
}
reqClientIP := c.ClientIP()
reqPath := c.Request.URL.Path
if raw := c.Request.URL.RawQuery; raw != "" {
reqPath = reqPath + "?" + raw
}
logger.Debugf("%s | %8s | %10s | %10s | %13v | %15s | %-7s %s",
respStatus,
reqProto,
reqSize,
respSize,
reqLatency,
reqClientIP,
reqMethod,
reqPath,
)
}
}