@@ -34,7 +34,7 @@ type Config struct {
34
34
DisableMeasureInflight bool
35
35
// IgnoredPaths is a list of paths that will not be measured for the request duration
36
36
// and the response size. They will still be counted in the RequestsInflight metric.
37
- IgnoredPaths map [ string ] struct {}
37
+ IgnoredPaths [] string
38
38
}
39
39
40
40
func (c * Config ) defaults () {
@@ -51,14 +51,31 @@ func (c *Config) defaults() {
51
51
// receive a `Reporter` that knows how to get the data the Middleware service needs
52
52
// to measure.
53
53
type Middleware struct {
54
- cfg Config
54
+ recorder metrics.Recorder
55
+ service string
56
+ groupedStatus bool
57
+ disableMeasureSize bool
58
+ disableMeasureInflight bool
59
+ ignoredPaths map [string ]struct {}
55
60
}
56
61
57
62
// New returns the a Middleware service.
58
63
func New (cfg Config ) Middleware {
59
64
cfg .defaults ()
60
65
61
- m := Middleware {cfg : cfg }
66
+ ignPaths := map [string ]struct {}{}
67
+ for _ , path := range cfg .IgnoredPaths {
68
+ ignPaths [path ] = struct {}{}
69
+ }
70
+
71
+ m := Middleware {
72
+ recorder : cfg .Recorder ,
73
+ service : cfg .Service ,
74
+ groupedStatus : cfg .GroupedStatus ,
75
+ disableMeasureSize : cfg .DisableMeasureSize ,
76
+ disableMeasureInflight : cfg .DisableMeasureInflight ,
77
+ ignoredPaths : ignPaths ,
78
+ }
62
79
63
80
return m
64
81
}
@@ -78,19 +95,20 @@ func (m Middleware) Measure(handlerID string, reporter Reporter, next func()) {
78
95
}
79
96
80
97
// Measure inflights if required.
81
- if ! m .cfg . DisableMeasureInflight {
98
+ if ! m .disableMeasureInflight {
82
99
props := metrics.HTTPProperties {
83
- Service : m .cfg . Service ,
100
+ Service : m .service ,
84
101
ID : hid ,
85
102
}
86
- m .cfg . Recorder .AddInflightRequests (ctx , props , 1 )
87
- defer m .cfg . Recorder .AddInflightRequests (ctx , props , - 1 )
103
+ m .recorder .AddInflightRequests (ctx , props , 1 )
104
+ defer m .recorder .AddInflightRequests (ctx , props , - 1 )
88
105
}
89
106
90
107
// Start the timer and when finishing measure the duration.
91
108
start := time .Now ()
92
109
defer func () {
93
- if _ , isPathIgnored := m .cfg .IgnoredPaths [reporter .URLPath ()]; isPathIgnored {
110
+ _ , shouldIgnore := m .ignoredPaths [reporter .URLPath ()]
111
+ if shouldIgnore {
94
112
return
95
113
}
96
114
@@ -100,23 +118,23 @@ func (m Middleware) Measure(handlerID string, reporter Reporter, next func()) {
100
118
// first number of the status code because is the least
101
119
// required identification way.
102
120
var code string
103
- if m .cfg . GroupedStatus {
121
+ if m .groupedStatus {
104
122
code = fmt .Sprintf ("%dxx" , reporter .StatusCode ()/ 100 )
105
123
} else {
106
124
code = strconv .Itoa (reporter .StatusCode ())
107
125
}
108
126
109
127
props := metrics.HTTPReqProperties {
110
- Service : m .cfg . Service ,
128
+ Service : m .service ,
111
129
ID : hid ,
112
130
Method : reporter .Method (),
113
131
Code : code ,
114
132
}
115
- m .cfg . Recorder .ObserveHTTPRequestDuration (ctx , props , duration )
133
+ m .recorder .ObserveHTTPRequestDuration (ctx , props , duration )
116
134
117
135
// Measure size of response if required.
118
- if ! m .cfg . DisableMeasureSize {
119
- m .cfg . Recorder .ObserveHTTPResponseSize (ctx , props , reporter .BytesWritten ())
136
+ if ! m .disableMeasureSize {
137
+ m .recorder .ObserveHTTPResponseSize (ctx , props , reporter .BytesWritten ())
120
138
}
121
139
}()
122
140
0 commit comments