event.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package statsd
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // Events support
  7. // EventAlertType and EventAlertPriority became exported types after this issue was submitted: https://github.com/DataDog/datadog-go/issues/41
  8. // The reason why they got exported is so that client code can directly use the types.
  9. // EventAlertType is the alert type for events
  10. type EventAlertType string
  11. const (
  12. // Info is the "info" AlertType for events
  13. Info EventAlertType = "info"
  14. // Error is the "error" AlertType for events
  15. Error EventAlertType = "error"
  16. // Warning is the "warning" AlertType for events
  17. Warning EventAlertType = "warning"
  18. // Success is the "success" AlertType for events
  19. Success EventAlertType = "success"
  20. )
  21. // EventPriority is the event priority for events
  22. type EventPriority string
  23. const (
  24. // Normal is the "normal" Priority for events
  25. Normal EventPriority = "normal"
  26. // Low is the "low" Priority for events
  27. Low EventPriority = "low"
  28. )
  29. // An Event is an object that can be posted to your DataDog event stream.
  30. type Event struct {
  31. // Title of the event. Required.
  32. Title string
  33. // Text is the description of the event. Required.
  34. Text string
  35. // Timestamp is a timestamp for the event. If not provided, the dogstatsd
  36. // server will set this to the current time.
  37. Timestamp time.Time
  38. // Hostname for the event.
  39. Hostname string
  40. // AggregationKey groups this event with others of the same key.
  41. AggregationKey string
  42. // Priority of the event. Can be statsd.Low or statsd.Normal.
  43. Priority EventPriority
  44. // SourceTypeName is a source type for the event.
  45. SourceTypeName string
  46. // AlertType can be statsd.Info, statsd.Error, statsd.Warning, or statsd.Success.
  47. // If absent, the default value applied by the dogstatsd server is Info.
  48. AlertType EventAlertType
  49. // Tags for the event.
  50. Tags []string
  51. }
  52. // NewEvent creates a new event with the given title and text. Error checking
  53. // against these values is done at send-time, or upon running e.Check.
  54. func NewEvent(title, text string) *Event {
  55. return &Event{
  56. Title: title,
  57. Text: text,
  58. }
  59. }
  60. // Check verifies that an event is valid.
  61. func (e Event) Check() error {
  62. if len(e.Title) == 0 {
  63. return fmt.Errorf("statsd.Event title is required")
  64. }
  65. if len(e.Text) == 0 {
  66. return fmt.Errorf("statsd.Event text is required")
  67. }
  68. return nil
  69. }
  70. // Encode returns the dogstatsd wire protocol representation for an event.
  71. // Tags may be passed which will be added to the encoded output but not to
  72. // the Event's list of tags, eg. for default tags.
  73. func (e Event) Encode(tags ...string) (string, error) {
  74. err := e.Check()
  75. if err != nil {
  76. return "", err
  77. }
  78. var buffer []byte
  79. buffer = appendEvent(buffer, e, tags)
  80. return string(buffer), nil
  81. }