Skip to content

Commit

Permalink
Merge pull request #111 from angristan/skip-paths
Browse files Browse the repository at this point in the history
Add IgnoredPaths option
  • Loading branch information
slok authored Sep 5, 2024
2 parents 3bddbe2 + 764140e commit 8005104
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ This setting will disable measuring the size of the responses. By default measur

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

### IgnoredPaths

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.

#### Custom handler ID

One of the options that you need to pass when wrapping the handler with the middleware is `handlerID`, this has 2 working ways.
Expand Down
7 changes: 7 additions & 0 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type Config struct {
// DisableMeasureInflight will disable the recording metrics about the inflight requests number,
// by default measuring inflights is enabled (`DisableMeasureInflight` is false).
DisableMeasureInflight bool
// IgnoredPaths 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.
IgnoredPaths map[string]struct{}
}

func (c *Config) defaults() {
Expand Down Expand Up @@ -87,6 +90,10 @@ func (m Middleware) Measure(handlerID string, reporter Reporter, next func()) {
// Start the timer and when finishing measure the duration.
start := time.Now()
defer func() {
if _, isPathIgnored := m.cfg.IgnoredPaths[reporter.URLPath()]; isPathIgnored {
return
}

duration := time.Since(start)

// If we need to group the status code, it uses the
Expand Down
34 changes: 34 additions & 0 deletions middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestMiddlewareMeasure(t *testing.T) {
mrep.On("StatusCode").Once().Return(418)
mrep.On("Method").Once().Return("PATCH")
mrep.On("BytesWritten").Once().Return(int64(42))
mrep.On("URLPath").Once().Return("/test/01")

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

"Having an ignored path in the config, it should not measure the metrics for the ignored path.": {
handlerID: "test01",
config: func() middleware.Config {
return middleware.Config{
Service: "svc1",
IgnoredPaths: map[string]struct{}{
"/ignored": {},
},
}
},
mock: func(mrec *mockmetrics.Recorder, mrep *mockmiddleware.Reporter) {
// Reporter mocks.
mrep.On("Context").Once().Return(context.TODO())
mrep.AssertNotCalled(t, "StatusCode")
mrep.AssertNotCalled(t, "Method")
mrep.AssertNotCalled(t, "BytesWritten")
mrep.On("URLPath").Once().Return("/ignored")

// Recorder mocks.
expProps := metrics.HTTPProperties{Service: "svc1", ID: "test01"}
expRepProps := metrics.HTTPReqProperties{Service: "svc1", ID: "test01", Method: "PATCH", Code: "418"}

mrec.On("AddInflightRequests", mock.Anything, expProps, 1).Once()
mrec.On("AddInflightRequests", mock.Anything, expProps, -1).Once()
mrec.AssertNotCalled(t, "ObserveHTTPRequestDuration", mock.Anything, expRepProps, mock.Anything)
mrec.AssertNotCalled(t, "ObserveHTTPResponseSize", mock.Anything, expRepProps, int64(42))
},
},

"Without having handler ID, it should measure the metrics using the request path.": {
handlerID: "",
config: func() middleware.Config {
Expand All @@ -56,6 +86,7 @@ func TestMiddlewareMeasure(t *testing.T) {
mrep.On("StatusCode").Once().Return(418)
mrep.On("Method").Once().Return("PATCH")
mrep.On("BytesWritten").Once().Return(int64(42))
mrep.On("URLPath").Once().Return("/test/01")

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

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

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

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

0 comments on commit 8005104

Please sign in to comment.