aws-powertools / powertools-lambda-dotnet

Powertools is a developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/dotnet/
MIT No Attribution
152 stars 24 forks source link

Feature request: Ability to not throw error on full batch failure #608

Open hjgraca opened 2 months ago

hjgraca commented 2 months ago

Use case

Currently when using the Batch Processor utility, if all the records in a batch are marked as failed the utility throws a BatchProcessingException.

Taking into consideration that the utility is supposed to be used with partial failure reporting, a Lambda function that throws an error is functionally equal to a partial failure that reports all items as failed in the sense that all the items in that batch are retried as a result.

While we initially implemented this as an error to reflect the full batch failure in the operational metrics (i.e. function runtime errors), there are cases such as when processing small batches that this behaviour can skew the metrics due to higher chances of a full batch to fail.

To accommodate these use cases, as well as those customers who simply want to avoid throwing an error, we should add a new policy RaiseOnBatchFailure (or similar) option to the utility that allows customers to opt out of the error throwing mechanism.

More information here: https://github.com/aws-powertools/powertools-lambda-typescript/issues/2122#issuecomment-1959176961

Solution/User Experience

public class CustomSqsRecordHandler : ISqsRecordHandler 
{
    public async Task<RecordHandlerResult> HandleAsync(SQSEvent.SQSMessage record, CancellationToken cancellationToken)
    {
         var product = JsonSerializer.Deserialize<Product>(record.Body);
         return await Task.FromResult(RecordHandlerResult.None); 
     }
}

[BatchProcessor(RecordHandler = typeof(CustomSqsRecordHandler), 
ErrorHandlingPolicy = BatchProcessorErrorHandlingPolicy.RaiseOnBatchFailure)]
public BatchItemFailuresResponse HandlerUsingAttribute(SQSEvent _)
{
    return SqsBatchProcessor.Result.BatchItemFailuresResponse; 
}

Alternative solutions

No response

Acknowledgment

lachriz-aws commented 2 weeks ago

I'm happy to take a look at this. Will try to set time aside in the comings weeks.

hjgraca commented 2 weeks ago

thank you @lachriz-aws