-
Notifications
You must be signed in to change notification settings - Fork 25
/
lamdba.go
58 lines (47 loc) · 1.21 KB
/
lamdba.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
package aws
import (
"context"
"sync"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/kentik/ktranslate/pkg/kt"
)
func (vpc *AwsVpc) handleLamdba(ctx context.Context, s3Event events.S3Event) error {
var wg sync.WaitGroup
outputCB := func(err error) {
if err != nil {
vpc.Errorf("Cannot send: %v", err)
}
wg.Done()
}
lines := 0
for _, record := range s3Event.Records {
if record.EventName != "ObjectCreated:Put" {
vpc.Warnf("Skipping non put operation: %s", record.EventName)
continue
}
obj := &s3.Object{
Key: aws.String(record.S3.Object.Key),
}
err := vpc.processObject(record.S3.Bucket.Name, obj)
if err != nil {
return err
}
// Get the record back, turn it into flow.
rec := <-vpc.recs
dst := make([]*kt.JCHF, len(rec.Lines))
vpc.Debugf("Found %d logs to send", len(rec.Lines))
lines += len(rec.Lines)
for i, l := range rec.Lines {
dst[i] = l.ToFlow(vpc, vpc.topo)
}
wg.Add(1)
vpc.lambdaHandler(dst, outputCB) // Decrement in the callback.
}
if lines > 0 {
vpc.Infof("Waiting on %d records with %d lines to finish sending", len(s3Event.Records), lines)
wg.Wait()
}
return nil
}