Skip to content

Commit

Permalink
Merge pull request #2013 from torredil/mw-handler-24123
Browse files Browse the repository at this point in the history
Consolidate request handling in RecordRequestsMiddleware
  • Loading branch information
k8s-ci-robot authored Apr 19, 2024
2 parents b977f78 + c943d37 commit 7df4dd6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
1 change: 0 additions & 1 deletion pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ func newEC2Cloud(region string, awsSdkDebugLog bool, userAgentExtra string, batc
svc := ec2.NewFromConfig(cfg, func(o *ec2.Options) {
o.APIOptions = append(o.APIOptions,
RecordRequestsMiddleware(),
RecordThrottledRequestsMiddleware(),
)

o.RetryMaxAttempts = retryMaxAttempt // Retry EC2 API calls at sdk level until request contexts are cancelled
Expand Down
37 changes: 14 additions & 23 deletions pkg/cloud/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,26 @@ func RecordRequestsMiddleware() func(*middleware.Stack) error {
start := time.Now()
output, metadata, err = next.HandleFinalize(ctx, input)
labels := createLabels(ctx)
if err != nil {
metrics.Recorder().IncreaseCount("cloudprovider_aws_api_request_errors", labels)
} else {
duration := time.Since(start).Seconds()
metrics.Recorder().ObserveHistogram("cloudprovider_aws_api_request_duration_seconds", duration, labels, nil)
}
return output, metadata, err
}), middleware.Before)
}
}

// RecordThrottledRequestsHandler is added to the AfterRetry chain; called after any error
func RecordThrottledRequestsMiddleware() func(*middleware.Stack) error {
return func(stack *middleware.Stack) error {
return stack.Finalize.Add(middleware.FinalizeMiddlewareFunc("RecordThrottledRequestsMiddleware", func(ctx context.Context, input middleware.FinalizeInput, next middleware.FinalizeHandler) (output middleware.FinalizeOutput, metadata middleware.Metadata, err error) {
output, metadata, err = next.HandleFinalize(ctx, input)
if err != nil {
var apiErr smithy.APIError
if errors.As(err, &apiErr) && apiErr.ErrorCode() == requestLimitExceededErrorCode {
operationName := awsmiddleware.GetOperationName(ctx)
labels := map[string]string{
"operation_name": operationName,
if errors.As(err, &apiErr) {
if apiErr.ErrorCode() == requestLimitExceededErrorCode {
operationName := awsmiddleware.GetOperationName(ctx)
labels = map[string]string{
"operation_name": operationName,
}
metrics.Recorder().IncreaseCount("cloudprovider_aws_api_throttled_requests_total", labels)
klog.InfoS("Got RequestLimitExceeded error on AWS request", "request", operationName)
} else {
metrics.Recorder().IncreaseCount("cloudprovider_aws_api_request_errors", labels)
}
metrics.Recorder().IncreaseCount("cloudprovider_aws_api_throttled_requests_total", labels)
klog.InfoS("Got RequestLimitExceeded error on AWS request", "request", operationName)
}
} else {
duration := time.Since(start).Seconds()
metrics.Recorder().ObserveHistogram("cloudprovider_aws_api_request_duration_seconds", duration, labels, nil)
}
return output, metadata, err
}), middleware.Before)
}), middleware.After)
}
}

Expand Down

0 comments on commit 7df4dd6

Please sign in to comment.