-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcloudwatch.go
84 lines (66 loc) · 1.92 KB
/
cloudwatch.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
package cloudwatch
import (
"os"
"time"
log "github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/gliderlabs/logspout/router"
)
// Set batch sizes based CloudWatch Logs limits found in the developer guide.
// While the actual size limit is 1 MB, we use 900 KB due to small differences
// in how batch size is calculated.
const batchSize = 900000
const batchLength = 10000
const batchDuration = 250 * time.Millisecond
func init() {
router.AdapterFactories.Register(NewAdapter, "cloudwatch")
}
// Adapter ships logs to AWS CloudWatch.
type Adapter struct {
route *router.Route
logstream *LogStream
capacity Capacity
}
func init() {
level, err := log.ParseLevel(os.Getenv("LOG_LEVEL"))
if err != nil {
level = log.InfoLevel
}
log.SetLevel(level)
}
// NewAdapter instances a new AWS CloudWatch adapter.
func NewAdapter(route *router.Route) (router.LogAdapter, error) {
group := route.Address
stream, err := os.Hostname()
if err != nil {
return nil, err
}
logstream, err := NewLogStream(group, stream)
if err != nil {
return nil, err
}
capacity := Capacity{
Size: batchSize,
Length: batchLength,
Duration: batchDuration,
}
log.Infof("Created CloudWatch adapter - group: %s, stream: %s", group, stream)
return &Adapter{route: route, logstream: logstream, capacity: capacity}, nil
}
// Stream passes messages from a logspout message channel to AWS CloudWatch.
func (a *Adapter) Stream(logstream chan *router.Message) {
log.Infof("CloudWatch adapter is streaming Docker logs")
logs := filter(transform(logstream))
batches := batch(logs, a.capacity)
for batch := range batches {
events := make([]*cloudwatchlogs.InputLogEvent, len(batch))
for i, log := range batch {
events[i] = &cloudwatchlogs.InputLogEvent{
Message: aws.String(log.Body()),
Timestamp: aws.Int64(log.Timestamp()),
}
}
a.logstream.Log(events)
}
}