Skip to content

Commit ddad4f3

Browse files
authored
Public backoff config to allow usage of WithAPIBackoff (#1895)
* public_backoff_config Signed-off-by: pipiland2612 <nguyen.t.dang.minh@gmail.com> * nit_fixing Signed-off-by: pipiland2612 <nguyen.t.dang.minh@gmail.com> --------- Signed-off-by: pipiland2612 <nguyen.t.dang.minh@gmail.com>
1 parent 187cb02 commit ddad4f3

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

exp/api/remote/remote_api.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ import (
3535
"github.com/prometheus/client_golang/exp/internal/github.com/efficientgo/core/backoff"
3636
)
3737

38+
// BackoffConfig configures exponential backoff with jitter for retry operations.
39+
type BackoffConfig struct {
40+
Min time.Duration `yaml:"min_period"` // Start backoff at this level
41+
Max time.Duration `yaml:"max_period"` // Increase exponentially to this level
42+
MaxRetries int `yaml:"max_retries"` // Give up after this many; zero means infinite retries
43+
}
44+
3845
// API is a client for Prometheus Remote Protocols.
3946
// NOTE(bwplotka): Only https://prometheus.io/docs/specs/remote_write_spec_2_0/ is currently implemented,
4047
// read protocols to be implemented if there will be a demand.
@@ -56,14 +63,14 @@ type RetryCallback func(err error)
5663
type apiOpts struct {
5764
logger *slog.Logger
5865
client *http.Client
59-
backoff backoff.Config
66+
backoffConfig BackoffConfig
6067
compression Compression
6168
path string
6269
retryOnRateLimit bool
6370
}
6471

6572
var defaultAPIOpts = &apiOpts{
66-
backoff: backoff.Config{
73+
backoffConfig: BackoffConfig{
6774
Min: 1 * time.Second,
6875
Max: 10 * time.Second,
6976
MaxRetries: 10,
@@ -107,10 +114,11 @@ func WithAPINoRetryOnRateLimit() APIOption {
107114
}
108115
}
109116

110-
// WithAPIBackoff returns APIOption that allows overriding backoff configuration.
111-
func WithAPIBackoff(backoff backoff.Config) APIOption {
117+
// WithAPIBackoff returns APIOption that allows configuring backoff.
118+
// By default, exponential backoff with jitter is used (see defaultAPIOpts).
119+
func WithAPIBackoff(cfg BackoffConfig) APIOption {
112120
return func(o *apiOpts) error {
113-
o.backoff = backoff
121+
o.backoffConfig = cfg
114122
return nil
115123
}
116124
}
@@ -259,7 +267,11 @@ func (r *API) Write(ctx context.Context, msgType WriteMessageType, msg any, opts
259267
// across the various attempts.
260268
accumulatedStats := WriteResponseStats{}
261269

262-
b := backoff.New(ctx, r.opts.backoff)
270+
b := backoff.New(ctx, backoff.Config{
271+
Min: r.opts.backoffConfig.Min,
272+
Max: r.opts.backoffConfig.Max,
273+
MaxRetries: r.opts.backoffConfig.MaxRetries,
274+
})
263275
for {
264276
rs, err := r.attemptWrite(ctx, r.opts.compression, msgType, payload, b.NumRetries())
265277
accumulatedStats.Add(rs)

exp/api/remote/remote_api_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"google.golang.org/protobuf/testing/protocmp"
3232

3333
writev2 "github.com/prometheus/client_golang/exp/api/remote/genproto/v2"
34-
"github.com/prometheus/client_golang/exp/internal/github.com/efficientgo/core/backoff"
3534
)
3635

3736
func TestRetryAfterDuration(t *testing.T) {
@@ -189,7 +188,7 @@ func TestRemoteAPI_Write_WithHandler(t *testing.T) {
189188
WithAPIHTTPClient(srv.Client()),
190189
WithAPILogger(tLogger),
191190
WithAPIPath("api/v1/write"),
192-
WithAPIBackoff(backoff.Config{
191+
WithAPIBackoff(BackoffConfig{
193192
Min: 1 * time.Second,
194193
Max: 1 * time.Second,
195194
MaxRetries: 2,
@@ -226,7 +225,7 @@ func TestRemoteAPI_Write_WithHandler(t *testing.T) {
226225
WithAPIHTTPClient(srv.Client()),
227226
WithAPILogger(tLogger),
228227
WithAPIPath("api/v1/write"),
229-
WithAPIBackoff(backoff.Config{
228+
WithAPIBackoff(BackoffConfig{
230229
Min: 1 * time.Millisecond,
231230
Max: 1 * time.Millisecond,
232231
MaxRetries: 3,

0 commit comments

Comments
 (0)