forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartitioner.go
More file actions
66 lines (55 loc) · 2.47 KB
/
partitioner.go
File metadata and controls
66 lines (55 loc) · 2.47 KB
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package kafkaexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/kafkaexporter"
import (
"fmt"
"github.com/twmb/franz-go/pkg/kgo"
"go.opentelemetry.io/collector/component"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/kafka"
)
const (
// RecordPartitionerTypeSaramaCompatible is the default partitioner. It uses a sticky
// key partitioner with Sarama-compatible FNV-1a hashing when a record key is set,
// and a random sticky partition when no key is set.
RecordPartitionerTypeSaramaCompatible = "sarama_compatible"
// RecordPartitionerTypeRoundRobin distributes records evenly across all available
// partitions in a round-robin fashion, regardless of the record key.
RecordPartitionerTypeRoundRobin = "round_robin"
// RecordPartitionerTypeLeastBackup routes each record to the partition with the fewest
// buffered records, which can reduce produce latency under uneven load.
RecordPartitionerTypeLeastBackup = "least_backup"
// RecordPartitionerTypeExtension delegates partitioning to a user-provided extension
// that implements RecordPartitionerExtension.
RecordPartitionerTypeExtension = "custom"
)
// RecordPartitionerExtension is implemented by extensions that supply a custom Kafka record
// partitioner for use with the kafka exporter.
type RecordPartitionerExtension interface {
component.Component
GetPartitioner() kgo.Partitioner
}
func buildPartitionerOpt(cfg RecordPartitionerConfig, host component.Host) (kgo.Opt, error) {
switch cfg.Type {
case "", RecordPartitionerTypeSaramaCompatible:
return kgo.RecordPartitioner(kafka.NewSaramaCompatPartitioner()), nil
case RecordPartitionerTypeRoundRobin:
return kgo.RecordPartitioner(kgo.RoundRobinPartitioner()), nil
case RecordPartitionerTypeLeastBackup:
return kgo.RecordPartitioner(kgo.LeastBackupPartitioner()), nil
case RecordPartitionerTypeExtension:
if cfg.Extension == nil {
return nil, errRecordPartitionerExtRequired
}
ext, ok := host.GetExtensions()[*cfg.Extension]
if !ok {
return nil, fmt.Errorf("partitioner extension %q not found", cfg.Extension)
}
partExt, ok := ext.(RecordPartitionerExtension)
if !ok {
return nil, fmt.Errorf("extension %q does not implement RecordPartitionerExtension", cfg.Extension)
}
return kgo.RecordPartitioner(partExt.GetPartitioner()), nil
default:
return nil, fmt.Errorf("unknown partitioner type %q", cfg.Type)
}
}