Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit c54f0c8

Browse files
authored
Merge pull request #35 from InVisionApp/nostats
Add ability to turn off specific stats
2 parents 1631744 + 7d1a2fb commit c54f0c8

2 files changed

Lines changed: 102 additions & 11 deletions

File tree

rye.go

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ type MWHandler struct {
2828
type 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
166195
func WriteJSONStatus(rw http.ResponseWriter, status, message string, statusCode int) {
167196
jsonData, _ := json.Marshal(&JSONStatus{

rye_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,68 @@ var _ = Describe("Rye", func() {
267267
})
268268
})
269269

270+
Context("when error stats are turned off", func() {
271+
It("should not call Inc or TimingDuration", func() {
272+
ryeConfig := Config{
273+
Statter: fakeStatter,
274+
NoErrStats: true,
275+
}
276+
277+
handler := NewMWHandler(ryeConfig)
278+
//use the failureHandler so an error would be reported
279+
h := handler.Handle([]Handler{failureHandler})
280+
h.ServeHTTP(response, request)
281+
282+
time.Sleep(time.Millisecond * 10)
283+
284+
Expect(fakeStatter.IncCallCount()).To(Equal(1))
285+
metric, _, _ := fakeStatter.IncArgsForCall(0)
286+
Expect(metric).ToNot(Equal("errors"))
287+
288+
Expect(fakeStatter.TimingDurationCallCount()).To(Equal(1))
289+
})
290+
})
291+
292+
Context("when statusCode stats are turned off", func() {
293+
It("should not call Inc or TimingDuration", func() {
294+
ryeConfig := Config{
295+
Statter: fakeStatter,
296+
NoStatusCodeStats: true,
297+
}
298+
299+
handler := NewMWHandler(ryeConfig)
300+
//use the failureHandler so an error would be reported
301+
h := handler.Handle([]Handler{failureHandler})
302+
h.ServeHTTP(response, request)
303+
304+
time.Sleep(time.Millisecond * 10)
305+
306+
Expect(fakeStatter.IncCallCount()).To(Equal(1))
307+
metric, _, _ := fakeStatter.IncArgsForCall(0)
308+
Expect(metric).ToNot(ContainSubstring("handlers."))
309+
310+
Expect(fakeStatter.TimingDurationCallCount()).To(Equal(1))
311+
})
312+
})
313+
314+
Context("when timming stats are turned off", func() {
315+
It("should not call Inc or TimingDuration", func() {
316+
ryeConfig := Config{
317+
Statter: fakeStatter,
318+
NoDurationStats: true,
319+
}
320+
321+
handler := NewMWHandler(ryeConfig)
322+
//use the failureHandler so an error would be reported
323+
h := handler.Handle([]Handler{failureHandler})
324+
h.ServeHTTP(response, request)
325+
326+
time.Sleep(time.Millisecond * 10)
327+
328+
Expect(fakeStatter.TimingDurationCallCount()).To(Equal(0))
329+
Expect(fakeStatter.IncCallCount()).To(Equal(2))
330+
})
331+
})
270332
})
271333

272334
Describe("getFuncName", func() {

0 commit comments

Comments
 (0)