Skip to content

Commit 5cb9780

Browse files
committed
Improve UX of ignore paths
Signed-off-by: Xabier Larrakoetxea <[email protected]>
1 parent 8005104 commit 5cb9780

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- Added `IgnoredPaths` option to ignore measuring specific exact paths.
8+
9+
510
## [0.12.0] - 2024-04-19
611

712
### Changed

middleware/middleware.go

+31-13
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 map[string]struct{}
37+
IgnoredPaths []string
3838
}
3939

4040
func (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.
5353
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{}
5560
}
5661

5762
// New returns the a Middleware service.
5863
func 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

middleware/middleware_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ func TestMiddlewareMeasure(t *testing.T) {
4949
handlerID: "test01",
5050
config: func() middleware.Config {
5151
return middleware.Config{
52-
Service: "svc1",
53-
IgnoredPaths: map[string]struct{}{
54-
"/ignored": {},
55-
},
52+
Service: "svc1",
53+
IgnoredPaths: []string{"/ignored"},
5654
}
5755
},
5856
mock: func(mrec *mockmetrics.Recorder, mrep *mockmiddleware.Reporter) {

0 commit comments

Comments
 (0)