maxovertime.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package aggregator
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // maxOverTimeAggregator is a MetricAggregator which returns the max value passed to it through the Update function
  7. type maxOverTimeAggregator struct {
  8. lock sync.Mutex
  9. name string
  10. labelValues []string
  11. max float64
  12. }
  13. func MaxOverTime(name string, labelValues []string) MetricAggregator {
  14. return &maxOverTimeAggregator{
  15. name: name,
  16. labelValues: labelValues,
  17. }
  18. }
  19. func (a *maxOverTimeAggregator) Name() string {
  20. return a.name
  21. }
  22. func (a *maxOverTimeAggregator) AdditionInfo() map[string]string {
  23. return nil
  24. }
  25. func (a *maxOverTimeAggregator) LabelValues() []string {
  26. return a.labelValues
  27. }
  28. func (a *maxOverTimeAggregator) Update(value float64, timestamp time.Time, additionalInfo map[string]string) {
  29. a.lock.Lock()
  30. defer a.lock.Unlock()
  31. if value > a.max {
  32. a.max = value
  33. }
  34. }
  35. func (a *maxOverTimeAggregator) Value() []MetricValue {
  36. a.lock.Lock()
  37. defer a.lock.Unlock()
  38. if a.max == 0 {
  39. return []MetricValue{}
  40. }
  41. return []MetricValue{
  42. {Value: a.max},
  43. }
  44. }