Skip to content

Commit 8005104

Browse files
authored
Merge pull request #111 from angristan/skip-paths
Add IgnoredPaths option
2 parents 3bddbe2 + 764140e commit 8005104

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

Readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ This setting will disable measuring the size of the responses. By default measur
155155

156156
This settings will disable measuring the number of requests being handled concurrently by the handlers.
157157

158+
### IgnoredPaths
159+
160+
This setting is a list of paths that will not be measured for the request duration and the response size. They will still be counted in the RequestsInflight metric.
161+
158162
#### Custom handler ID
159163

160164
One of the options that you need to pass when wrapping the handler with the middleware is `handlerID`, this has 2 working ways.

middleware/middleware.go

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ type Config struct {
3232
// DisableMeasureInflight will disable the recording metrics about the inflight requests number,
3333
// by default measuring inflights is enabled (`DisableMeasureInflight` is false).
3434
DisableMeasureInflight bool
35+
// IgnoredPaths is a list of paths that will not be measured for the request duration
36+
// and the response size. They will still be counted in the RequestsInflight metric.
37+
IgnoredPaths map[string]struct{}
3538
}
3639

3740
func (c *Config) defaults() {
@@ -87,6 +90,10 @@ func (m Middleware) Measure(handlerID string, reporter Reporter, next func()) {
8790
// Start the timer and when finishing measure the duration.
8891
start := time.Now()
8992
defer func() {
93+
if _, isPathIgnored := m.cfg.IgnoredPaths[reporter.URLPath()]; isPathIgnored {
94+
return
95+
}
96+
9097
duration := time.Since(start)
9198

9299
// If we need to group the status code, it uses the

middleware/middleware_test.go

+34
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)