...

Source file src/github.com/docker/go-metrics/timer.go

Documentation: github.com/docker/go-metrics

     1  package metrics
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  )
     8  
     9  // StartTimer begins a timer observation at the callsite. When the target
    10  // operation is completed, the caller should call the return done func().
    11  func StartTimer(timer Timer) (done func()) {
    12  	start := time.Now()
    13  	return func() {
    14  		timer.Update(time.Since(start))
    15  	}
    16  }
    17  
    18  // Timer is a metric that allows collecting the duration of an action in seconds
    19  type Timer interface {
    20  	// Update records an observation, duration, and converts to the target
    21  	// units.
    22  	Update(duration time.Duration)
    23  
    24  	// UpdateSince will add the duration from the provided starting time to the
    25  	// timer's summary with the precisions that was used in creation of the timer
    26  	UpdateSince(time.Time)
    27  }
    28  
    29  // LabeledTimer is a timer that must have label values populated before use.
    30  type LabeledTimer interface {
    31  	WithValues(labels ...string) *labeledTimerObserver
    32  }
    33  
    34  type labeledTimer struct {
    35  	m *prometheus.HistogramVec
    36  }
    37  
    38  type labeledTimerObserver struct {
    39  	m prometheus.Observer
    40  }
    41  
    42  func (lbo *labeledTimerObserver) Update(duration time.Duration) {
    43  	lbo.m.Observe(duration.Seconds())
    44  }
    45  
    46  func (lbo *labeledTimerObserver) UpdateSince(since time.Time) {
    47  	lbo.m.Observe(time.Since(since).Seconds())
    48  }
    49  
    50  func (lt *labeledTimer) WithValues(labels ...string) *labeledTimerObserver {
    51  	return &labeledTimerObserver{m: lt.m.WithLabelValues(labels...)}
    52  }
    53  
    54  func (lt *labeledTimer) Describe(c chan<- *prometheus.Desc) {
    55  	lt.m.Describe(c)
    56  }
    57  
    58  func (lt *labeledTimer) Collect(c chan<- prometheus.Metric) {
    59  	lt.m.Collect(c)
    60  }
    61  
    62  type timer struct {
    63  	m prometheus.Observer
    64  }
    65  
    66  func (t *timer) Update(duration time.Duration) {
    67  	t.m.Observe(duration.Seconds())
    68  }
    69  
    70  func (t *timer) UpdateSince(since time.Time) {
    71  	t.m.Observe(time.Since(since).Seconds())
    72  }
    73  
    74  func (t *timer) Describe(c chan<- *prometheus.Desc) {
    75  	c <- t.m.(prometheus.Metric).Desc()
    76  }
    77  
    78  func (t *timer) Collect(c chan<- prometheus.Metric) {
    79  	// Are there any observers that don't implement Collector? It is really
    80  	// unclear what the point of the upstream change was, but we'll let this
    81  	// panic if we get an observer that doesn't implement collector. In this
    82  	// case, we should almost always see metricVec objects, so this should
    83  	// never panic.
    84  	t.m.(prometheus.Collector).Collect(c)
    85  }
    86  

View as plain text