-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
245 lines (214 loc) · 7.25 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/hashicorp/consul/api"
"gopkg.in/yaml.v2"
)
var (
InfoLogger *log.Logger
ErrorLogger *log.Logger
FatalLogger *log.Logger
)
type Config struct {
Consul ConsulConfig `yaml:"consul"`
AWSRegion string `yaml:"aws_region"`
AWSProfile string `yaml:"aws_profile"`
AWSRoleARN string `yaml:"aws_role_arn"`
AWSAccessKey string `yaml:"aws_access_key"`
AWSSecretKey string `yaml:"aws_secret_key"`
Services []ServiceConfig `yaml:"services"`
OffsetFallbackDuration time.Duration `yaml:"offset_fallback_duration"`
}
type ConsulConfig struct {
Address string `yaml:"address"`
Token string `yaml:"token"`
}
type ServiceConfig struct {
Name string `yaml:"name"`
ConsulKVPath string `yaml:"consul_kv_path"`
LogConfigs []LogConfig `yaml:"log_configs"`
Destination Destination `yaml:"destination"`
}
type LogConfig struct {
LogGroupName string `yaml:"log_group_name"`
LogStreamPrefix string `yaml:"log_stream_prefix"`
}
type Destination struct {
Type string `yaml:"type"`
FilePath string `yaml:"file_path"`
FileName string `yaml:"file_name"`
}
func init() {
InfoLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
ErrorLogger = log.New(os.Stderr, "", log.Ldate|log.Ltime)
FatalLogger = log.New(os.Stderr, "", log.Ldate|log.Ltime)
}
func main() {
configPath := os.Getenv("CONFIG_PATH")
if configPath == "" {
configPath = "config.yaml"
}
config := loadConfig(configPath)
sess := createAWSSession(config)
consulClient := setupConsulClient(config.Consul)
OffsetFallbackDuration := config.OffsetFallbackDuration
for _, service := range config.Services {
cwLogs := cloudwatchlogs.New(sess)
for _, logConfig := range service.LogConfigs {
logStreams, err := listLogStreams(cwLogs, logConfig.LogGroupName, logConfig.LogStreamPrefix)
if err != nil {
FatalLogger.Fatalf("failed to list log streams for %s: %v", service.Name, err)
}
for _, stream := range logStreams {
go tailLogStream(cwLogs, service, logConfig, stream, consulClient, OffsetFallbackDuration)
}
}
}
select {}
}
func loadConfig(path string) Config {
data, err := os.ReadFile(path)
if err != nil {
FatalLogger.Fatalf("failed to read config file: %v", err)
}
var config Config
if err := yaml.Unmarshal(data, &config); err != nil {
FatalLogger.Fatalf("failed to unmarshal config file: %v", err)
}
return config
}
func setupConsulClient(consulConfig ConsulConfig) *api.Client {
config := api.DefaultConfig()
config.Address = consulConfig.Address
config.Token = consulConfig.Token
client, err := api.NewClient(config)
if err != nil {
FatalLogger.Fatalf("failed to create Consul client: %v", err)
}
return client
}
func createAWSSession(config Config) *session.Session {
sessOptions := session.Options{
Config: aws.Config{
Region: aws.String(config.AWSRegion),
},
}
// I used profile for local testing
if config.AWSProfile != "" {
sessOptions.Profile = config.AWSProfile
} else if config.AWSRoleARN != "" {
sess := session.Must(session.NewSession(&sessOptions.Config))
creds := stscreds.NewCredentials(sess, config.AWSRoleARN)
sessOptions.Config.Credentials = creds
} else if config.AWSAccessKey != "" && config.AWSSecretKey != "" {
sessOptions.Config.Credentials = credentials.NewStaticCredentials(
config.AWSAccessKey,
config.AWSSecretKey, "",
)
} else {
sessOptions.SharedConfigState = session.SharedConfigEnable
}
return session.Must(session.NewSessionWithOptions(sessOptions))
}
func listLogStreams(cwLogs *cloudwatchlogs.CloudWatchLogs, logGroupName, logStreamPrefix string) ([]string, error) {
var logStreams []string
err := cwLogs.DescribeLogStreamsPages(&cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: aws.String(logGroupName),
LogStreamNamePrefix: aws.String(logStreamPrefix),
}, func(page *cloudwatchlogs.DescribeLogStreamsOutput, lastPage bool) bool {
for _, stream := range page.LogStreams {
if strings.HasPrefix(*stream.LogStreamName, logStreamPrefix) {
logStreams = append(logStreams, *stream.LogStreamName)
}
}
return !lastPage
})
if err != nil {
return nil, err
}
return logStreams, nil
}
func tailLogStream(cwLogs *cloudwatchlogs.CloudWatchLogs, service ServiceConfig, logConfig LogConfig, logStreamName string, consulClient *api.Client, OffsetFallbackDuration time.Duration) {
OffsetPath := service.ConsulKVPath + "/" + logStreamName
lastTimestamp := loadOffsetFromConsul(consulClient, OffsetPath, OffsetFallbackDuration)
//InfoLogger.Printf("Starting to tail log stream %s from timestamp %d", logStreamName, lastTimestamp)
InfoLogger.Printf("Starting to tail log stream %s from timestamp %d (%s)", logStreamName, lastTimestamp, time.Unix(lastTimestamp/1000, 0).Format(time.RFC3339))
retryDelay := 10 * time.Second
maxRetryDelay := 5 * time.Minute
var nextToken *string
for {
params := &cloudwatchlogs.GetLogEventsInput{
LogGroupName: aws.String(logConfig.LogGroupName),
LogStreamName: aws.String(logStreamName),
StartTime: aws.Int64(lastTimestamp),
StartFromHead: aws.Bool(true),
Limit: aws.Int64(500),
NextToken: nextToken,
}
resp, err := cwLogs.GetLogEvents(params)
if err != nil {
ErrorLogger.Printf("Error getting log events for stream %s: %v", logStreamName, err)
time.Sleep(60 * time.Second)
continue
}
if len(resp.Events) > 0 {
for _, event := range resp.Events {
InfoLogger.Printf("[%s] %s\n", logStreamName, *event.Message)
}
lastTimestamp = *resp.Events[len(resp.Events)-1].Timestamp
err = saveOffsetToConsul(consulClient, OffsetPath, lastTimestamp)
if err != nil {
FatalLogger.Printf("Error saving offset to Consul: %v", err)
}
nextToken = resp.NextForwardToken
retryDelay = 10 * time.Second
} else {
if nextToken != nil && *resp.NextForwardToken == *nextToken {
lastTimestamp += 1
nextToken = nil
} else {
nextToken = resp.NextForwardToken
continue
}
time.Sleep(retryDelay)
if retryDelay < maxRetryDelay {
retryDelay *= 2
}
}
}
}
func saveOffsetToConsul(consulClient *api.Client, kvPath string, lastTimestamp int64) error {
kvPair := &api.KVPair{
Key: kvPath,
Value: []byte(fmt.Sprintf("%d", lastTimestamp)),
}
_, err := consulClient.KV().Put(kvPair, nil)
return err
}
func loadOffsetFromConsul(consulClient *api.Client, kvPath string, OffsetFallbackDuration time.Duration) int64 {
var lastTimestamp int64
kvPair, _, err := consulClient.KV().Get(kvPath, nil)
if err != nil {
FatalLogger.Fatalf("Failed to load offset from Consul: %v", err)
}
if kvPair == nil {
InfoLogger.Printf("Offset not found in Consul, using default timestamp of %s", OffsetFallbackDuration)
return time.Now().UTC().Add(-OffsetFallbackDuration).UnixMilli()
} else {
lastTimestamp, err = strconv.ParseInt(string(kvPair.Value), 10, 64)
if err != nil {
FatalLogger.Fatalf("Failed to parse offset from Consul: %v", err)
}
}
return lastTimestamp
}