forked from stripe/veneur
/
kafka.go
398 lines (341 loc) · 12.1 KB
/
kafka.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package kafka
import (
"context"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"io/ioutil"
"math"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/Shopify/sarama"
"github.com/gogo/protobuf/proto"
gometrics "github.com/rcrowley/go-metrics"
"github.com/sirupsen/logrus"
"github.com/stripe/veneur/samplers"
"github.com/stripe/veneur/sinks"
"github.com/stripe/veneur/ssf"
"github.com/stripe/veneur/trace"
"github.com/stripe/veneur/trace/metrics"
)
func init() {
gometrics.UseNilMetrics = true
}
const IngestTimeout = 5 * time.Second
var IngestTimeoutError = errors.New("Timed out writing to Kafka producer")
var _ sinks.MetricSink = &KafkaMetricSink{}
var _ sinks.SpanSink = &KafkaSpanSink{}
type KafkaMetricSink struct {
logger *logrus.Entry
producer sarama.AsyncProducer
checkTopic string
eventTopic string
metricTopic string
brokers string
config *sarama.Config
traceClient *trace.Client
}
type KafkaSpanSink struct {
logger *logrus.Entry
producer sarama.AsyncProducer
topic string
brokers string
serializer string
sampleTag string
sampleThreshold uint32
config *sarama.Config
spansFlushed int64
traceClient *trace.Client
}
// NewKafkaMetricSink creates a new Kafka Plugin.
func NewKafkaMetricSink(logger *logrus.Logger, cl *trace.Client, brokers string, checkTopic string, eventTopic string, metricTopic string, ackRequirement string, partitioner string, retries int, bufferBytes int, bufferMessages int, bufferDuration string) (*KafkaMetricSink, error) {
if logger == nil {
logger = &logrus.Logger{Out: ioutil.Discard}
}
if checkTopic == "" && eventTopic == "" && metricTopic == "" {
return nil, errors.New("Unable to start Kafka sink with no valid topic names")
}
ll := logger.WithField("metric_sink", "kafka")
var finalBufferDuration time.Duration
if bufferDuration != "" {
var err error
finalBufferDuration, err = time.ParseDuration(bufferDuration)
if err != nil {
return nil, err
}
}
config, _ := newProducerConfig(ll, ackRequirement, partitioner, retries, bufferBytes, bufferMessages, finalBufferDuration)
ll.WithFields(logrus.Fields{
"brokers": brokers,
"check_topic": checkTopic,
"event_topic": eventTopic,
"metric_topic": metricTopic,
"partitioner": partitioner,
"ack_requirement": ackRequirement,
"max_retries": retries,
"buffer_bytes": bufferBytes,
"buffer_messages": bufferMessages,
"buffer_duration": bufferDuration,
}).Info("Created Kafka metric sink")
return &KafkaMetricSink{
logger: ll,
checkTopic: checkTopic,
eventTopic: eventTopic,
metricTopic: metricTopic,
brokers: brokers,
config: config,
traceClient: cl,
}, nil
}
func newProducerConfig(logger *logrus.Entry, ackRequirement string, partitioner string, retries int, bufferBytes int, bufferMessages int, bufferFrequency time.Duration) (*sarama.Config, error) {
config := sarama.NewConfig()
// TODO Stringer?
switch ackRequirement {
case "all":
config.Producer.RequiredAcks = sarama.WaitForAll
case "none":
config.Producer.RequiredAcks = sarama.NoResponse
case "local":
config.Producer.RequiredAcks = sarama.WaitForLocal
default:
logrus.WithField("ack_requirement", ackRequirement).Warn("Unknown ack requirement, defaulting to all")
config.Producer.RequiredAcks = sarama.WaitForAll
}
switch partitioner {
case "random":
config.Producer.Partitioner = sarama.NewRandomPartitioner
default:
config.Producer.Partitioner = sarama.NewHashPartitioner
}
if bufferBytes != 0 {
config.Producer.Flush.Bytes = bufferBytes
}
if bufferMessages != 0 {
config.Producer.Flush.Messages = bufferMessages
}
if bufferFrequency != 0 {
config.Producer.Flush.Frequency = bufferFrequency
}
config.Producer.Retry.Max = retries
// If either of these is set to true, you must
// read from the corresponding channels in a separate
// goroutine. Otherwise, the entire sink will back up.
config.Producer.Return.Successes = false
config.Producer.Return.Errors = false
return config, nil
}
// newConfiguredProducer returns a configured Sarama SyncProducer
func newConfiguredProducer(logger *logrus.Entry, brokerString string, config *sarama.Config) (sarama.AsyncProducer, error) {
brokerList := strings.Split(brokerString, ",")
if len(brokerList) < 1 {
logger.WithField("addrs", brokerString).Error("No brokers?")
return nil, errors.New("No brokers in broker list")
}
logger.WithField("addrs", brokerList).Info("Connecting to Kafka")
producer, err := sarama.NewAsyncProducer(brokerList, config)
if err != nil {
logger.Error("Error Connecting to Kafka. client error: ", err)
}
return producer, nil
}
// Name returns the name of this sink.
func (k *KafkaMetricSink) Name() string {
return "kafka"
}
// Start performs final adjustments on the sink.
func (k *KafkaMetricSink) Start(cl *trace.Client) error {
producer, err := newConfiguredProducer(k.logger, k.brokers, k.config)
if err != nil {
return err
}
k.producer = producer
return nil
}
// Flush sends a slice of metrics to Kafka
func (k *KafkaMetricSink) Flush(ctx context.Context, interMetrics []samplers.InterMetric) error {
samples := &ssf.Samples{}
defer metrics.Report(k.traceClient, samples)
if len(interMetrics) == 0 {
k.logger.Info("Nothing to flush, skipping.")
return nil
}
successes := int64(0)
for _, metric := range interMetrics {
if !sinks.IsAcceptableMetric(metric, k) {
continue
}
k.logger.Debug("Emitting Metric: ", metric.Name)
j, err := json.Marshal(metric)
if err != nil {
k.logger.Error("Error marshalling metric: ", metric.Name)
samples.Add(ssf.Count("kafka.marshal.error_total", 1, nil))
return err
}
k.producer.Input() <- &sarama.ProducerMessage{
Topic: k.metricTopic,
Value: sarama.StringEncoder(j),
}
successes++
}
samples.Add(ssf.Count(sinks.MetricKeyTotalMetricsFlushed, float32(successes), map[string]string{"sink": k.Name()}))
return nil
}
// FlushOtherSamples flushes non-metric, non-span samples
func (k *KafkaMetricSink) FlushOtherSamples(ctx context.Context, samples []ssf.SSFSample) {
// TODO
}
// NewKafkaSpanSink creates a new Kafka Plugin.
func NewKafkaSpanSink(logger *logrus.Logger, cl *trace.Client, brokers string, topic string, partitioner string, ackRequirement string, retries int, bufferBytes int, bufferMessages int, bufferDuration string, serializationFormat string, sampleTag string, sampleRatePercentage float64) (*KafkaSpanSink, error) {
if logger == nil {
logger = &logrus.Logger{Out: ioutil.Discard}
}
if topic == "" {
return nil, errors.New("Cannot start Kafka span sink with no span topic")
}
ll := logger.WithField("span_sink", "kafka")
serializer := serializationFormat
if serializer != "json" && serializer != "protobuf" {
ll.WithField("serializer", serializer).Warn("Unknown serializer, defaulting to protobuf")
serializer = "protobuf"
}
var sampleThreshold uint32
if sampleRatePercentage < 0 || sampleRatePercentage > 100 {
return nil, errors.New("Span sample rate percentage must be greater than 0%% and less than or equal to 100%%")
}
// Set the sample threshold to (sample rate) * (maximum value of uint32), so that
// we can store it as a uint32 instead of a float64 and compare apples-to-apples
// with the output of our hashing algorithm.
sampleThreshold = uint32(sampleRatePercentage * math.MaxUint32 / 100)
var finalBufferDuration time.Duration
if bufferDuration != "" {
var err error
finalBufferDuration, err = time.ParseDuration(bufferDuration)
if err != nil {
return nil, err
}
}
config, _ := newProducerConfig(ll, ackRequirement, partitioner, retries, bufferBytes, bufferMessages, finalBufferDuration)
ll.WithFields(logrus.Fields{
"brokers": brokers,
"topic": topic,
"partitioner": partitioner,
"ack_requirement": ackRequirement,
"max_retries": retries,
"buffer_bytes": bufferBytes,
"buffer_messages": bufferMessages,
"buffer_duration": bufferDuration,
}).Info("Started Kafka span sink")
return &KafkaSpanSink{
logger: ll,
topic: topic,
brokers: brokers,
config: config,
serializer: serializer,
sampleTag: sampleTag,
sampleThreshold: sampleThreshold,
}, nil
}
// Name returns the name of this sink.
func (k *KafkaSpanSink) Name() string {
return "kafka"
}
// Start performs final adjustments on the sink.
func (k *KafkaSpanSink) Start(cl *trace.Client) error {
producer, err := newConfiguredProducer(k.logger, k.brokers, k.config)
if err != nil {
return err
}
k.producer = producer
return nil
}
// Ingest takes the span and adds it to Kafka producer for async flushing. The
// flushing is driven by the settings from KafkaSpanSink's constructor. Tune
// the bytes, messages and interval settings to your tastes!
func (k *KafkaSpanSink) Ingest(span *ssf.SSFSpan) error {
samples := &ssf.Samples{}
defer metrics.Report(k.traceClient, samples)
// If we're sampling less than 100%, we should check whether a span should
// be sampled:
if k.sampleTag != "" || k.sampleThreshold < uint32(math.MaxUint32) {
var hashKey uint32
var sampleTagValue string
if k.sampleTag == "" {
// If we haven't set a sampleTag, we'll be hashing based on the traceID
sampleTagValue = strconv.FormatInt(span.TraceId, 10)
} else {
// If we've set a sampleTag, we'll be hashing based off of that tag's value.
var exists bool
sampleTagValue, exists = span.Tags[k.sampleTag]
if !exists {
// If the span isn't tagged appropriately, we should drop it, regardless
// of our sample rate.
k.logger.Debug("Rejected span without appropriate tag")
samples.Add(ssf.Count(sinks.MetricKeyTotalSpansDropped, 1, map[string]string{"sink": k.Name()}))
return nil
}
}
// Lifted from https://github.com/stathat/consistent/blob/75142be0209ec69bb014c7a1ac7d1a3c892c6424/consistent.go#L238-L245:
// if the sample tag value that we're hashing is shorter than 64 bytes, we
// need to pad it with zeroes for the crc32.ChecksumIEEE function.
if len(sampleTagValue) < 64 {
var scratch [64]byte
copy(scratch[:], sampleTagValue)
hashKey = crc32.ChecksumIEEE(scratch[:len(sampleTagValue)])
} else {
hashKey = crc32.ChecksumIEEE([]byte(sampleTagValue))
}
// Reject any spans whose hash keys end up greater than the threshold that
// we previously computed.
if hashKey > k.sampleThreshold {
k.logger.WithField("traceId", span.TraceId).WithField("sampleTag", k.sampleTag).WithField("sampleTagValue", sampleTagValue).WithField("hashKey", hashKey).WithField("sampleThreshold", k.sampleThreshold).Debug("Rejected span based off of sampling rules")
samples.Add(ssf.Count(sinks.MetricKeyTotalSpansSkipped, 1, map[string]string{"sink": k.Name()}))
return nil
}
}
var enc sarama.Encoder
switch k.serializer {
case "json":
j, err := json.Marshal(span)
if err != nil {
k.logger.Error("Error marshalling span")
samples.Add(ssf.Count("kafka.span_marshal_error_total", 1, nil))
return err
}
enc = sarama.StringEncoder(j)
case "protobuf":
p, err := proto.Marshal(span)
if err != nil {
k.logger.Error("Error marshalling span")
samples.Add(ssf.Count("kafka.span_marshal_error_total", 1, nil))
return err
}
enc = sarama.ByteEncoder(p)
default:
return fmt.Errorf("Unknown serialization format for encoding Kafka message: %s", k.serializer)
}
message := &sarama.ProducerMessage{
Topic: k.topic,
Value: enc,
}
select {
case k.producer.Input() <- message:
atomic.AddInt64(&k.spansFlushed, 1)
return nil
case _ = <-time.After(IngestTimeout):
return IngestTimeoutError
}
}
// Flush emits metrics, since the spans have already been ingested and are
// sending async.
func (k *KafkaSpanSink) Flush() {
// TODO We have no stuff in here for detecting failed writes from the async
// producer. We should add that.
k.logger.WithFields(logrus.Fields{
"flushed_spans": atomic.LoadInt64(&k.spansFlushed),
}).Debug("Checkpointing flushed spans for Kafka")
metrics.ReportOne(k.traceClient, ssf.Count(sinks.MetricKeyTotalSpansFlushed, float32(atomic.LoadInt64(&k.spansFlushed)), map[string]string{"sink": k.Name()}))
atomic.SwapInt64(&k.spansFlushed, 0)
}