-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
150 lines (117 loc) · 2.8 KB
/
main.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
package main
import (
"cwlogsexample/datasource"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/google/uuid"
"log"
"sync"
"time"
)
var (
cwl *cloudwatchlogs.CloudWatchLogs
logGroupName = "youtubeTest"
logStreamName = ""
sequenceToken = ""
)
func init() {
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String("eu-west-2"), // london
},
})
if err != nil {
panic(err)
}
cwl = cloudwatchlogs.New(sess)
err = ensureLogGroupExists(logGroupName)
if err != nil {
panic(err)
}
}
func main() {
queue := []string{}
lock := sync.Mutex{}
go datasource.GenerateData(&queue, &lock)
go processQueue(&queue, &lock)
// to stop the code from exiting
wg := sync.WaitGroup{}
wg.Add(1)
wg.Wait()
}
// ensureLogGroupExists first checks if the log group exists,
// if it doesn't it will create one.
func ensureLogGroupExists(name string) error {
resp, err := cwl.DescribeLogGroups(&cloudwatchlogs.DescribeLogGroupsInput{})
if err != nil {
return err
}
for _, logGroup := range resp.LogGroups {
if *logGroup.LogGroupName == name {
return nil
}
}
_, err = cwl.CreateLogGroup(&cloudwatchlogs.CreateLogGroupInput{
LogGroupName: &name,
})
if err != nil {
return err
}
_, err = cwl.PutRetentionPolicy(&cloudwatchlogs.PutRetentionPolicyInput{
RetentionInDays: aws.Int64(14),
LogGroupName: &name,
})
return err
}
// createLogStream will make a new logStream with a random uuid as its name.
func createLogStream() error {
name := uuid.New().String()
_, err := cwl.CreateLogStream(&cloudwatchlogs.CreateLogStreamInput{
LogGroupName: &logGroupName,
LogStreamName: &name,
})
logStreamName = name
return err
}
// processQueue will process the log queue
func processQueue(queue *[]string, lock *sync.Mutex) error {
var logQueue []*cloudwatchlogs.InputLogEvent
for {
lock.Lock()
if len(*queue) > 0 {
for _, item := range *queue {
logQueue = append(logQueue, &cloudwatchlogs.InputLogEvent{
Message: &item,
Timestamp: aws.Int64(time.Now().UnixNano() / int64(time.Millisecond)),
})
}
*queue = []string{}
}
lock.Unlock()
if len(logQueue) > 0 {
input := cloudwatchlogs.PutLogEventsInput{
LogEvents: logQueue,
LogGroupName: &logGroupName,
}
if sequenceToken == "" {
err := createLogStream()
if err != nil {
panic(err)
}
} else {
input = *input.SetSequenceToken(sequenceToken)
}
input = *input.SetLogStreamName(logStreamName)
resp, err := cwl.PutLogEvents(&input)
if err != nil {
log.Println(err)
}
if resp != nil {
sequenceToken = *resp.NextSequenceToken
}
logQueue = []*cloudwatchlogs.InputLogEvent{}
}
time.Sleep(time.Second * 5)
}
}