increase.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package aggregator
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. type increaseAggregator struct {
  7. lock sync.Mutex
  8. labelValues []string
  9. initialized bool
  10. initialTime time.Time
  11. currentTime time.Time
  12. initial float64
  13. current float64
  14. }
  15. func Increase(labelValues []string) MetricAggregator {
  16. return &increaseAggregator{
  17. labelValues: labelValues,
  18. }
  19. }
  20. func (a *increaseAggregator) AdditionInfo() map[string]string {
  21. return nil
  22. }
  23. func (a *increaseAggregator) LabelValues() []string {
  24. return a.labelValues
  25. }
  26. func (a *increaseAggregator) Update(value float64, timestamp time.Time, additionalInfo map[string]string) {
  27. a.lock.Lock()
  28. defer a.lock.Unlock()
  29. if !a.initialized {
  30. a.initialTime = timestamp
  31. a.currentTime = timestamp
  32. a.initialized = true
  33. }
  34. if a.initialTime == timestamp {
  35. a.initial += value
  36. }
  37. if a.currentTime.Before(timestamp) {
  38. a.currentTime = timestamp
  39. a.current = 0
  40. }
  41. a.current += value
  42. }
  43. func (a *increaseAggregator) Value() []MetricValue {
  44. a.lock.Lock()
  45. defer a.lock.Unlock()
  46. return []MetricValue{
  47. {Value: a.current - a.initial},
  48. }
  49. }