-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
datadogexporter
: Retry per network call (#6412)
* [exporter/datadogexporter] Refactor metadata retrier to take RetrySettings into account * [exporter/datadogexporter] Do retries per network call on the metrics side * Address review comment * Fix lint
- Loading branch information
Showing
8 changed files
with
177 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package utils // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/utils" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"go.opentelemetry.io/collector/consumer/consumererror" | ||
"go.opentelemetry.io/collector/exporter/exporterhelper" | ||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/scrub" | ||
) | ||
|
||
type Retrier struct { | ||
cfg exporterhelper.RetrySettings | ||
logger *zap.Logger | ||
scrubber scrub.Scrubber | ||
} | ||
|
||
func NewRetrier(logger *zap.Logger, settings exporterhelper.RetrySettings, scrubber scrub.Scrubber) *Retrier { | ||
return &Retrier{ | ||
cfg: settings, | ||
logger: logger, | ||
scrubber: scrubber, | ||
} | ||
} | ||
|
||
// DoWithRetries does a function with retries. This is a condensed version of the code on | ||
// the exporterhelper, which we reuse here since we want custom retry logic. | ||
func (r *Retrier) DoWithRetries(ctx context.Context, fn func(context.Context) error) error { | ||
if !r.cfg.Enabled { | ||
return fn(ctx) | ||
} | ||
|
||
// 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: r.cfg.InitialInterval, | ||
RandomizationFactor: backoff.DefaultRandomizationFactor, | ||
Multiplier: backoff.DefaultMultiplier, | ||
MaxInterval: r.cfg.MaxInterval, | ||
MaxElapsedTime: r.cfg.MaxElapsedTime, | ||
Stop: backoff.Stop, | ||
Clock: backoff.SystemClock, | ||
} | ||
expBackoff.Reset() | ||
retryNum := int64(0) | ||
for { | ||
err := fn(ctx) | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
err = r.scrubber.Scrub(err) | ||
|
||
if consumererror.IsPermanent(err) { | ||
return err | ||
} | ||
|
||
backoffDelay := expBackoff.NextBackOff() | ||
if backoffDelay == backoff.Stop { | ||
err = fmt.Errorf("max elapsed time expired %w", err) | ||
return err | ||
} | ||
|
||
backoffDelayStr := backoffDelay.String() | ||
r.logger.Info( | ||
"Request 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 <-ctx.Done(): | ||
return fmt.Errorf("request is cancelled or timed out %w", err) | ||
case <-time.After(backoffDelay): | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package utils | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/exporter/exporterhelper" | ||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/scrub" | ||
) | ||
|
||
func TestDoWithRetries(t *testing.T) { | ||
scrubber := scrub.NewScrubber() | ||
retrier := NewRetrier(zap.NewNop(), exporterhelper.DefaultRetrySettings(), scrubber) | ||
ctx := context.Background() | ||
|
||
err := retrier.DoWithRetries(ctx, func(context.Context) error { return nil }) | ||
require.NoError(t, err) | ||
|
||
retrier = NewRetrier(zap.NewNop(), | ||
exporterhelper.RetrySettings{ | ||
Enabled: true, | ||
InitialInterval: 5 * time.Millisecond, | ||
MaxInterval: 30 * time.Millisecond, | ||
MaxElapsedTime: 100 * time.Millisecond, | ||
}, | ||
scrubber, | ||
) | ||
err = retrier.DoWithRetries(ctx, func(context.Context) error { return errors.New("action failed") }) | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters