Stop elasticsearch_dsl from sending events to Elastic when server is down

We have a django web application that sends user data with elasticsearch_dsl library to an APM server. Our Elastic Stack is mounted with docker-compose, with Kibana, Elasticsearch and APM nodes.

For every user interaction, django tracking analyzer creates the corresponding instance in our database, and signals elasticsearch_dsl to synchronize data with APM server.

What we have experienced is that, in the case of APM server down, the server cannot respond and the nginx timeout is triggered:

upstream prematurely closed connection while reading response header from upstream

Is there a way to avoid sending elasticsearch_dsl signals when the server is down? How can we detect the server availability to avoid sending this request to APM?