/
send_aws_s3.go
195 lines (158 loc) · 4.6 KB
/
send_aws_s3.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package transform
import (
"context"
"encoding/json"
"fmt"
"os"
"sync"
"github.com/brexhq/substation/config"
"github.com/brexhq/substation/message"
"github.com/brexhq/substation/internal/aggregate"
"github.com/brexhq/substation/internal/aws"
"github.com/brexhq/substation/internal/aws/s3manager"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
"github.com/brexhq/substation/internal/file"
)
type sendAWSS3Config struct {
// BucketName is the AWS S3 bucket that data is written to.
BucketName string `json:"bucket_name"`
// FilePath determines how the name of the uploaded object is constructed.
// See filePath.New for more information.
FilePath file.Path `json:"file_path"`
// UseBatchKeyAsPrefix determines if the batch key should be used as the prefix.
UseBatchKeyAsPrefix bool `json:"use_batch_key_as_prefix"`
// AuxTransforms are applied to batched data before it is sent.
AuxTransforms []config.Config `json:"auxiliary_transforms"`
Object iconfig.Object `json:"object"`
Batch iconfig.Batch `json:"batch"`
AWS iconfig.AWS `json:"aws"`
Retry iconfig.Retry `json:"retry"`
}
func (c *sendAWSS3Config) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func (c *sendAWSS3Config) Validate() error {
if c.BucketName == "" {
return fmt.Errorf("bucket_name: %v", errors.ErrMissingRequiredOption)
}
return nil
}
func newSendAWSS3(_ context.Context, cfg config.Config) (*sendAWSS3, error) {
conf := sendAWSS3Config{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, fmt.Errorf("transform: send_aws_s3: %v", err)
}
if err := conf.Validate(); err != nil {
return nil, fmt.Errorf("transform: send_aws_s3: %v", err)
}
tf := sendAWSS3{
conf: conf,
}
agg, err := aggregate.New(aggregate.Config{
Count: conf.Batch.Count,
Size: conf.Batch.Size,
Duration: conf.Batch.Duration,
})
if err != nil {
return nil, fmt.Errorf("transform: send_aws_s3: %v", err)
}
tf.agg = agg
if len(conf.AuxTransforms) > 0 {
tf.tforms = make([]Transformer, len(conf.AuxTransforms))
for i, c := range conf.AuxTransforms {
t, err := New(context.Background(), c)
if err != nil {
return nil, fmt.Errorf("transform: send_aws_s3: %v", err)
}
tf.tforms[i] = t
}
}
// Setup the AWS client.
tf.client.Setup(aws.Config{
Region: conf.AWS.Region,
RoleARN: conf.AWS.RoleARN,
MaxRetries: conf.Retry.Count,
RetryableErrors: conf.Retry.ErrorMessages,
})
return &tf, nil
}
type sendAWSS3 struct {
conf sendAWSS3Config
// client is safe for concurrent use.
client s3manager.UploaderAPI
mu sync.Mutex
agg *aggregate.Aggregate
tforms []Transformer
}
func (tf *sendAWSS3) Transform(ctx context.Context, msg *message.Message) ([]*message.Message, error) {
tf.mu.Lock()
defer tf.mu.Unlock()
if msg.IsControl() {
for key := range tf.agg.GetAll() {
if tf.agg.Count(key) == 0 {
continue
}
if err := tf.send(ctx, key); err != nil {
return nil, fmt.Errorf("transform: send_aws_s3: %v", err)
}
}
tf.agg.ResetAll()
return []*message.Message{msg}, nil
}
// If this value does not exist, then all data is batched together.
key := msg.GetValue(tf.conf.Object.BatchKey).String()
if ok := tf.agg.Add(key, msg.Data()); ok {
return []*message.Message{msg}, nil
}
if err := tf.send(ctx, key); err != nil {
return nil, fmt.Errorf("transform: send_aws_s3: %v", err)
}
// If data cannot be added after reset, then the batch is misconfgured.
tf.agg.Reset(key)
if ok := tf.agg.Add(key, msg.Data()); !ok {
return nil, fmt.Errorf("transform: send_aws_s3: %v", errSendBatchMisconfigured)
}
return []*message.Message{msg}, nil
}
func (tf *sendAWSS3) String() string {
b, _ := json.Marshal(tf.conf)
return string(b)
}
func (tf *sendAWSS3) send(ctx context.Context, key string) error {
p := tf.conf.FilePath
if key != "" && tf.conf.UseBatchKeyAsPrefix {
p.Prefix = key
}
filePath := p.New()
if filePath == "" {
return fmt.Errorf("file path is empty")
}
temp, err := os.CreateTemp("", "substation")
if err != nil {
return err
}
defer temp.Close()
data, err := withTransforms(ctx, tf.tforms, tf.agg.Get(key))
if err != nil {
return fmt.Errorf("transform: send_aws_s3: %v", err)
}
for _, d := range data {
if _, err := temp.Write(d); err != nil {
return err
}
}
// Flush the file before uploading to S3.
if err := temp.Close(); err != nil {
return err
}
f, err := os.Open(temp.Name())
if err != nil {
return err
}
defer f.Close()
if _, err := tf.client.Upload(ctx, tf.conf.BucketName, filePath, f); err != nil {
return err
}
return nil
}