-
Notifications
You must be signed in to change notification settings - Fork 30
/
awscloudwatch.go
61 lines (52 loc) · 1.55 KB
/
awscloudwatch.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
// nolint:lll
// inspired by: https://github.com/RedHatInsights/insights-ingress-go/blob/3ea33a8d793c2154f7cfa12057ca005c5f6031fa/logger/logger.go
//
// https://github.com/kdar/logrus-cloudwatchlogs
package utils
import (
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
lc "github.com/redhatinsights/platform-go-middlewares/logging/cloudwatch"
log "github.com/sirupsen/logrus"
)
var hook *lc.Hook
// Try to init CloudWatch logging
func trySetupCloudWatchLogging() {
key := CoreCfg.CloudWatchAccessKeyID
if key == "" {
LogInfo("config for aws CloudWatch not loaded")
return
}
secret := FailIfEmpty(CoreCfg.CloudWatchSecretAccesskey, "CW_AWS_SECRET_ACCESS_KEY")
region := CoreCfg.CloudWatchRegion
group := CoreCfg.CloudWatchLogGroup
hostname, err := os.Hostname()
if err != nil {
LogError("err", err.Error(), "unable to get hostname to set CloudWatch log_stream")
return
}
log.SetFormatter(&log.JSONFormatter{
TimestampFormat: "2006-01-02T15:04:05.999Z",
FieldMap: log.FieldMap{
log.FieldKeyTime: "@timestamp",
log.FieldKeyLevel: "level",
log.FieldKeyMsg: "message",
},
})
cred := credentials.NewStaticCredentials(key, secret, "")
awsconf := aws.NewConfig().WithRegion(region).WithCredentials(cred)
hook, err = lc.NewBatchingHook(group, hostname, awsconf, 10*time.Second)
if err != nil {
LogError("err", err.Error(), "unable to setup CloudWatch logging")
return
}
log.AddHook(hook)
log.Info("CloudWatch logging configured")
}
func FlushLogs() {
if hook != nil {
_ = hook.Flush()
}
}