forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from Huawei-IRC-Cloud-Telemetry-Engineering/na…
…rcis/fix_todos allow the receiver to operate with delayed metrics
- Loading branch information
Showing
16 changed files
with
548 additions
and
164 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,88 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/huaweicloudcesreceiver/internal" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Generic function to make an API call with exponential backoff and context cancellation handling. | ||
func MakeAPICallWithRetry[T any]( | ||
ctx context.Context, | ||
shutdownChan chan struct{}, | ||
logger *zap.Logger, | ||
apiCall func() (*T, error), | ||
isThrottlingError func(error) bool, | ||
backOffConfig *backoff.ExponentialBackOff, | ||
) (*T, error) { | ||
// Immediately check for context cancellation or server shutdown. | ||
select { | ||
case <-ctx.Done(): | ||
return nil, fmt.Errorf("request was cancelled or timed out") | ||
case <-shutdownChan: | ||
return nil, fmt.Errorf("request is cancelled due to server shutdown") | ||
case <-time.After(50 * time.Millisecond): | ||
} | ||
|
||
// Make the initial API call. | ||
resp, err := apiCall() | ||
if err == nil { | ||
return resp, nil | ||
} | ||
|
||
// If the error is not due to request throttling, return the error. | ||
if !isThrottlingError(err) { | ||
return nil, err | ||
} | ||
|
||
// Initialize the backoff mechanism for retrying the API call. | ||
expBackoff := &backoff.ExponentialBackOff{ | ||
InitialInterval: backOffConfig.InitialInterval, | ||
RandomizationFactor: backOffConfig.RandomizationFactor, | ||
Multiplier: backOffConfig.Multiplier, | ||
MaxInterval: backOffConfig.MaxInterval, | ||
MaxElapsedTime: backOffConfig.MaxElapsedTime, | ||
Stop: backoff.Stop, | ||
Clock: backoff.SystemClock, | ||
} | ||
expBackoff.Reset() | ||
attempts := 0 | ||
|
||
// Retry loop for handling throttling errors. | ||
for { | ||
attempts++ | ||
delay := expBackoff.NextBackOff() | ||
if delay == backoff.Stop { | ||
return resp, err | ||
} | ||
logger.Warn("server busy, retrying request", | ||
zap.Int("attempts", attempts), | ||
zap.Duration("delay", delay)) | ||
|
||
// Handle context cancellation or shutdown before retrying. | ||
select { | ||
case <-ctx.Done(): | ||
return nil, fmt.Errorf("request was cancelled or timed out") | ||
case <-shutdownChan: | ||
return nil, fmt.Errorf("request is cancelled due to server shutdown") | ||
case <-time.After(delay): | ||
} | ||
|
||
// Retry the API call. | ||
resp, err = apiCall() | ||
if err == nil { | ||
return resp, nil | ||
} | ||
if !isThrottlingError(err) { | ||
break | ||
} | ||
} | ||
|
||
return nil, err | ||
} |
Oops, something went wrong.