/
kinesis.go
196 lines (161 loc) · 4.77 KB
/
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
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
196
//go:generate ../../../tools/readme_config_includer/generator
package kinesis
import (
"context"
_ "embed"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/kinesis"
"github.com/aws/aws-sdk-go-v2/service/kinesis/types"
"github.com/gofrs/uuid"
"github.com/influxdata/telegraf"
internalaws "github.com/influxdata/telegraf/config/aws"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/telegraf/plugins/serializers"
)
// DO NOT REMOVE THE NEXT TWO LINES! This is required to embed the sampleConfig data.
//go:embed sample.conf
var sampleConfig string
// Limit set by AWS (https://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecords.html)
const maxRecordsPerRequest uint32 = 500
type (
KinesisOutput struct {
StreamName string `toml:"streamname"`
PartitionKey string `toml:"partitionkey" deprecated:"1.5.0;use 'partition.key' instead"`
RandomPartitionKey bool `toml:"use_random_partitionkey" deprecated:"1.5.0;use 'partition.method' instead"`
Partition *Partition `toml:"partition"`
Debug bool `toml:"debug"`
Log telegraf.Logger `toml:"-"`
serializer serializers.Serializer
svc kinesisClient
internalaws.CredentialConfig
}
Partition struct {
Method string `toml:"method"`
Key string `toml:"key"`
Default string `toml:"default"`
}
)
type kinesisClient interface {
PutRecords(context.Context, *kinesis.PutRecordsInput, ...func(*kinesis.Options)) (*kinesis.PutRecordsOutput, error)
}
func (*KinesisOutput) SampleConfig() string {
return sampleConfig
}
func (k *KinesisOutput) Connect() error {
if k.Partition == nil {
k.Log.Error("Deprecated partitionkey configuration in use, please consider using outputs.kinesis.partition")
}
// We attempt first to create a session to Kinesis using an IAMS role, if that fails it will fall through to using
// environment variables, and then Shared Credentials.
if k.Debug {
k.Log.Infof("Establishing a connection to Kinesis in %s", k.Region)
}
cfg, err := k.CredentialConfig.Credentials()
if err != nil {
return err
}
svc := kinesis.NewFromConfig(cfg)
_, err = svc.DescribeStreamSummary(context.Background(), &kinesis.DescribeStreamSummaryInput{
StreamName: aws.String(k.StreamName),
})
k.svc = svc
return err
}
func (k *KinesisOutput) Close() error {
return nil
}
func (k *KinesisOutput) SetSerializer(serializer serializers.Serializer) {
k.serializer = serializer
}
func (k *KinesisOutput) writeKinesis(r []types.PutRecordsRequestEntry) time.Duration {
start := time.Now()
payload := &kinesis.PutRecordsInput{
Records: r,
StreamName: aws.String(k.StreamName),
}
resp, err := k.svc.PutRecords(context.Background(), payload)
if err != nil {
k.Log.Errorf("Unable to write to Kinesis : %s", err.Error())
return time.Since(start)
}
if k.Debug {
k.Log.Infof("Wrote: '%+v'", resp)
}
failed := *resp.FailedRecordCount
if failed > 0 {
k.Log.Errorf("Unable to write %+v of %+v record(s) to Kinesis", failed, len(r))
}
return time.Since(start)
}
func (k *KinesisOutput) getPartitionKey(metric telegraf.Metric) string {
if k.Partition != nil {
switch k.Partition.Method {
case "static":
return k.Partition.Key
case "random":
u, err := uuid.NewV4()
if err != nil {
return k.Partition.Default
}
return u.String()
case "measurement":
return metric.Name()
case "tag":
if t, ok := metric.GetTag(k.Partition.Key); ok {
return t
} else if len(k.Partition.Default) > 0 {
return k.Partition.Default
}
// Default partition name if default is not set
return "telegraf"
default:
k.Log.Errorf("You have configured a Partition method of '%s' which is not supported", k.Partition.Method)
}
}
if k.RandomPartitionKey {
u, err := uuid.NewV4()
if err != nil {
return k.Partition.Default
}
return u.String()
}
return k.PartitionKey
}
func (k *KinesisOutput) Write(metrics []telegraf.Metric) error {
var sz uint32
if len(metrics) == 0 {
return nil
}
r := []types.PutRecordsRequestEntry{}
for _, metric := range metrics {
sz++
values, err := k.serializer.Serialize(metric)
if err != nil {
k.Log.Debugf("Could not serialize metric: %v", err)
continue
}
partitionKey := k.getPartitionKey(metric)
d := types.PutRecordsRequestEntry{
Data: values,
PartitionKey: aws.String(partitionKey),
}
r = append(r, d)
if sz == maxRecordsPerRequest {
elapsed := k.writeKinesis(r)
k.Log.Debugf("Wrote a %d point batch to Kinesis in %+v.", sz, elapsed)
sz = 0
r = nil
}
}
if sz > 0 {
elapsed := k.writeKinesis(r)
k.Log.Debugf("Wrote a %d point batch to Kinesis in %+v.", sz, elapsed)
}
return nil
}
func init() {
outputs.Add("kinesis", func() telegraf.Output {
return &KinesisOutput{}
})
}