forked from open-telemetry/opentelemetry-collector-contrib
/
factory.go
174 lines (160 loc) · 5.52 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:generate mdatagen metadata.yaml
package fkpulsarexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/pulsarexporter"
import (
"context"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)
const (
Type = "fkpulsar"
TracesStability = component.StabilityLevelAlpha
MetricsStability = component.StabilityLevelAlpha
LogsStability = component.StabilityLevelAlpha
)
const (
defaultTracesTopic = "otlp_spans"
defaultMetricsTopic = "otlp_metrics"
defaultLogsTopic = "otlp_logs"
defaultEncoding = "otlp_proto"
defaultBroker = "pulsar://localhost:6650"
)
// FactoryOption applies changes to pulsarExporterFactory.
type FactoryOption func(factory *pulsarExporterFactory)
// WithTracesMarshalers adds tracesMarshalers.
func WithTracesMarshalers(tracesMarshalers ...TracesMarshaler) FactoryOption {
return func(factory *pulsarExporterFactory) {
for _, marshaler := range tracesMarshalers {
factory.tracesMarshalers[marshaler.Encoding()] = marshaler
}
}
}
// NewFactory creates Pulsar exporter factory.
func NewFactory(options ...FactoryOption) exporter.Factory {
f := &pulsarExporterFactory{
tracesMarshalers: tracesMarshalers(),
metricsMarshalers: metricsMarshalers(),
logsMarshalers: logsMarshalers(),
}
for _, o := range options {
o(f)
}
return exporter.NewFactory(
Type,
createDefaultConfig,
exporter.WithTraces(f.createTracesExporter, TracesStability),
exporter.WithMetrics(f.createMetricsExporter, MetricsStability),
exporter.WithLogs(f.createLogsExporter, LogsStability),
)
}
func createDefaultConfig() component.Config {
return &Config{
TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(),
RetrySettings: exporterhelper.NewDefaultRetrySettings(),
QueueSettings: exporterhelper.NewDefaultQueueSettings(),
Endpoint: defaultBroker,
// using an empty topic to track when it has not been set by user, default is based on traces or metrics.
Topic: "",
Encoding: defaultEncoding,
Authentication: Authentication{},
MaxConnectionsPerBroker: 1,
ConnectionTimeout: 5 * time.Second,
OperationTimeout: 30 * time.Second,
}
}
type pulsarExporterFactory struct {
tracesMarshalers map[string]TracesMarshaler
metricsMarshalers map[string]MetricsMarshaler
logsMarshalers map[string]LogsMarshaler
}
func (f *pulsarExporterFactory) createTracesExporter(
ctx context.Context,
set exporter.CreateSettings,
cfg component.Config,
) (exporter.Traces, error) {
oCfg := *(cfg.(*Config))
if oCfg.Topic == "" {
oCfg.Topic = defaultTracesTopic
}
if oCfg.Encoding == "otlp_json" {
set.Logger.Info("otlp_json is considered experimental and should not be used in a production environment")
}
exp, err := newTracesExporter(oCfg, set, f.tracesMarshalers)
if err != nil {
return nil, err
}
return exporterhelper.NewTracesExporter(
ctx,
set,
cfg,
exp.tracesPusher,
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
// Disable exporterhelper Timeout, because we cannot pass a Context to the Producer,
// and will rely on the Pulsar Producer Timeout logic.
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
exporterhelper.WithRetry(oCfg.RetrySettings),
exporterhelper.WithQueue(oCfg.QueueSettings),
exporterhelper.WithShutdown(exp.Close))
}
func (f *pulsarExporterFactory) createMetricsExporter(
ctx context.Context,
set exporter.CreateSettings,
cfg component.Config,
) (exporter.Metrics, error) {
oCfg := *(cfg.(*Config))
if oCfg.Topic == "" {
oCfg.Topic = defaultMetricsTopic
}
if oCfg.Encoding == "otlp_json" {
set.Logger.Info("otlp_json is considered experimental and should not be used in a production environment")
}
exp, err := newMetricsExporter(oCfg, set, f.metricsMarshalers)
if err != nil {
return nil, err
}
return exporterhelper.NewMetricsExporter(
ctx,
set,
cfg,
exp.metricsDataPusher,
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
// Disable exporterhelper Timeout, because we cannot pass a Context to the Producer,
// and will rely on the sarama Pulsar Timeout logic.
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
exporterhelper.WithRetry(oCfg.RetrySettings),
exporterhelper.WithQueue(oCfg.QueueSettings),
exporterhelper.WithShutdown(exp.Close))
}
func (f *pulsarExporterFactory) createLogsExporter(
ctx context.Context,
set exporter.CreateSettings,
cfg component.Config,
) (exporter.Logs, error) {
oCfg := *(cfg.(*Config))
if oCfg.Topic == "" {
oCfg.Topic = defaultLogsTopic
}
if oCfg.Encoding == "otlp_json" {
set.Logger.Info("otlp_json is considered experimental and should not be used in a production environment")
}
exp, err := newLogsExporter(oCfg, set, f.logsMarshalers)
if err != nil {
return nil, err
}
return exporterhelper.NewLogsExporter(
ctx,
set,
cfg,
exp.logsDataPusher,
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
// Disable exporterhelper Timeout, because we cannot pass a Context to the Producer,
// and will rely on the Pulsar Producer Timeout logic.
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
exporterhelper.WithRetry(oCfg.RetrySettings),
exporterhelper.WithQueue(oCfg.QueueSettings),
exporterhelper.WithShutdown(exp.Close))
}