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