observer.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2017 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. // Observer is the interface that wraps the Observe method, which is used by
  15. // Histogram and Summary to add observations.
  16. type Observer interface {
  17. Observe(float64)
  18. }
  19. // The ObserverFunc type is an adapter to allow the use of ordinary
  20. // functions as Observers. If f is a function with the appropriate
  21. // signature, ObserverFunc(f) is an Observer that calls f.
  22. //
  23. // This adapter is usually used in connection with the Timer type, and there are
  24. // two general use cases:
  25. //
  26. // The most common one is to use a Gauge as the Observer for a Timer.
  27. // See the "Gauge" Timer example.
  28. //
  29. // The more advanced use case is to create a function that dynamically decides
  30. // which Observer to use for observing the duration. See the "Complex" Timer
  31. // example.
  32. type ObserverFunc func(float64)
  33. // Observe calls f(value). It implements Observer.
  34. func (f ObserverFunc) Observe(value float64) {
  35. f(value)
  36. }
  37. // ObserverVec is an interface implemented by `HistogramVec` and `SummaryVec`.
  38. type ObserverVec interface {
  39. GetMetricWith(Labels) (Observer, error)
  40. GetMetricWithLabelValues(lvs ...string) (Observer, error)
  41. With(Labels) Observer
  42. WithLabelValues(...string) Observer
  43. Collector
  44. }