Skip to content

Allow Reporter to provide handler ID after the underlying call. #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ func (m Middleware) Measure(handlerID string, reporter Reporter, next func()) {
code = strconv.Itoa(reporter.StatusCode())
}

// If reporter was unable to provide a handler ID *before* calling the underlying
// handler, give it a chance to report it *after* the call.
if hid == "" {
hid = reporter.URLPath()
}

props := metrics.HTTPReqProperties{
Service: m.cfg.Service,
ID: hid,
Expand Down
24 changes: 24 additions & 0 deletions middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ func TestMiddlewareMeasure(t *testing.T) {
mrec.On("ObserveHTTPRequestDuration", mock.Anything, expRepProps, mock.Anything).Once()
},
},

"If a handler ID isn't available before calling the handler, it should be retried after.": {
handlerID: "",
config: func() middleware.Config {
return middleware.Config{}
},
mock: func(mrec *mockmetrics.Recorder, mrep *mockmiddleware.Reporter) {
// Reporter mocks.
mrep.On("URLPath").Once().Return("") // Return no path on first call.
mrep.On("URLPath").Once().Return("/test/01") // Only on the second.
mrep.On("Context").Once().Return(context.TODO())
mrep.On("StatusCode").Once().Return(418)
mrep.On("Method").Once().Return("PATCH")
mrep.On("BytesWritten").Once().Return(int64(42))

// Recorder mocks.
expRepProps := metrics.HTTPReqProperties{ID: "/test/01", Method: "PATCH", Code: "418"}

mrec.On("AddInflightRequests", mock.Anything, mock.Anything, mock.Anything).Once()
mrec.On("AddInflightRequests", mock.Anything, mock.Anything, mock.Anything).Once()
mrec.On("ObserveHTTPRequestDuration", mock.Anything, expRepProps, mock.Anything).Once()
mrec.On("ObserveHTTPResponseSize", mock.Anything, expRepProps, mock.Anything).Once()
},
},
}

for name, test := range tests {
Expand Down