-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
factory.go
186 lines (164 loc) · 4.94 KB
/
factory.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package kafkareceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkareceiver"
import (
"context"
"errors"
"strings"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/kafkaexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkareceiver/internal/metadata"
)
const (
defaultTracesTopic = "otlp_spans"
defaultMetricsTopic = "otlp_metrics"
defaultLogsTopic = "otlp_logs"
defaultEncoding = "otlp_proto"
defaultBroker = "localhost:9092"
defaultClientID = "otel-collector"
defaultGroupID = defaultClientID
defaultInitialOffset = offsetLatest
defaultSessionTimeout = 10 * time.Second
defaultHeartbeatInterval = 3 * time.Second
// default from sarama.NewConfig()
defaultMetadataRetryMax = 3
// default from sarama.NewConfig()
defaultMetadataRetryBackoff = time.Millisecond * 250
// default from sarama.NewConfig()
defaultMetadataFull = true
// default from sarama.NewConfig()
defaultAutoCommitEnable = true
// default from sarama.NewConfig()
defaultAutoCommitInterval = 1 * time.Second
// default from sarama.NewConfig()
defaultMinFetchSize = int32(1)
// default from sarama.NewConfig()
defaultDefaultFetchSize = int32(1048576)
// default from sarama.NewConfig()
defaultMaxFetchSize = int32(0)
)
var errUnrecognizedEncoding = errors.New("unrecognized encoding")
// FactoryOption applies changes to kafkaExporterFactory.
type FactoryOption func(factory *kafkaReceiverFactory)
// NewFactory creates Kafka receiver factory.
func NewFactory(options ...FactoryOption) receiver.Factory {
f := &kafkaReceiverFactory{}
for _, o := range options {
o(f)
}
return receiver.NewFactory(
metadata.Type,
createDefaultConfig,
receiver.WithTraces(f.createTracesReceiver, metadata.TracesStability),
receiver.WithMetrics(f.createMetricsReceiver, metadata.MetricsStability),
receiver.WithLogs(f.createLogsReceiver, metadata.LogsStability),
)
}
func createDefaultConfig() component.Config {
return &Config{
Encoding: defaultEncoding,
Brokers: []string{defaultBroker},
ClientID: defaultClientID,
GroupID: defaultGroupID,
InitialOffset: defaultInitialOffset,
SessionTimeout: defaultSessionTimeout,
HeartbeatInterval: defaultHeartbeatInterval,
Metadata: kafkaexporter.Metadata{
Full: defaultMetadataFull,
Retry: kafkaexporter.MetadataRetry{
Max: defaultMetadataRetryMax,
Backoff: defaultMetadataRetryBackoff,
},
},
AutoCommit: AutoCommit{
Enable: defaultAutoCommitEnable,
Interval: defaultAutoCommitInterval,
},
MessageMarking: MessageMarking{
After: false,
OnError: false,
},
HeaderExtraction: HeaderExtraction{
ExtractHeaders: false,
},
MinFetchSize: defaultMinFetchSize,
DefaultFetchSize: defaultDefaultFetchSize,
MaxFetchSize: defaultMaxFetchSize,
}
}
type kafkaReceiverFactory struct{}
func (f *kafkaReceiverFactory) createTracesReceiver(
_ context.Context,
set receiver.Settings,
cfg component.Config,
nextConsumer consumer.Traces,
) (receiver.Traces, error) {
oCfg := *(cfg.(*Config))
if oCfg.Topic == "" {
oCfg.Topic = defaultTracesTopic
}
r, err := newTracesReceiver(oCfg, set, nextConsumer)
if err != nil {
return nil, err
}
return r, nil
}
func (f *kafkaReceiverFactory) createMetricsReceiver(
_ context.Context,
set receiver.Settings,
cfg component.Config,
nextConsumer consumer.Metrics,
) (receiver.Metrics, error) {
oCfg := *(cfg.(*Config))
if oCfg.Topic == "" {
oCfg.Topic = defaultMetricsTopic
}
r, err := newMetricsReceiver(oCfg, set, nextConsumer)
if err != nil {
return nil, err
}
return r, nil
}
func (f *kafkaReceiverFactory) createLogsReceiver(
_ context.Context,
set receiver.Settings,
cfg component.Config,
nextConsumer consumer.Logs,
) (receiver.Logs, error) {
oCfg := *(cfg.(*Config))
if oCfg.Topic == "" {
oCfg.Topic = defaultLogsTopic
}
r, err := newLogsReceiver(oCfg, set, nextConsumer)
if err != nil {
return nil, err
}
return r, nil
}
func getLogsUnmarshaler(encoding string, unmarshalers map[string]LogsUnmarshaler) (LogsUnmarshaler, error) {
var enc string
unmarshaler, ok := unmarshalers[encoding]
if !ok {
split := strings.SplitN(encoding, "_", 2)
prefix := split[0]
if len(split) > 1 {
enc = split[1]
}
unmarshaler, ok = unmarshalers[prefix].(LogsUnmarshalerWithEnc)
if !ok {
return nil, errUnrecognizedEncoding
}
}
if unmarshalerWithEnc, ok := unmarshaler.(LogsUnmarshalerWithEnc); ok {
// This should be called even when enc is an empty string to initialize the encoding.
unmarshaler, err := unmarshalerWithEnc.WithEnc(enc)
if err != nil {
return nil, err
}
return unmarshaler, nil
}
return unmarshaler, nil
}