소스 검색

tsweb: add StatusCodeCounters to HandlerOptions

This lets servers using tsweb register expvars
that will track the number of requests ending
in 200s/300s/400s/500s.

Signed-off-by: Josh Bleecher Snyder <[email protected]>
Josh Bleecher Snyder 5 년 전
부모
커밋
3b46655dbb
1개의 변경된 파일9개의 추가작업 그리고 0개의 파일을 삭제
  1. 9 0
      tsweb/tsweb.go

+ 9 - 0
tsweb/tsweb.go

@@ -161,6 +161,11 @@ type HandlerOptions struct {
 	Quiet200s bool // if set, do not log successfully handled HTTP requests
 	Logf      logger.Logf
 	Now       func() time.Time // if nil, defaults to time.Now
+
+	// If non-nil, StatusCodeCounters maintains counters
+	// of status codes for handled responses.
+	// The keys are "1xx", "2xx", "3xx", "4xx", and "5xx".
+	StatusCodeCounters *expvar.Map
 }
 
 // StdHandler converts a ReturnHandler into a standard http.Handler.
@@ -278,6 +283,10 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	if msg.Code != 200 || !h.opts.Quiet200s {
 		h.opts.Logf("%s", msg)
 	}
+
+	if h.opts.StatusCodeCounters != nil {
+		key := fmt.Sprintf("%dxx", msg.Code/100)
+		h.opts.StatusCodeCounters.Add(key, 1)
 	}
 }