tsweb: add optional on completion callback func

Updates corp#17075

Co-Authored-By: Anton Tolchanov <anton@tailscale.com>
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2024-02-22 09:42:12 +01:00
parent 232a2d627c
commit 5ef0d2f1b3
No known key found for this signature in database
1 changed files with 12 additions and 0 deletions

View File

@ -253,11 +253,19 @@ type HandlerOptions struct {
// is intended to be used to present pretty error pages if
// the user agent is determined to be a browser.
OnError ErrorHandlerFunc
// OnCompletion is called when ServeHTTP is finished and gets
// useful data that the implementor can use for metrics.
OnCompletion OnCompletionFunc
}
// ErrorHandlerFunc is called to present a error response.
type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, HTTPError)
// OnCompletionFunc is called when ServeHTTP is finished and gets
// useful data that the implementor can use for metrics.
type OnCompletionFunc func(*http.Request, AccessLogRecord)
// ReturnHandlerFunc is an adapter to allow the use of ordinary
// functions as ReturnHandlers. If f is a function with the
// appropriate signature, ReturnHandlerFunc(f) is a ReturnHandler that
@ -400,6 +408,10 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
if h.opts.OnCompletion != nil {
h.opts.OnCompletion(r, msg)
}
if bs := h.opts.BucketedStats; bs != nil && bs.Finished != nil {
bs.Finished.Add(bucket, 1)
}