@@ -28,6 +28,11 @@ type MWHandler struct {
2828type Config struct {
2929 Statter statsd.Statter
3030 StatRate float32
31+
32+ // toggle types of stats sent
33+ NoErrStats bool
34+ NoDurationStats bool
35+ NoStatusCodeStats bool
3136}
3237
3338// JSONStatus is a simple container used for conveying status messages.
@@ -122,7 +127,7 @@ func (m *MWHandler) do(w http.ResponseWriter, r *http.Request, handler Handler)
122127
123128 // Now assume we have an error.
124129 if m .Config .Statter != nil && resp .StatusCode >= 500 {
125- go m .Config . Statter . Inc ( "errors" , 1 , m . Config . StatRate )
130+ go m .reportError ( )
126131 }
127132
128133 // Write the error out
@@ -138,18 +143,10 @@ func (m *MWHandler) do(w http.ResponseWriter, r *http.Request, handler Handler)
138143
139144 if m .Config .Statter != nil {
140145 // Record runtime metric
141- go m .Config .Statter .TimingDuration (
142- "handlers." + handlerName + ".runtime" ,
143- time .Since (startTime ), // delta
144- m .Config .StatRate ,
145- )
146+ go m .reportDuration (handlerName , startTime )
146147
147148 // Record status code metric (default 2xx)
148- go m .Config .Statter .Inc (
149- "handlers." + handlerName + "." + statusCode ,
150- 1 ,
151- m .Config .StatRate ,
152- )
149+ go m .reportStatusCode (handlerName , statusCode )
153150 }
154151 }()
155152
@@ -162,6 +159,38 @@ func (m *MWHandler) do(w http.ResponseWriter, r *http.Request, handler Handler)
162159 return false , r
163160}
164161
162+ func (m * MWHandler ) reportError () {
163+ if m .Config .NoErrStats {
164+ return
165+ }
166+
167+ m .Config .Statter .Inc ("errors" , 1 , m .Config .StatRate )
168+ }
169+
170+ func (m * MWHandler ) reportDuration (handlerName string , startTime time.Time ) {
171+ if m .Config .NoDurationStats {
172+ return
173+ }
174+
175+ m .Config .Statter .TimingDuration (
176+ "handlers." + handlerName + ".runtime" ,
177+ time .Since (startTime ), // delta
178+ m .Config .StatRate ,
179+ )
180+ }
181+
182+ func (m * MWHandler ) reportStatusCode (handlerName string , statusCode string ) {
183+ if m .Config .NoStatusCodeStats {
184+ return
185+ }
186+
187+ m .Config .Statter .Inc (
188+ "handlers." + handlerName + "." + statusCode ,
189+ 1 ,
190+ m .Config .StatRate ,
191+ )
192+ }
193+
165194// WriteJSONStatus is a wrapper for WriteJSONResponse that returns a marshalled JSONStatus blob
166195func WriteJSONStatus (rw http.ResponseWriter , status , message string , statusCode int ) {
167196 jsonData , _ := json .Marshal (& JSONStatus {
0 commit comments