You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

82 lines
2.5 KiB

  1. // Copyright 2016 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package prometheus
  14. import "time"
  15. // Timer is a helper type to time functions. Use NewTimer to create new
  16. // instances.
  17. type Timer struct {
  18. begin time.Time
  19. observer Observer
  20. }
  21. // NewTimer creates a new Timer. The provided Observer is used to observe a
  22. // duration in seconds. If the Observer implements ExemplarObserver, passing exemplar
  23. // later on will be also supported.
  24. // Timer is usually used to time a function call in the
  25. // following way:
  26. //
  27. // func TimeMe() {
  28. // timer := NewTimer(myHistogram)
  29. // defer timer.ObserveDuration()
  30. // // Do actual work.
  31. // }
  32. //
  33. // or
  34. //
  35. // func TimeMeWithExemplar() {
  36. // timer := NewTimer(myHistogram)
  37. // defer timer.ObserveDurationWithExemplar(exemplar)
  38. // // Do actual work.
  39. // }
  40. func NewTimer(o Observer) *Timer {
  41. return &Timer{
  42. begin: time.Now(),
  43. observer: o,
  44. }
  45. }
  46. // ObserveDuration records the duration passed since the Timer was created with
  47. // NewTimer. It calls the Observe method of the Observer provided during
  48. // construction with the duration in seconds as an argument. The observed
  49. // duration is also returned. ObserveDuration is usually called with a defer
  50. // statement.
  51. //
  52. // Note that this method is only guaranteed to never observe negative durations
  53. // if used with Go1.9+.
  54. func (t *Timer) ObserveDuration() time.Duration {
  55. d := time.Since(t.begin)
  56. if t.observer != nil {
  57. t.observer.Observe(d.Seconds())
  58. }
  59. return d
  60. }
  61. // ObserveDurationWithExemplar is like ObserveDuration, but it will also
  62. // observe exemplar with the duration unless exemplar is nil or provided Observer can't
  63. // be casted to ExemplarObserver.
  64. func (t *Timer) ObserveDurationWithExemplar(exemplar Labels) time.Duration {
  65. d := time.Since(t.begin)
  66. eo, ok := t.observer.(ExemplarObserver)
  67. if ok && exemplar != nil {
  68. eo.ObserveWithExemplar(d.Seconds(), exemplar)
  69. return d
  70. }
  71. if t.observer != nil {
  72. t.observer.Observe(d.Seconds())
  73. }
  74. return d
  75. }