Skip to content

Commit 764140e

Browse files
committed
Use map instead of list, and add test
1 parent f8708d6 commit 764140e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

middleware/middleware.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 []string
37+
IgnoredPaths map[string]struct{}
3838
}
3939

4040
func (c *Config) defaults() {
@@ -90,10 +90,8 @@ func (m Middleware) Measure(handlerID string, reporter Reporter, next func()) {
9090
// Start the timer and when finishing measure the duration.
9191
start := time.Now()
9292
defer func() {
93-
for _, path := range m.cfg.IgnoredPaths {
94-
if path == reporter.URLPath() {
95-
return
96-
}
93+
if _, isPathIgnored := m.cfg.IgnoredPaths[reporter.URLPath()]; isPathIgnored {
94+
return
9795
}
9896

9997
duration := time.Since(start)

middleware/middleware_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestMiddlewareMeasure(t *testing.T) {
3232
mrep.On("StatusCode").Once().Return(418)
3333
mrep.On("Method").Once().Return("PATCH")
3434
mrep.On("BytesWritten").Once().Return(int64(42))
35+
mrep.On("URLPath").Once().Return("/test/01")
3536

3637
// Recorder mocks.
3738
expProps := metrics.HTTPProperties{Service: "svc1", ID: "test01"}
@@ -44,6 +45,35 @@ func TestMiddlewareMeasure(t *testing.T) {
4445
},
4546
},
4647

48+
"Having an ignored path in the config, it should not measure the metrics for the ignored path.": {
49+
handlerID: "test01",
50+
config: func() middleware.Config {
51+
return middleware.Config{
52+
Service: "svc1",
53+
IgnoredPaths: map[string]struct{}{
54+
"/ignored": {},
55+
},
56+
}
57+
},
58+
mock: func(mrec *mockmetrics.Recorder, mrep *mockmiddleware.Reporter) {
59+
// Reporter mocks.
60+
mrep.On("Context").Once().Return(context.TODO())
61+
mrep.AssertNotCalled(t, "StatusCode")
62+
mrep.AssertNotCalled(t, "Method")
63+
mrep.AssertNotCalled(t, "BytesWritten")
64+
mrep.On("URLPath").Once().Return("/ignored")
65+
66+
// Recorder mocks.
67+
expProps := metrics.HTTPProperties{Service: "svc1", ID: "test01"}
68+
expRepProps := metrics.HTTPReqProperties{Service: "svc1", ID: "test01", Method: "PATCH", Code: "418"}
69+
70+
mrec.On("AddInflightRequests", mock.Anything, expProps, 1).Once()
71+
mrec.On("AddInflightRequests", mock.Anything, expProps, -1).Once()
72+
mrec.AssertNotCalled(t, "ObserveHTTPRequestDuration", mock.Anything, expRepProps, mock.Anything)
73+
mrec.AssertNotCalled(t, "ObserveHTTPResponseSize", mock.Anything, expRepProps, int64(42))
74+
},
75+
},
76+
4777
"Without having handler ID, it should measure the metrics using the request path.": {
4878
handlerID: "",
4979
config: func() middleware.Config {
@@ -56,6 +86,7 @@ func TestMiddlewareMeasure(t *testing.T) {
5686
mrep.On("StatusCode").Once().Return(418)
5787
mrep.On("Method").Once().Return("PATCH")
5888
mrep.On("BytesWritten").Once().Return(int64(42))
89+
mrep.On("URLPath").Once().Return("/test/01")
5990

6091
// Recorder mocks.
6192
expRepProps := metrics.HTTPReqProperties{ID: "/test/01", Method: "PATCH", Code: "418"}
@@ -80,6 +111,7 @@ func TestMiddlewareMeasure(t *testing.T) {
80111
mrep.On("StatusCode").Once().Return(418)
81112
mrep.On("Method").Once().Return("PATCH")
82113
mrep.On("BytesWritten").Once().Return(int64(42))
114+
mrep.On("URLPath").Once().Return("/test/01")
83115

84116
// Recorder mocks.
85117
expRepProps := metrics.HTTPReqProperties{ID: "test01", Method: "PATCH", Code: "4xx"}
@@ -104,6 +136,7 @@ func TestMiddlewareMeasure(t *testing.T) {
104136
mrep.On("StatusCode").Once().Return(418)
105137
mrep.On("Method").Once().Return("PATCH")
106138
mrep.On("BytesWritten").Once().Return(int64(42))
139+
mrep.On("URLPath").Once().Return("/test/01")
107140

108141
// Recorder mocks.
109142
expRepProps := metrics.HTTPReqProperties{ID: "test01", Method: "PATCH", Code: "418"}
@@ -125,6 +158,7 @@ func TestMiddlewareMeasure(t *testing.T) {
125158
mrep.On("Context").Once().Return(context.TODO())
126159
mrep.On("StatusCode").Once().Return(418)
127160
mrep.On("Method").Once().Return("PATCH")
161+
mrep.On("URLPath").Once().Return("/test/01")
128162

129163
// Recorder mocks.
130164
expRepProps := metrics.HTTPReqProperties{ID: "test01", Method: "PATCH", Code: "418"}

0 commit comments

Comments
 (0)