@@ -34,7 +34,7 @@ type Config struct {
3434 DisableMeasureInflight bool
3535 // IgnoredPaths is a list of paths that will not be measured for the request duration
3636 // and the response size. They will still be counted in the RequestsInflight metric.
37- IgnoredPaths map [ string ] struct {}
37+ IgnoredPaths [] string
3838}
3939
4040func (c * Config ) defaults () {
@@ -51,14 +51,31 @@ func (c *Config) defaults() {
5151// receive a `Reporter` that knows how to get the data the Middleware service needs
5252// to measure.
5353type 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 {}
5560}
5661
5762// New returns the a Middleware service.
5863func New (cfg Config ) Middleware {
5964 cfg .defaults ()
6065
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+ }
6279
6380 return m
6481}
@@ -78,19 +95,20 @@ func (m Middleware) Measure(handlerID string, reporter Reporter, next func()) {
7895 }
7996
8097 // Measure inflights if required.
81- if ! m .cfg . DisableMeasureInflight {
98+ if ! m .disableMeasureInflight {
8299 props := metrics.HTTPProperties {
83- Service : m .cfg . Service ,
100+ Service : m .service ,
84101 ID : hid ,
85102 }
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 )
88105 }
89106
90107 // Start the timer and when finishing measure the duration.
91108 start := time .Now ()
92109 defer func () {
93- if _ , isPathIgnored := m .cfg .IgnoredPaths [reporter .URLPath ()]; isPathIgnored {
110+ _ , shouldIgnore := m .ignoredPaths [reporter .URLPath ()]
111+ if shouldIgnore {
94112 return
95113 }
96114
@@ -100,23 +118,23 @@ func (m Middleware) Measure(handlerID string, reporter Reporter, next func()) {
100118 // first number of the status code because is the least
101119 // required identification way.
102120 var code string
103- if m .cfg . GroupedStatus {
121+ if m .groupedStatus {
104122 code = fmt .Sprintf ("%dxx" , reporter .StatusCode ()/ 100 )
105123 } else {
106124 code = strconv .Itoa (reporter .StatusCode ())
107125 }
108126
109127 props := metrics.HTTPReqProperties {
110- Service : m .cfg . Service ,
128+ Service : m .service ,
111129 ID : hid ,
112130 Method : reporter .Method (),
113131 Code : code ,
114132 }
115- m .cfg . Recorder .ObserveHTTPRequestDuration (ctx , props , duration )
133+ m .recorder .ObserveHTTPRequestDuration (ctx , props , duration )
116134
117135 // 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 ())
120138 }
121139 }()
122140
0 commit comments