This proposal is for solving crowdsecurity/cs-firewall-bouncer#166 in a generalized manner.
Currently the clients of StreamBouncer have no way to handle the errors on LAPI calls .
We could solve this specific issue by tracking LAPI call errors via prometheus counter.
However we want to keep the code flexible enough to do other types of error handling.
One way of doing this would be to have add an error handler function as a field to the StreamBouncer struct.
type StreamBouncer struct {
APIKey string `yaml:"api_key"`
........
........
LAPIErrorHandler func(err error)
........
........Then upon encountering an error when making LAPI call, the error would be passed to the LAPIErrorHandler function. The code in Run() method would look like
data, resp, err := b.APIClient.Decisions.GetStream(context.Background(), b.Opts)
if err != nil {
if resp != nil && resp.Response != nil {
resp.Response.Body.Close()
}
b.LAPIErrorHandler(err)
continue
}The clients of StreamBouncer, in this case the firewall bouncer then would pass an error handler like:
var TotalLAPIError prometheus.Counter = promauto.NewCounter(prometheus.CounterOpts{
Name: "lapi_call_failures",
Help: "The total number of failed calls to CrowdSec LAPI",
},
)
prometheusErrorTracker := func(err error) {
TotalLAPIError.Inc()
log.Errorf(err.Error())
}
bouncer := &csbouncer.StreamBouncer{LAPIErrorHandler: prometheusErrorTracker}
..... // Do config + Init stuff
.....If the clients of StreamBouncer don't pass an error handler, we will set the value ofLAPIErrorHandler to a default error handler, which would just log the error. This would be checked in the Init() method of StreamBouncer