Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

55 linhas
1.7 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. Timer is usually used to time a function call in the
  23. // following way:
  24. // func TimeMe() {
  25. // timer := NewTimer(myHistogram)
  26. // defer timer.ObserveDuration()
  27. // // Do actual work.
  28. // }
  29. func NewTimer(o Observer) *Timer {
  30. return &Timer{
  31. begin: time.Now(),
  32. observer: o,
  33. }
  34. }
  35. // ObserveDuration records the duration passed since the Timer was created with
  36. // NewTimer. It calls the Observe method of the Observer provided during
  37. // construction with the duration in seconds as an argument. The observed
  38. // duration is also returned. ObserveDuration is usually called with a defer
  39. // statement.
  40. //
  41. // Note that this method is only guaranteed to never observe negative durations
  42. // if used with Go1.9+.
  43. func (t *Timer) ObserveDuration() time.Duration {
  44. d := time.Since(t.begin)
  45. if t.observer != nil {
  46. t.observer.Observe(d.Seconds())
  47. }
  48. return d
  49. }