uptime.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package aggregator
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // uptimeAggregator is a MetricAggregator which records the first and last timestamp of updates called on it
  7. type uptimeAggregator struct {
  8. lock sync.Mutex
  9. labelValues []string
  10. start *time.Time
  11. end *time.Time
  12. }
  13. func Uptime(labelValues []string) MetricAggregator {
  14. return &uptimeAggregator{
  15. labelValues: labelValues,
  16. }
  17. }
  18. func (a *uptimeAggregator) AdditionInfo() map[string]string {
  19. return nil
  20. }
  21. func (a *uptimeAggregator) LabelValues() []string {
  22. return a.labelValues
  23. }
  24. func (a *uptimeAggregator) Update(value float64, timestamp time.Time, additionalInfo map[string]string) {
  25. a.lock.Lock()
  26. defer a.lock.Unlock()
  27. if a.start == nil {
  28. a.start = &timestamp
  29. }
  30. if !timestamp.Equal(*a.start) {
  31. a.end = &timestamp
  32. }
  33. }
  34. func (a *uptimeAggregator) Value() []MetricValue {
  35. a.lock.Lock()
  36. defer a.lock.Unlock()
  37. metricValues := make([]MetricValue, 0)
  38. if a.start != nil {
  39. metricValues = append(metricValues, MetricValue{Value: 1, Timestamp: a.start})
  40. }
  41. if a.end != nil {
  42. metricValues = append(metricValues, MetricValue{Value: 1, Timestamp: a.end})
  43. }
  44. return metricValues
  45. }