timer.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // includes code from
  2. // https://raw.githubusercontent.com/rcrowley/go-metrics/master/sample.go
  3. // Copyright 2012 Richard Crowley. All rights reserved.
  4. package metrics
  5. import (
  6. "sync"
  7. "time"
  8. "github.com/prometheus/client_golang/prometheus"
  9. )
  10. // Timers capture the duration and rate of events.
  11. type Timer interface {
  12. Metric
  13. Count() int64
  14. Max() int64
  15. Mean() float64
  16. Min() int64
  17. Percentile(float64) float64
  18. Percentiles([]float64) []float64
  19. Rate1() float64
  20. Rate5() float64
  21. Rate15() float64
  22. RateMean() float64
  23. StdDev() float64
  24. Sum() int64
  25. Time(func())
  26. Update(time.Duration)
  27. UpdateSince(time.Time)
  28. Variance() float64
  29. }
  30. // NewTimer constructs a new StandardTimer using an exponentially-decaying
  31. // sample with the same reservoir size and alpha as UNIX load averages.
  32. func NewTimer(meta *MetricMeta) Timer {
  33. promSummary := prometheus.NewSummary(prometheus.SummaryOpts{
  34. Name: promifyName(meta.Name()),
  35. Help: meta.Name(),
  36. ConstLabels: prometheus.Labels(meta.GetTagsCopy()),
  37. })
  38. prometheus.MustRegister(promSummary)
  39. return &StandardTimer{
  40. MetricMeta: meta,
  41. histogram: NewHistogram(meta, NewExpDecaySample(1028, 0.015)),
  42. meter: NewMeter(meta),
  43. Summary: promSummary,
  44. }
  45. }
  46. func RegTimer(name string, tagStrings ...string) Timer {
  47. tr := NewTimer(NewMetricMeta(name, tagStrings))
  48. MetricStats.Register(tr)
  49. return tr
  50. }
  51. // NilTimer is a no-op Timer.
  52. type NilTimer struct {
  53. *MetricMeta
  54. h Histogram
  55. m Meter
  56. }
  57. // Count is a no-op.
  58. func (NilTimer) Count() int64 { return 0 }
  59. // Max is a no-op.
  60. func (NilTimer) Max() int64 { return 0 }
  61. // Mean is a no-op.
  62. func (NilTimer) Mean() float64 { return 0.0 }
  63. // Min is a no-op.
  64. func (NilTimer) Min() int64 { return 0 }
  65. // Percentile is a no-op.
  66. func (NilTimer) Percentile(p float64) float64 { return 0.0 }
  67. // Percentiles is a no-op.
  68. func (NilTimer) Percentiles(ps []float64) []float64 {
  69. return make([]float64, len(ps))
  70. }
  71. // Rate1 is a no-op.
  72. func (NilTimer) Rate1() float64 { return 0.0 }
  73. // Rate5 is a no-op.
  74. func (NilTimer) Rate5() float64 { return 0.0 }
  75. // Rate15 is a no-op.
  76. func (NilTimer) Rate15() float64 { return 0.0 }
  77. // RateMean is a no-op.
  78. func (NilTimer) RateMean() float64 { return 0.0 }
  79. // Snapshot is a no-op.
  80. func (n NilTimer) Snapshot() Metric { return n }
  81. // StdDev is a no-op.
  82. func (NilTimer) StdDev() float64 { return 0.0 }
  83. // Sum is a no-op.
  84. func (NilTimer) Sum() int64 { return 0 }
  85. // Time is a no-op.
  86. func (NilTimer) Time(func()) {}
  87. // Update is a no-op.
  88. func (NilTimer) Update(time.Duration) {}
  89. // UpdateSince is a no-op.
  90. func (NilTimer) UpdateSince(time.Time) {}
  91. // Variance is a no-op.
  92. func (NilTimer) Variance() float64 { return 0.0 }
  93. // StandardTimer is the standard implementation of a Timer and uses a Histogram
  94. // and Meter.
  95. type StandardTimer struct {
  96. *MetricMeta
  97. histogram Histogram
  98. meter Meter
  99. mutex sync.Mutex
  100. prometheus.Summary
  101. }
  102. // Count returns the number of events recorded.
  103. func (t *StandardTimer) Count() int64 {
  104. return t.histogram.Count()
  105. }
  106. // Max returns the maximum value in the sample.
  107. func (t *StandardTimer) Max() int64 {
  108. return t.histogram.Max()
  109. }
  110. // Mean returns the mean of the values in the sample.
  111. func (t *StandardTimer) Mean() float64 {
  112. return t.histogram.Mean()
  113. }
  114. // Min returns the minimum value in the sample.
  115. func (t *StandardTimer) Min() int64 {
  116. return t.histogram.Min()
  117. }
  118. // Percentile returns an arbitrary percentile of the values in the sample.
  119. func (t *StandardTimer) Percentile(p float64) float64 {
  120. return t.histogram.Percentile(p)
  121. }
  122. // Percentiles returns a slice of arbitrary percentiles of the values in the
  123. // sample.
  124. func (t *StandardTimer) Percentiles(ps []float64) []float64 {
  125. return t.histogram.Percentiles(ps)
  126. }
  127. // Rate1 returns the one-minute moving average rate of events per second.
  128. func (t *StandardTimer) Rate1() float64 {
  129. return t.meter.Rate1()
  130. }
  131. // Rate5 returns the five-minute moving average rate of events per second.
  132. func (t *StandardTimer) Rate5() float64 {
  133. return t.meter.Rate5()
  134. }
  135. // Rate15 returns the fifteen-minute moving average rate of events per second.
  136. func (t *StandardTimer) Rate15() float64 {
  137. return t.meter.Rate15()
  138. }
  139. // RateMean returns the meter's mean rate of events per second.
  140. func (t *StandardTimer) RateMean() float64 {
  141. return t.meter.RateMean()
  142. }
  143. // Snapshot returns a read-only copy of the timer.
  144. func (t *StandardTimer) Snapshot() Metric {
  145. t.mutex.Lock()
  146. defer t.mutex.Unlock()
  147. return &TimerSnapshot{
  148. MetricMeta: t.MetricMeta,
  149. histogram: t.histogram.Snapshot().(*HistogramSnapshot),
  150. meter: t.meter.Snapshot().(*MeterSnapshot),
  151. }
  152. }
  153. // StdDev returns the standard deviation of the values in the sample.
  154. func (t *StandardTimer) StdDev() float64 {
  155. return t.histogram.StdDev()
  156. }
  157. // Sum returns the sum in the sample.
  158. func (t *StandardTimer) Sum() int64 {
  159. return t.histogram.Sum()
  160. }
  161. // Record the duration of the execution of the given function.
  162. func (t *StandardTimer) Time(f func()) {
  163. ts := time.Now()
  164. f()
  165. t.Update(time.Since(ts))
  166. }
  167. // Record the duration of an event.
  168. func (t *StandardTimer) Update(d time.Duration) {
  169. t.mutex.Lock()
  170. defer t.mutex.Unlock()
  171. t.histogram.Update(int64(d))
  172. t.meter.Mark(1)
  173. t.Summary.Observe(float64(d))
  174. }
  175. // Record the duration of an event that started at a time and ends now.
  176. func (t *StandardTimer) UpdateSince(ts time.Time) {
  177. t.mutex.Lock()
  178. defer t.mutex.Unlock()
  179. sinceMs := time.Since(ts) / time.Millisecond
  180. t.histogram.Update(int64(sinceMs))
  181. t.meter.Mark(1)
  182. t.Summary.Observe(float64(sinceMs))
  183. }
  184. // Variance returns the variance of the values in the sample.
  185. func (t *StandardTimer) Variance() float64 {
  186. return t.histogram.Variance()
  187. }
  188. // TimerSnapshot is a read-only copy of another Timer.
  189. type TimerSnapshot struct {
  190. *MetricMeta
  191. histogram *HistogramSnapshot
  192. meter *MeterSnapshot
  193. }
  194. // Count returns the number of events recorded at the time the snapshot was
  195. // taken.
  196. func (t *TimerSnapshot) Count() int64 { return t.histogram.Count() }
  197. // Max returns the maximum value at the time the snapshot was taken.
  198. func (t *TimerSnapshot) Max() int64 { return t.histogram.Max() }
  199. // Mean returns the mean value at the time the snapshot was taken.
  200. func (t *TimerSnapshot) Mean() float64 { return t.histogram.Mean() }
  201. // Min returns the minimum value at the time the snapshot was taken.
  202. func (t *TimerSnapshot) Min() int64 { return t.histogram.Min() }
  203. // Percentile returns an arbitrary percentile of sampled values at the time the
  204. // snapshot was taken.
  205. func (t *TimerSnapshot) Percentile(p float64) float64 {
  206. return t.histogram.Percentile(p)
  207. }
  208. // Percentiles returns a slice of arbitrary percentiles of sampled values at
  209. // the time the snapshot was taken.
  210. func (t *TimerSnapshot) Percentiles(ps []float64) []float64 {
  211. return t.histogram.Percentiles(ps)
  212. }
  213. // Rate1 returns the one-minute moving average rate of events per second at the
  214. // time the snapshot was taken.
  215. func (t *TimerSnapshot) Rate1() float64 { return t.meter.Rate1() }
  216. // Rate5 returns the five-minute moving average rate of events per second at
  217. // the time the snapshot was taken.
  218. func (t *TimerSnapshot) Rate5() float64 { return t.meter.Rate5() }
  219. // Rate15 returns the fifteen-minute moving average rate of events per second
  220. // at the time the snapshot was taken.
  221. func (t *TimerSnapshot) Rate15() float64 { return t.meter.Rate15() }
  222. // RateMean returns the meter's mean rate of events per second at the time the
  223. // snapshot was taken.
  224. func (t *TimerSnapshot) RateMean() float64 { return t.meter.RateMean() }
  225. // Snapshot returns the snapshot.
  226. func (t *TimerSnapshot) Snapshot() Metric { return t }
  227. // StdDev returns the standard deviation of the values at the time the snapshot
  228. // was taken.
  229. func (t *TimerSnapshot) StdDev() float64 { return t.histogram.StdDev() }
  230. // Sum returns the sum at the time the snapshot was taken.
  231. func (t *TimerSnapshot) Sum() int64 { return t.histogram.Sum() }
  232. // Time panics.
  233. func (*TimerSnapshot) Time(func()) {
  234. panic("Time called on a TimerSnapshot")
  235. }
  236. // Update panics.
  237. func (*TimerSnapshot) Update(time.Duration) {
  238. panic("Update called on a TimerSnapshot")
  239. }
  240. // UpdateSince panics.
  241. func (*TimerSnapshot) UpdateSince(time.Time) {
  242. panic("UpdateSince called on a TimerSnapshot")
  243. }
  244. // Variance returns the variance of the values at the time the snapshot was
  245. // taken.
  246. func (t *TimerSnapshot) Variance() float64 { return t.histogram.Variance() }