Handling ThrottlingException in Cloudwatch Goland SDK
I've been recently working on a new cli tool that was helping our Teams to subscribe cloudwatch log groups to a specific lambda that forwards logs to Datadog. However, when a project has big number of lambda's, we were facing with ThrottlingException
from cloudwatch. This allowed me to learn how to handle specific exception in Goland and i decided to share it with everyone.
Let's start with client itself:
isThrottlingError
method defines if error is the one we need- each method is accepting
cloudwatchClient cloudwatchlogsiface.CloudWatchLogsAPI
- we need this for writing unit tests for our methods
Of course we can't leave this code without unit tests. In order to check all the cases, we need to create mock first:
After that we can write all the unit tests to verify functions behaviour:
And this is how we can write cloudwatch client that will retry on Throttling exception and will do the job :)
Happy coding everyone :)