/
notifications.go
162 lines (135 loc) · 4.54 KB
/
notifications.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package core
import (
"errors"
"log"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
var notificationChannel *chan string
var once sync.Once
// GetNotificationChannel - returns notification channel to publish messages to Cloudwatch
func GetNotificationChannel(currentSession *session.Session) *chan string {
once.Do(func() {
notificationChannel, _ = initialiseCloudwatchLogging(currentSession)
})
return notificationChannel
}
func getLogGroup(service *cloudwatchlogs.CloudWatchLogs, logGroupName *string) (*cloudwatchlogs.LogGroup, error) {
search := &cloudwatchlogs.DescribeLogGroupsInput{
LogGroupNamePrefix: logGroupName,
}
describeLogGroupsOutput, err := service.DescribeLogGroups(search)
if err != nil {
return nil, errors.New("Can't get cloudwatch log groups")
}
if len(describeLogGroupsOutput.LogGroups) != 0 {
return describeLogGroupsOutput.LogGroups[0], nil
}
return nil, nil
}
func createLogGroup(service *cloudwatchlogs.CloudWatchLogs) (*string, error) {
logGroupName := "capuchin-log-group"
logGroupRole := "capuchin"
logGroupTags := map[string]*string{
"Role": &logGroupRole,
}
logGroup, err := getLogGroup(service, &logGroupName)
if err != nil {
return nil, err
}
if logGroup != nil {
return logGroup.LogGroupName, nil
}
log.Printf("Creating new Cloudwatch log group: %s", logGroupName)
input := &cloudwatchlogs.CreateLogGroupInput{
LogGroupName: &logGroupName,
Tags: logGroupTags,
}
_, err = service.CreateLogGroup(input)
if err != nil {
return nil, errors.New("Can't create cloudwatch log group")
}
return &logGroupName, nil
}
func getLogStream(service *cloudwatchlogs.CloudWatchLogs, logGroupName *string, logStreamName *string) (*cloudwatchlogs.LogStream, error) {
search := &cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: logGroupName,
LogStreamNamePrefix: logStreamName,
}
describeLogStreamsOutput, err := service.DescribeLogStreams(search)
if err != nil {
return nil, errors.New("Can't get cloudwatch log streams")
}
if len(describeLogStreamsOutput.LogStreams) != 0 {
return describeLogStreamsOutput.LogStreams[0], nil
}
return nil, nil
}
func createLogStream(service *cloudwatchlogs.CloudWatchLogs, logGroupName *string) (*string, error) {
logStreamName := "capuchin-log-stream"
logStream, err := getLogStream(service, logGroupName, &logStreamName)
if err != nil {
return nil, err
}
if logStream != nil {
return logStream.LogStreamName, nil
}
log.Printf("Creating new Cloudwatch log stream: %s", logStreamName)
input := &cloudwatchlogs.CreateLogStreamInput{
LogGroupName: logGroupName,
LogStreamName: &logStreamName,
}
_, err = service.CreateLogStream(input)
if err != nil {
return nil, errors.New("Can't create cloudwatch log stream")
}
return &logStreamName, nil
}
func sendCloudwatchEvent(service *cloudwatchlogs.CloudWatchLogs, logGroupName *string, logStreamName *string, message *string) {
logStream, err := getLogStream(service, logGroupName, logStreamName)
if err != nil {
log.Printf("Error retrieving Cloudwatch log stream: %s", err)
return
}
currentTimestamp := int64(time.Now().UnixNano() / int64(time.Millisecond))
logEventInput := &cloudwatchlogs.PutLogEventsInput{
LogGroupName: logGroupName,
LogStreamName: logStreamName,
SequenceToken: logStream.UploadSequenceToken,
LogEvents: []*cloudwatchlogs.InputLogEvent{
{
Message: message,
Timestamp: ¤tTimestamp,
},
},
}
_, err = service.PutLogEvents(logEventInput)
if err != nil {
log.Printf("Error publishing event to Cloudwatch: %s", err)
}
}
func listenForChannel(service *cloudwatchlogs.CloudWatchLogs, logGroupName *string, logStreamName *string, notificationChannel chan string) {
for message := range notificationChannel {
log.Println(message)
sendCloudwatchEvent(service, logGroupName, logStreamName, &message)
}
}
func initialiseCloudwatchLogging(currentSession *session.Session) (*chan string, error) {
service := cloudwatchlogs.New(currentSession)
logGroupName, err := createLogGroup(service)
if err != nil {
log.Printf("Can't create notification channel %s", err)
return nil, err
}
logStreamName, err := createLogStream(service, logGroupName)
if err != nil {
log.Printf("Can't create notification channel %s", err)
return nil, err
}
log.Printf("Using Cloudwatch log group and stream: %s -> %s", *logGroupName, *logStreamName)
notificationChannel := make(chan string)
go listenForChannel(service, logGroupName, logStreamName, notificationChannel)
return ¬ificationChannel, nil
}