-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
retry_sender.go
201 lines (177 loc) · 6.33 KB
/
retry_sender.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package exporterhelper // import "go.opentelemetry.io/collector/exporter/exporterhelper"
import (
"errors"
"fmt"
"time"
"github.com/cenkalti/backoff/v4"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/exporter/exporterhelper/internal"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
)
// RetrySettings defines configuration for retrying batches in case of export failure.
// The current supported strategy is exponential backoff.
type RetrySettings struct {
// Enabled indicates whether to not retry sending batches in case of export failure.
Enabled bool `mapstructure:"enabled"`
// InitialInterval the time to wait after the first failure before retrying.
InitialInterval time.Duration `mapstructure:"initial_interval"`
// RandomizationFactor is a random factor used to calculate next backoffs
// Randomized interval = RetryInterval * (1 ± RandomizationFactor)
RandomizationFactor float64 `mapstructure:"randomization_factor"`
// Multiplier is the value multiplied by the backoff interval bounds
Multiplier float64 `mapstructure:"multiplier"`
// MaxInterval is the upper bound on backoff interval. Once this value is reached the delay between
// consecutive retries will always be `MaxInterval`.
MaxInterval time.Duration `mapstructure:"max_interval"`
// MaxElapsedTime is the maximum amount of time (including retries) spent trying to send a request/batch.
// Once this value is reached, the data is discarded.
MaxElapsedTime time.Duration `mapstructure:"max_elapsed_time"`
}
// NewDefaultRetrySettings returns the default settings for RetrySettings.
func NewDefaultRetrySettings() RetrySettings {
return RetrySettings{
Enabled: true,
InitialInterval: 5 * time.Second,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: backoff.DefaultMultiplier,
MaxInterval: 30 * time.Second,
MaxElapsedTime: 5 * time.Minute,
}
}
// TODO: Clean this by forcing all exporters to return an internal error type that always include the information about retries.
type throttleRetry struct {
err error
delay time.Duration
}
func (t throttleRetry) Error() string {
return "Throttle (" + t.delay.String() + "), error: " + t.err.Error()
}
func (t throttleRetry) Unwrap() error {
return t.err
}
// NewThrottleRetry creates a new throttle retry error.
func NewThrottleRetry(err error, delay time.Duration) error {
return throttleRetry{
err: err,
delay: delay,
}
}
type onRequestHandlingFinishedFunc func(*zap.Logger, internal.Request, error) error
type retrySender struct {
baseRequestSender
traceAttribute attribute.KeyValue
cfg RetrySettings
stopCh chan struct{}
logger *zap.Logger
onTemporaryFailure onRequestHandlingFinishedFunc
}
func newRetrySender(id component.ID, rCfg RetrySettings, logger *zap.Logger, onTemporaryFailure onRequestHandlingFinishedFunc) *retrySender {
if onTemporaryFailure == nil {
onTemporaryFailure = func(logger *zap.Logger, req internal.Request, err error) error {
return err
}
}
return &retrySender{
traceAttribute: attribute.String(obsmetrics.ExporterKey, id.String()),
cfg: rCfg,
stopCh: make(chan struct{}),
logger: logger,
onTemporaryFailure: onTemporaryFailure,
}
}
func (rs *retrySender) shutdown() {
close(rs.stopCh)
}
// send implements the requestSender interface
func (rs *retrySender) send(req internal.Request) error {
if !rs.cfg.Enabled {
err := rs.nextSender.send(req)
if err != nil {
rs.logger.Error(
"Exporting failed. Try enabling retry_on_failure config option to retry on retryable errors",
zap.Error(err),
)
}
return err
}
// Do not use NewExponentialBackOff since it calls Reset and the code here must
// call Reset after changing the InitialInterval (this saves an unnecessary call to Now).
expBackoff := backoff.ExponentialBackOff{
InitialInterval: rs.cfg.InitialInterval,
RandomizationFactor: rs.cfg.RandomizationFactor,
Multiplier: rs.cfg.Multiplier,
MaxInterval: rs.cfg.MaxInterval,
MaxElapsedTime: rs.cfg.MaxElapsedTime,
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}
expBackoff.Reset()
span := trace.SpanFromContext(req.Context())
retryNum := int64(0)
for {
span.AddEvent(
"Sending request.",
trace.WithAttributes(rs.traceAttribute, attribute.Int64("retry_num", retryNum)))
err := rs.nextSender.send(req)
if err == nil {
return nil
}
// Immediately drop data on permanent errors.
if consumererror.IsPermanent(err) {
rs.logger.Error(
"Exporting failed. The error is not retryable. Dropping data.",
zap.Error(err),
zap.Int("dropped_items", req.Count()),
)
return err
}
// Give the request a chance to extract signal data to retry if only some data
// failed to process.
req = req.OnError(err)
backoffDelay := expBackoff.NextBackOff()
if backoffDelay == backoff.Stop {
// throw away the batch
err = fmt.Errorf("max elapsed time expired %w", err)
return rs.onTemporaryFailure(rs.logger, req, err)
}
throttleErr := throttleRetry{}
isThrottle := errors.As(err, &throttleErr)
if isThrottle {
backoffDelay = max(backoffDelay, throttleErr.delay)
}
backoffDelayStr := backoffDelay.String()
span.AddEvent(
"Exporting failed. Will retry the request after interval.",
trace.WithAttributes(
rs.traceAttribute,
attribute.String("interval", backoffDelayStr),
attribute.String("error", err.Error())))
rs.logger.Info(
"Exporting failed. Will retry the request after interval.",
zap.Error(err),
zap.String("interval", backoffDelayStr),
)
retryNum++
// back-off, but get interrupted when shutting down or request is cancelled or timed out.
select {
case <-req.Context().Done():
return fmt.Errorf("Request is cancelled or timed out %w", err)
case <-rs.stopCh:
return rs.onTemporaryFailure(rs.logger, req, fmt.Errorf("interrupted due to shutdown %w", err))
case <-time.After(backoffDelay):
}
}
}
// max returns the larger of x or y.
func max(x, y time.Duration) time.Duration {
if x < y {
return y
}
return x
}