Skip to content

Commit 3e32db2

Browse files
committed
add test
1 parent e1a97fc commit 3e32db2

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

pkg/backends/otlp/backend_test.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,13 @@ func TestBackendSendAsyncMetrics(t *testing.T) {
469469
func TestRetrySendMetrics(t *testing.T) {
470470
t.Parallel()
471471
for _, tc := range []struct {
472-
name string
473-
numUntilSuccess int
474-
maxRetries int
475-
wantAttempts int
476-
numErrs int
472+
name string
473+
numUntilSuccess int
474+
maxRetries int
475+
maxRequestElapseTime int
476+
wantAttempts int
477+
approxAttempts bool // because of randomness of the retry interval
478+
numErrs int
477479
}{
478480
{
479481
name: "should retry sending metrics if it fails for the first time",
@@ -503,6 +505,23 @@ func TestRetrySendMetrics(t *testing.T) {
503505
wantAttempts: 1,
504506
numErrs: 1,
505507
},
508+
{
509+
name: "should not retry if maxRetries reached",
510+
numUntilSuccess: 5,
511+
maxRetries: 3,
512+
maxRequestElapseTime: 100,
513+
wantAttempts: 4,
514+
numErrs: 1,
515+
},
516+
{
517+
name: "should stop retry if maxRequestElapseTime reached",
518+
numUntilSuccess: 5,
519+
maxRetries: 100,
520+
maxRequestElapseTime: 1,
521+
wantAttempts: 3,
522+
approxAttempts: true,
523+
numErrs: 1,
524+
},
506525
} {
507526
t.Run(tc.name, func(t *testing.T) {
508527
attempts := 0
@@ -520,6 +539,7 @@ func TestRetrySendMetrics(t *testing.T) {
520539
v.Set("otlp.metrics_endpoint", fmt.Sprintf("%s/%s", s.URL, "v1/metrics"))
521540
v.Set("otlp.logs_endpoint", fmt.Sprintf("%s/%s", s.URL, "v1/logs"))
522541
v.Set("otlp.max_retries", tc.maxRetries)
542+
v.Set("otlp.max_request_elapsed_time", tc.maxRequestElapseTime)
523543

524544
logger := fixtures.NewTestLogger(t)
525545

@@ -532,7 +552,12 @@ func TestRetrySendMetrics(t *testing.T) {
532552

533553
b.SendMetricsAsync(context.Background(), gostatsd.NewMetricMap(false), func(errs []error) {
534554
assert.Equal(t, tc.numErrs, len(errs))
535-
assert.Equal(t, tc.wantAttempts, attempts, "Must retry sending metrics")
555+
if tc.approxAttempts {
556+
assert.InDelta(t, tc.wantAttempts, attempts, 1, "Must retry sending metrics")
557+
} else {
558+
assert.Equal(t, tc.wantAttempts, attempts, "Must retry sending metrics")
559+
}
560+
536561
})
537562
})
538563
}

pkg/backends/otlp/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Config struct {
3030
// MaxRetries (Optional, default: 3) is the maximum number of retries to send a batch
3131
MaxRetries int `mapstructure:"max_retries"`
3232
// MaxRequestElapsedTime (Optional, default: 15) is the maximum time in seconds to wait for a request to complete
33+
// 0 means it never stop until reaches MaxRetries
3334
MaxRequestElapsedTime int `mapstructure:"max_request_elapsed_time"`
3435
// CompressPayload (Optional, default: true) is used to enable payload compression
3536
CompressPayload bool `mapstructure:"compress_payload"`
@@ -100,8 +101,8 @@ func (c *Config) Validate() (errs error) {
100101
if c.MaxRetries < 0 {
101102
errs = multierr.Append(errs, errors.New("max retries must be a positive value"))
102103
}
103-
if c.MaxRequestElapsedTime <= 0 {
104-
errs = multierr.Append(errs, errors.New("max request elapsed time must be a positive value"))
104+
if c.MaxRequestElapsedTime < 0 {
105+
errs = multierr.Append(errs, errors.New("max request elapsed time must >= 0"))
105106
}
106107
if c.MetricsPerBatch <= 0 {
107108
errs = multierr.Append(errs, errors.New("metrics per batch must be a positive value"))

0 commit comments

Comments
 (0)