Browse Source

tsweb: add optional on completion callback func

Updates corp#17075

Co-Authored-By: Anton Tolchanov <[email protected]>
Signed-off-by: Kristoffer Dalby <[email protected]>
Kristoffer Dalby 2 years ago
parent
commit
95f266f1ce
1 changed files with 12 additions and 0 deletions
  1. 12 0
      tsweb/tsweb.go

+ 12 - 0
tsweb/tsweb.go

@@ -254,11 +254,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
@@ -435,6 +443,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 {
 		// Only increment metrics for buckets that result in good HTTP statuses
 		// or when we know the start was already counted.