-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lambda-promtail: Add support for Kinesis data stream events (#5977)
**What this PR does / why we need it**: This PR adds support for sending Kinesis data stream events to lambda-promtail. One use case would be e.g. to send CloudFront realtime logs to Loki. **Which issue(s) this PR fixes**: Fixes #5978 **Special notes for your reviewer**: n/a **Checklist** - [x] Documentation added - [x] Tests updated - [x] Is this an important fix or new feature? Add an entry in the `CHANGELOG.md`. - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/upgrading/_index.md`
- Loading branch information
Showing
7 changed files
with
174 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/grafana/loki/pkg/logproto" | ||
"github.com/prometheus/common/model" | ||
) | ||
|
||
func parseKinesisEvent(ctx context.Context, b batchIf, ev *events.KinesisEvent) error { | ||
if ev == nil { | ||
return nil | ||
} | ||
|
||
for _, record := range ev.Records { | ||
timestamp := time.UnixMilli(record.Kinesis.ApproximateArrivalTimestamp.Unix()) | ||
|
||
labels := model.LabelSet{ | ||
model.LabelName("__aws_log_type"): model.LabelValue("kinesis"), | ||
model.LabelName("__aws_kinesis_event_source_arn"): model.LabelValue(record.EventSourceArn), | ||
} | ||
|
||
labels = applyExtraLabels(labels) | ||
|
||
b.add(ctx, entry{labels, logproto.Entry{ | ||
Line: string(record.Kinesis.Data), | ||
Timestamp: timestamp, | ||
}}) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func processKinesisEvent(ctx context.Context, ev *events.KinesisEvent) error { | ||
batch, _ := newBatch(ctx) | ||
|
||
err := parseKinesisEvent(ctx, batch, ev) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = sendToPromtail(ctx, batch) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/grafana/loki/pkg/logproto" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type MockBatch struct { | ||
streams map[string]*logproto.Stream | ||
size int | ||
} | ||
|
||
func (b *MockBatch) add(ctx context.Context, e entry) error { | ||
b.streams[e.labels.String()] = &logproto.Stream{ | ||
Labels: e.labels.String(), | ||
} | ||
return nil | ||
} | ||
|
||
func (b *MockBatch) flushBatch(ctx context.Context) error { | ||
return nil | ||
} | ||
func (b *MockBatch) encode() ([]byte, int, error) { | ||
return nil, 0, nil | ||
} | ||
func (b *MockBatch) createPushRequest() (*logproto.PushRequest, int) { | ||
return nil, 0 | ||
} | ||
|
||
func ReadJSONFromFile(t *testing.T, inputFile string) []byte { | ||
inputJSON, err := ioutil.ReadFile(inputFile) | ||
if err != nil { | ||
t.Errorf("could not open test file. details: %v", err) | ||
} | ||
|
||
return inputJSON | ||
} | ||
|
||
func TestLambdaPromtail_KinesisParseEvents(t *testing.T) { | ||
inputJson, err := ioutil.ReadFile("../testdata/kinesis-event.json") | ||
|
||
if err != nil { | ||
t.Errorf("could not open test file. details: %v", err) | ||
} | ||
|
||
var testEvent events.KinesisEvent | ||
if err := json.Unmarshal(inputJson, &testEvent); err != nil { | ||
t.Errorf("could not unmarshal event. details: %v", err) | ||
} | ||
|
||
ctx := context.TODO() | ||
b := &MockBatch{ | ||
streams: map[string]*logproto.Stream{}, | ||
} | ||
|
||
err = parseKinesisEvent(ctx, b, &testEvent) | ||
require.Nil(t, err) | ||
|
||
labels_str := "{__aws_kinesis_event_source_arn=\"arn:aws:kinesis:us-east-1:123456789012:stream/simple-stream\", __aws_log_type=\"kinesis\"}" | ||
require.Contains(t, b.streams, labels_str) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"Records": [ | ||
{ | ||
"kinesis": { | ||
"kinesisSchemaVersion": "1.0", | ||
"partitionKey": "s1", | ||
"sequenceNumber": "49568167373333333333333333333333333333333333333333333333", | ||
"data": "SGVsbG8gV29ybGQ=", | ||
"approximateArrivalTimestamp": 1480641523.477 | ||
}, | ||
"eventSource": "aws:kinesis", | ||
"eventVersion": "1.0", | ||
"eventID": "shardId-000000000000:49568167373333333333333333333333333333333333333333333333", | ||
"eventName": "aws:kinesis:record", | ||
"invokeIdentityArn": "arn:aws:iam::123456789012:role/LambdaRole", | ||
"awsRegion": "us-east-1", | ||
"eventSourceARN": "arn:aws:kinesis:us-east-1:123456789012:stream/simple-stream" | ||
}, | ||
{ | ||
"kinesis": { | ||
"kinesisSchemaVersion": "1.0", | ||
"partitionKey": "s1", | ||
"sequenceNumber": "49568167373333333334444444444444444444444444444444444444", | ||
"data": "SGVsbG8gV29ybGQ=", | ||
"approximateArrivalTimestamp": 1480841523.477 | ||
}, | ||
"eventSource": "aws:kinesis", | ||
"eventVersion": "1.0", | ||
"eventID": "shardId-000000000000:49568167373333333334444444444444444444444444444444444444", | ||
"eventName": "aws:kinesis:record", | ||
"invokeIdentityArn": "arn:aws:iam::123456789012:role/LambdaRole", | ||
"awsRegion": "us-east-1", | ||
"eventSourceARN": "arn:aws:kinesis:us-east-1:123456789012:stream/simple-stream" | ||
} | ||
] | ||
} |