increment.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package event
  2. import "fmt"
  3. // Increment represents a metric whose value is averaged over a minute
  4. type Increment struct {
  5. Name string
  6. Value int64
  7. }
  8. // Update the event with metrics coming from a new one of the same type and with the same key
  9. func (e *Increment) Update(e2 Event) error {
  10. if e.Type() != e2.Type() {
  11. return fmt.Errorf("statsd event type conflict: %s vs %s ", e.String(), e2.String())
  12. }
  13. e.Value += e2.Payload().(int64)
  14. return nil
  15. }
  16. // Payload returns the aggregated value for this event
  17. func (e Increment) Payload() interface{} {
  18. return e.Value
  19. }
  20. // Stats returns an array of StatsD events as they travel over UDP
  21. func (e Increment) Stats() []string {
  22. return []string{fmt.Sprintf("%s:%d|c", e.Name, e.Value)}
  23. }
  24. // Key returns the name of this metric
  25. func (e Increment) Key() string {
  26. return e.Name
  27. }
  28. // SetKey sets the name of this metric
  29. func (e *Increment) SetKey(key string) {
  30. e.Name = key
  31. }
  32. // Type returns an integer identifier for this type of metric
  33. func (e Increment) Type() int {
  34. return EventIncr
  35. }
  36. // TypeString returns a name for this type of metric
  37. func (e Increment) TypeString() string {
  38. return "Increment"
  39. }
  40. // String returns a debug-friendly representation of this metric
  41. func (e Increment) String() string {
  42. return fmt.Sprintf("{Type: %s, Key: %s, Value: %d}", e.TypeString(), e.Name, e.Value)
  43. }