gauge.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package event
  2. import "fmt"
  3. // Gauge - Gauges are a constant data type. They are not subject to averaging,
  4. // and they don’t change unless you change them. That is, once you set a gauge value,
  5. // it will be a flat line on the graph until you change it again
  6. type Gauge struct {
  7. Name string
  8. Value int64
  9. }
  10. // Update the event with metrics coming from a new one of the same type and with the same key
  11. func (e *Gauge) Update(e2 Event) error {
  12. if e.Type() != e2.Type() {
  13. return fmt.Errorf("statsd event type conflict: %s vs %s ", e.String(), e2.String())
  14. }
  15. e.Value = e2.Payload().(int64)
  16. return nil
  17. }
  18. // Payload returns the aggregated value for this event
  19. func (e Gauge) Payload() interface{} {
  20. return e.Value
  21. }
  22. // Stats returns an array of StatsD events as they travel over UDP
  23. func (e Gauge) Stats() []string {
  24. if e.Value < 0 {
  25. // because a leading '+' or '-' in the value of a gauge denotes a delta, to send
  26. // a negative gauge value we first set the gauge absolutely to 0, then send the
  27. // negative value as a delta from 0 (that's just how the spec works :-)
  28. return []string{
  29. fmt.Sprintf("%s:%d|g", e.Name, 0),
  30. fmt.Sprintf("%s:%d|g", e.Name, e.Value),
  31. }
  32. }
  33. return []string{fmt.Sprintf("%s:%d|g", e.Name, e.Value)}
  34. }
  35. // Key returns the name of this metric
  36. func (e Gauge) Key() string {
  37. return e.Name
  38. }
  39. // SetKey sets the name of this metric
  40. func (e *Gauge) SetKey(key string) {
  41. e.Name = key
  42. }
  43. // Type returns an integer identifier for this type of metric
  44. func (e Gauge) Type() int {
  45. return EventGauge
  46. }
  47. // TypeString returns a name for this type of metric
  48. func (e Gauge) TypeString() string {
  49. return "Gauge"
  50. }
  51. // String returns a debug-friendly representation of this metric
  52. func (e Gauge) String() string {
  53. return fmt.Sprintf("{Type: %s, Key: %s, Value: %d}", e.TypeString(), e.Name, e.Value)
  54. }