Merge pull request #13 from prometheus/refactor/remove-timer
Remove timer.go and timer_test.go
This commit is contained in:
commit
c39c592874
|
@ -18,6 +18,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -269,7 +270,8 @@ func (registry registry) YieldExporter() http.HandlerFunc {
|
||||||
|
|
||||||
func (registry registry) Handler() http.HandlerFunc {
|
func (registry registry) Handler() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var instrumentable InstrumentableCall = func() {
|
defer requestLatencyAccumulator(time.Now())
|
||||||
|
|
||||||
requestCount.Increment(nil)
|
requestCount.Increment(nil)
|
||||||
url := r.URL
|
url := r.URL
|
||||||
|
|
||||||
|
@ -289,9 +291,6 @@ func (registry registry) Handler() http.HandlerFunc {
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
InstrumentCall(instrumentable, requestLatencyAccumulator)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,8 +40,8 @@ func init() {
|
||||||
|
|
||||||
// This callback accumulates the microsecond duration of the reporting
|
// This callback accumulates the microsecond duration of the reporting
|
||||||
// framework's overhead such that it can be reported.
|
// framework's overhead such that it can be reported.
|
||||||
var requestLatencyAccumulator CompletionCallback = func(duration time.Duration) {
|
var requestLatencyAccumulator = func(began time.Time) {
|
||||||
microseconds := float64(duration / time.Microsecond)
|
microseconds := float64(time.Since(began) / time.Microsecond)
|
||||||
|
|
||||||
requestLatency.Add(nil, microseconds)
|
requestLatency.Add(nil, microseconds)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,62 +0,0 @@
|
||||||
// Copyright (c) 2013, Prometheus Team
|
|
||||||
// All rights reserved.
|
|
||||||
//
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package prometheus
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// This callback is called upon the completion of the timer—i.e., when it stops.
|
|
||||||
type CompletionCallback func(duration time.Duration)
|
|
||||||
|
|
||||||
// This is meant to capture a function that a StopWatch can call for purposes
|
|
||||||
// of instrumentation.
|
|
||||||
type InstrumentableCall func()
|
|
||||||
|
|
||||||
// StopWatch is the structure that captures instrumentation for durations.
|
|
||||||
|
|
||||||
// N.B.(mtp): A major limitation hereof is that the StopWatch protocol cannot
|
|
||||||
// retain instrumentation if a panic percolates within the context that is
|
|
||||||
// being measured.
|
|
||||||
type StopWatch interface {
|
|
||||||
Stop() time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
type stopWatch struct {
|
|
||||||
endTime time.Time
|
|
||||||
onCompletion CompletionCallback
|
|
||||||
startTime time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return a new StopWatch that is ready for instrumentation.
|
|
||||||
func Start(onCompletion CompletionCallback) StopWatch {
|
|
||||||
return &stopWatch{
|
|
||||||
onCompletion: onCompletion,
|
|
||||||
startTime: time.Now(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop the StopWatch returning the elapsed duration of its lifetime while
|
|
||||||
// firing an optional CompletionCallback in the background.
|
|
||||||
func (s *stopWatch) Stop() time.Duration {
|
|
||||||
s.endTime = time.Now()
|
|
||||||
duration := s.endTime.Sub(s.startTime)
|
|
||||||
|
|
||||||
if s.onCompletion != nil {
|
|
||||||
go s.onCompletion(duration)
|
|
||||||
}
|
|
||||||
|
|
||||||
return duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// Provide a quick way of instrumenting a InstrumentableCall and emitting its
|
|
||||||
// duration.
|
|
||||||
func InstrumentCall(instrumentable InstrumentableCall, onCompletion CompletionCallback) time.Duration {
|
|
||||||
s := Start(onCompletion)
|
|
||||||
instrumentable()
|
|
||||||
return s.Stop()
|
|
||||||
}
|
|
|
@ -1,69 +0,0 @@
|
||||||
// Copyright (c) 2013, Prometheus Team
|
|
||||||
// All rights reserved.
|
|
||||||
//
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package prometheus
|
|
||||||
|
|
||||||
import (
|
|
||||||
. "github.com/matttproud/gocheck"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *S) TestTimerStart(c *C) {
|
|
||||||
stopWatch, ok := Start(nil).(*stopWatch)
|
|
||||||
if !ok {
|
|
||||||
c.Check(ok, Equals, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Assert(stopWatch, Not(IsNil))
|
|
||||||
c.Assert(stopWatch.startTime, Not(IsNil))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *S) TestTimerStop(c *C) {
|
|
||||||
done := make(chan bool)
|
|
||||||
|
|
||||||
var callbackInvoked bool = false
|
|
||||||
var complete CompletionCallback = func(duration time.Duration) {
|
|
||||||
callbackInvoked = true
|
|
||||||
done <- true
|
|
||||||
}
|
|
||||||
|
|
||||||
stopWatch := Start(complete)
|
|
||||||
|
|
||||||
c.Check(callbackInvoked, Equals, false)
|
|
||||||
|
|
||||||
d := stopWatch.Stop()
|
|
||||||
|
|
||||||
<-done
|
|
||||||
|
|
||||||
c.Assert(d, Not(IsNil))
|
|
||||||
c.Check(callbackInvoked, Equals, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *S) TestInstrumentCall(c *C) {
|
|
||||||
var callbackInvoked bool = false
|
|
||||||
var instrumentableInvoked bool = false
|
|
||||||
done := make(chan bool, 2)
|
|
||||||
|
|
||||||
var complete CompletionCallback = func(duration time.Duration) {
|
|
||||||
callbackInvoked = true
|
|
||||||
done <- true
|
|
||||||
}
|
|
||||||
|
|
||||||
var instrumentable InstrumentableCall = func() {
|
|
||||||
instrumentableInvoked = true
|
|
||||||
done <- true
|
|
||||||
}
|
|
||||||
|
|
||||||
d := InstrumentCall(instrumentable, complete)
|
|
||||||
|
|
||||||
c.Assert(d, Not(IsNil))
|
|
||||||
|
|
||||||
<-done
|
|
||||||
<-done
|
|
||||||
|
|
||||||
c.Check(instrumentableInvoked, Equals, true)
|
|
||||||
c.Check(callbackInvoked, Equals, true)
|
|
||||||
}
|
|
Loading…
Reference in New Issue