-
Notifications
You must be signed in to change notification settings - Fork 50
/
output_kinesis.go
137 lines (105 loc) · 3.84 KB
/
output_kinesis.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package stream
import (
"context"
"fmt"
"time"
"github.com/justtrackio/gosoline/pkg/cfg"
gosoKinesis "github.com/justtrackio/gosoline/pkg/cloud/aws/kinesis"
"github.com/justtrackio/gosoline/pkg/exec"
"github.com/justtrackio/gosoline/pkg/log"
"github.com/justtrackio/gosoline/pkg/mdl"
"github.com/spf13/cast"
)
const (
AttributeKinesisPartitionKey = "gosoline.kinesis.partitionKey"
AttributeKinesisExplicitHashKey = "gosoline.kinesis.explicitHashKey"
)
type KinesisOutputSettings struct {
cfg.AppId
ClientName string
StreamName string
}
func (s KinesisOutputSettings) GetAppId() cfg.AppId {
return s.AppId
}
func (s KinesisOutputSettings) GetClientName() string {
return s.ClientName
}
func (s KinesisOutputSettings) GetStreamName() string {
return s.StreamName
}
type kinesisOutput struct {
recordWriter gosoKinesis.RecordWriter
}
func NewKinesisOutput(ctx context.Context, config cfg.Config, logger log.Logger, settings *KinesisOutputSettings) (Output, error) {
settings.PadFromConfig(config)
var err error
var fullStreamName gosoKinesis.Stream
var recordWriter gosoKinesis.RecordWriter
if fullStreamName, err = gosoKinesis.GetStreamName(config, settings); err != nil {
return nil, fmt.Errorf("can not get stream name: %w", err)
}
backoffSettings := exec.ReadBackoffSettings(config)
backoffSettings.InitialInterval = time.Second
recordWriterSettings := &gosoKinesis.RecordWriterSettings{
ClientName: settings.ClientName,
StreamName: string(fullStreamName),
Backoff: backoffSettings,
}
if recordWriter, err = gosoKinesis.NewRecordWriter(ctx, config, logger, recordWriterSettings); err != nil {
return nil, fmt.Errorf("can not create record writer for stream %s: %w", fullStreamName, err)
}
return NewKinesisOutputWithInterfaces(recordWriter), nil
}
func NewKinesisOutputWithInterfaces(recordWriter gosoKinesis.RecordWriter) Output {
return &kinesisOutput{
recordWriter: recordWriter,
}
}
func (o *kinesisOutput) WriteOne(ctx context.Context, record WritableMessage) error {
return o.Write(ctx, []WritableMessage{record})
}
func (o *kinesisOutput) Write(ctx context.Context, batch []WritableMessage) error {
var err error
records := make([]*gosoKinesis.Record, len(batch))
for i, msg := range batch {
if records[i], err = o.buildRecord(msg); err != nil {
return fmt.Errorf("can not build record: %w", err)
}
}
return o.recordWriter.PutRecords(ctx, records)
}
func (o *kinesisOutput) IsPartitionedOutput() bool {
return true
}
func (o *kinesisOutput) GetMaxMessageSize() *int {
return mdl.Box(1024 * 1024)
}
func (o *kinesisOutput) GetMaxBatchSize() *int {
return mdl.Box(500)
}
func (o *kinesisOutput) buildRecord(msg WritableMessage) (*gosoKinesis.Record, error) {
var err error
var partitionKey, explicitHashKey string
record := &gosoKinesis.Record{}
if record.Data, err = msg.MarshalToBytes(); err != nil {
return nil, fmt.Errorf("can not marshal message to bytes: %w", err)
}
attributes := getAttributes(msg)
if p, ok := attributes[AttributeKinesisPartitionKey]; ok {
if partitionKey, err = cast.ToStringE(p); err != nil {
return nil, fmt.Errorf("the type of the %s attribute with value %v should be castable to string: %w", AttributeKinesisPartitionKey, attributes[AttributeKinesisPartitionKey], err)
}
record.PartitionKey = &partitionKey
}
if p, ok := attributes[AttributeKinesisExplicitHashKey]; ok {
if explicitHashKey, err = cast.ToStringE(p); err != nil {
return nil, fmt.Errorf("the type of the %s attribute with value %v should be castable to string: %w", AttributeKinesisExplicitHashKey, attributes[AttributeKinesisExplicitHashKey], err)
}
record.ExplicitHashKey = &explicitHashKey
// yes, this looks strange, but we need to provide something or AWS complains, so we just do that
// and hope it is ignored as documented...
record.PartitionKey = &explicitHashKey
}
return record, nil
}