forked from RedHatInsights/patchman-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawscloudwatch.go
60 lines (51 loc) · 1.56 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
// 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 := os.Getenv("CW_AWS_ACCESS_KEY_ID")
if key == "" {
log.Info("config for aws CloudWatch not loaded")
return
}
secret := GetenvOrFail("CW_AWS_SECRET_ACCESS_KEY")
region := Getenv("CW_AWS_REGION", "us-east-1")
group := Getenv("CW_AWS_LOG_GROUP", "platform-dev")
hostname, err := os.Hostname()
if err != nil {
Log("err", err.Error()).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 {
Log("err", err.Error()).Error("unable to setup CloudWatch logging")
return
}
log.AddHook(hook)
log.Info("CloudWatch logging configured")
}
func FlushLogs() {
if hook != nil {
_ = hook.Flush()
}
}