/
event.go
73 lines (61 loc) · 1.65 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package aws
import (
"encoding/json"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/convox/rack/pkg/structs"
)
// EventSend publishes an important message out to the world.
//
// On AWS messages are published to SNS. The Rack has an HTTP endpoint that is an SNS
// subscription, and when a message is delivered forwards them to all configured
// webhook services.
//
// Often the Rack has a Console webhook which facilitates forwarding events
// to Slack with additional formatting and filtering.
//
// Because these are important system events, they are also published to Segment
// for operational metrics.
type event struct {
Action string `json:"action"`
Data map[string]string `json:"data"`
Status string `json:"status"`
Timestamp time.Time `json:"timestamp"`
}
func (p *Provider) EventSend(action string, opts structs.EventSendOptions) error {
e := event{
Action: action,
Data: opts.Data,
Status: cs(opts.Status, "success"),
Timestamp: time.Now().UTC(),
}
if e.Data["timestamp"] != "" {
t, err := time.Parse(time.RFC3339, e.Data["timestamp"])
if err == nil {
e.Timestamp = t
}
}
if opts.Error != nil {
e.Status = "error"
e.Data["message"] = *opts.Error
}
e.Data["rack"] = p.Rack
if p.IsTest() {
e.Timestamp = time.Time{}
}
msg, err := json.Marshal(e)
if err != nil {
return err
}
// Publish Event to SNS
_, err = p.sns().Publish(&sns.PublishInput{
Message: aws.String(string(msg)), // Required
Subject: aws.String(e.Action),
TargetArn: aws.String(p.NotificationTopic),
})
if err != nil {
return err
}
return nil
}