Debian Codegraph @

exporter/exporterhelper/internal/base_exporter.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internal // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal"
5
6 import (
7 "context"
8 "errors"
9
10 "go.uber.org/multierr"
11 "go.uber.org/zap"
12
13 "go.opentelemetry.io/collector/component"
14 "go.opentelemetry.io/collector/config/configretry"
15 "go.opentelemetry.io/collector/consumer"
16 "go.opentelemetry.io/collector/exporter"
17 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/queuebatch"
18 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/request"
19 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/sender"
20 "go.opentelemetry.io/collector/pipeline"
21 )
22
23 // Option apply changes to BaseExporter.
24 type Option func(*BaseExporter) error
25
26 // BaseExporter contains common fields between different exporter types.
27 type BaseExporter struct {
28 component.StartFunc
29 component.ShutdownFunc
30
31 Set exporter.Settings
32
33 // Message for the user to be added with an export failure message.
34 ExportFailureMessage string
35
36 // Chain of senders that the exporter helper applies before passing the data to the actual exporter.
37 // The data is handled by each sender in the respective order starting from the QueueBatch.
38 // Most of the senders are optional, and initialized with a no-op path-through sender.
39 QueueSender sender.Sender[request.Request]
40 RetrySender sender.Sender[request.Request]
41
42 firstSender sender.Sender[request.Request]
43
44 ConsumerOptions []consumer.Option
45
46 timeoutCfg TimeoutConfig
47 retryCfg configretry.BackOffConfig
48
49 queueBatchSettings queuebatch.Settings[request.Request]
50 queueCfg queuebatch.Config
51 }
52
53 func NewBaseExporter(set exporter.Settings, signal pipeline.Signal, pusher sender.SendFunc[request.Request], options ...Option) (*BaseExporter, error) {
54 be := &BaseExporter{
55 Set: set,
56 timeoutCfg: NewDefaultTimeoutConfig(),
57 }
58
59 for _, op := range options {
60 if err := op(be); err != nil {
61 return nil, err
62 }
63 }
64
65 // Consumer Sender is always initialized.
66 be.firstSender = sender.NewSender(pusher)
67
68 // Next setup the timeout Sender since we want the timeout to control only the export functionality.
69 // Only initialize if not explicitly disabled.
70 if be.timeoutCfg.Timeout != 0 {
71 be.firstSender = newTimeoutSender(be.timeoutCfg, be.firstSender)
72 }
73
74 if be.retryCfg.Enabled {
75 be.RetrySender = newRetrySender(be.retryCfg, set, be.firstSender)
76 be.firstSender = be.RetrySender
77 }
78
79 var err error
80 be.firstSender, err = newObsReportSender(set, signal, be.firstSender)
81 if err != nil {
82 return nil, err
83 }
84
85 if be.queueCfg.Batch.HasValue() {
86 // Batcher mutates the data.
87 be.ConsumerOptions = append(be.ConsumerOptions, consumer.WithCapabilities(consumer.Capabilities{MutatesData: true}))
88 }
89
90 if be.queueCfg.Enabled {
91 qSet := queuebatch.AllSettings[request.Request]{
92 Settings: be.queueBatchSettings,
93 Signal: signal,
94 ID: set.ID,
95 Telemetry: set.TelemetrySettings,
96 }
97 be.QueueSender, err = NewQueueSender(qSet, be.queueCfg, be.ExportFailureMessage, be.firstSender)
98 if err != nil {
99 return nil, err
100 }
101 be.firstSender = be.QueueSender
102 }
103
104 return be, nil
105 }
106
107 // Send sends the request using the first sender in the chain.
108 func (be *BaseExporter) Send(ctx context.Context, req request.Request) error {
109 // Have to read the number of items before sending the request since the request can
110 // be modified by the downstream components like the batcher.
111 itemsCount := req.ItemsCount()
112 err := be.firstSender.Send(ctx, req)
113 if err != nil {
114 be.Set.Logger.Error("Exporting failed. Rejecting data."+be.ExportFailureMessage,
115 zap.Error(err), zap.Int("rejected_items", itemsCount))
116 }
117 return err
118 }
119
120 func (be *BaseExporter) Start(ctx context.Context, host component.Host) error {
121 // First start the wrapped exporter.
122 if err := be.StartFunc.Start(ctx, host); err != nil {
123 return err
124 }
125
126 // Last start the QueueBatch.
127 if be.QueueSender != nil {
128 return be.QueueSender.Start(ctx, host)
129 }
130
131 return nil
132 }
133
134 func (be *BaseExporter) Shutdown(ctx context.Context) error {
135 var err error
136
137 // First shutdown the retry sender, so the queue sender can flush the queue without retries.
138 if be.RetrySender != nil {
139 err = multierr.Append(err, be.RetrySender.Shutdown(ctx))
140 }
141
142 // Then shutdown the queue sender.
143 if be.QueueSender != nil {
144 err = multierr.Append(err, be.QueueSender.Shutdown(ctx))
145 }
146
147 // Last shutdown the wrapped exporter itself.
148 return multierr.Append(err, be.ShutdownFunc.Shutdown(ctx))
149 }
150
151 // WithStart overrides the default Start function for an exporter.
152 // The default start function does nothing and always returns nil.
153 func WithStart(start component.StartFunc) Option {
154 return func(o *BaseExporter) error {
155 o.StartFunc = start
156 return nil
157 }
158 }
159
160 // WithShutdown overrides the default Shutdown function for an exporter.
161 // The default shutdown function does nothing and always returns nil.
162 func WithShutdown(shutdown component.ShutdownFunc) Option {
163 return func(o *BaseExporter) error {
164 o.ShutdownFunc = shutdown
165 return nil
166 }
167 }
168
169 // WithTimeout overrides the default TimeoutConfig for an exporter.
170 // The default TimeoutConfig is 5 seconds.
171 func WithTimeout(timeoutConfig TimeoutConfig) Option {
172 return func(o *BaseExporter) error {
173 o.timeoutCfg = timeoutConfig
174 return nil
175 }
176 }
177
178 // WithRetry overrides the default configretry.BackOffConfig for an exporter.
179 // The default configretry.BackOffConfig is to disable retries.
180 func WithRetry(config configretry.BackOffConfig) Option {
181 return func(o *BaseExporter) error {
182 if !config.Enabled {
183 o.ExportFailureMessage += " Try enabling retry_on_failure config option to retry on retryable errors."
184 return nil
185 }
186 o.retryCfg = config
187 return nil
188 }
189 }
190
191 // WithQueue overrides the default queuebatch.Config for an exporter.
192 // The default queuebatch.Config is to disable queueing.
193 // This option cannot be used with the new exporter helpers New[Traces|Metrics|Logs]RequestExporter.
194 func WithQueue(cfg queuebatch.Config) Option {
195 return func(o *BaseExporter) error {
196 if o.queueBatchSettings.Encoding == nil {
197 return errors.New("WithQueue option is not available for the new request exporters, use WithQueueBatch instead")
198 }
199 return WithQueueBatch(cfg, o.queueBatchSettings)(o)
200 }
201 }
202
203 // WithQueueBatch enables queueing for an exporter.
204 // This option should be used with the new exporter helpers New[Traces|Metrics|Logs]RequestExporter.
205 // Experimental: This API is at the early stage of development and may change without backward compatibility
206 // until https://github.com/open-telemetry/opentelemetry-collector/issues/8122 is resolved.
207 func WithQueueBatch(cfg queuebatch.Config, set queuebatch.Settings[request.Request]) Option {
208 return func(o *BaseExporter) error {
209 if !cfg.Enabled {
210 o.ExportFailureMessage += " Try enabling sending_queue to survive temporary failures."
211 return nil
212 }
213 if cfg.StorageID != nil && set.Encoding == nil {
214 return errors.New("`Settings.Encoding` must not be nil when persistent queue is enabled")
215 }
216 o.queueBatchSettings = set
217 o.queueCfg = cfg
218 return nil
219 }
220 }
221
222 // WithCapabilities overrides the default Capabilities() function for a Consumer.
223 // The default is non-mutable data.
224 // TODO: Verify if we can change the default to be mutable as we do for processors.
225 func WithCapabilities(capabilities consumer.Capabilities) Option {
226 return func(o *BaseExporter) error {
227 o.ConsumerOptions = append(o.ConsumerOptions, consumer.WithCapabilities(capabilities))
228 return nil
229 }
230 }
231
232 // WithQueueBatchSettings is used to set the queuebatch.Settings for the new request based exporter helper.
233 // It must be provided as the first option when creating a new exporter helper.
234 func WithQueueBatchSettings(set queuebatch.Settings[request.Request]) Option {
235 return func(o *BaseExporter) error {
236 o.queueBatchSettings = set
237 return nil
238 }
239 }