absolute.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package event
  2. import "fmt"
  3. // Absolute is a metric that is not averaged/aggregated.
  4. // We keep each value distinct and then we flush them all individually.
  5. type Absolute struct {
  6. Name string
  7. Values []int64
  8. }
  9. // Update the event with metrics coming from a new one of the same type and with the same key
  10. func (e *Absolute) Update(e2 Event) error {
  11. if e.Type() != e2.Type() {
  12. return fmt.Errorf("statsd event type conflict: %s vs %s ", e.String(), e2.String())
  13. }
  14. e.Values = append(e.Values, e2.Payload().([]int64)...)
  15. return nil
  16. }
  17. // Payload returns the aggregated value for this event
  18. func (e Absolute) Payload() interface{} {
  19. return e.Values
  20. }
  21. // Stats returns an array of StatsD events as they travel over UDP
  22. func (e Absolute) Stats() []string {
  23. ret := make([]string, 0, len(e.Values))
  24. for _, v := range e.Values {
  25. ret = append(ret, fmt.Sprintf("%s:%d|a", e.Name, v))
  26. }
  27. return ret
  28. }
  29. // Key returns the name of this metric
  30. func (e Absolute) Key() string {
  31. return e.Name
  32. }
  33. // SetKey sets the name of this metric
  34. func (e *Absolute) SetKey(key string) {
  35. e.Name = key
  36. }
  37. // Type returns an integer identifier for this type of metric
  38. func (e Absolute) Type() int {
  39. return EventAbsolute
  40. }
  41. // TypeString returns a name for this type of metric
  42. func (e Absolute) TypeString() string {
  43. return "Absolute"
  44. }
  45. // String returns a debug-friendly representation of this metric
  46. func (e Absolute) String() string {
  47. return fmt.Sprintf("{Type: %s, Key: %s, Values: %v}", e.TypeString(), e.Name, e.Values)
  48. }