|
| 1 | +# [Standard] Writing a health check route |
| 2 | + |
| 3 | +## Owner: Arthur Levoyer |
| 4 | + |
| 5 | +# Why |
| 6 | + |
| 7 | +In order to monitor correctly their environments, some Cloud services require a HealthCheck route which returns a status depending on how the API is running. |
| 8 | + |
| 9 | +## Checks |
| 10 | + |
| 11 | +- [ ] Do a call to every DB used by the API |
| 12 | +- [ ] Send a 2xx status code status in case of API running correctly and 5xx status code if not |
| 13 | +- [ ] Do the less data usage DB calls |
| 14 | +- [ ] Include a timestamp in order not to reduce the number of successive calls |
| 15 | + |
| 16 | +## Examples |
| 17 | + |
| 18 | +In the examples below the API is concentrating calls to one database RDS and one DynamoDB |
| 19 | + |
| 20 | +### Example 1: Bad example |
| 21 | + |
| 22 | +```javascript |
| 23 | +app.get("/health-check", (req, res) => { |
| 24 | + res.status(200).send({ status: "OK" }); |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +- There is no call to any of the two databases |
| 29 | +- There is no 5xx status code if the API is not running |
| 30 | +- There is not timestamp hence the route may be called successively every seconds |
| 31 | + |
| 32 | +### Example 2: Bad example |
| 33 | + |
| 34 | +```javascript |
| 35 | +app.get("/health", async (req, res) => { |
| 36 | + try { |
| 37 | + await findAllDataCollectors(); // DataCollectors ~ 100 entries |
| 38 | + res.status(200).send({ status: "OK" }); |
| 39 | + } catch (error) { |
| 40 | + res.status(503).send({ status: "KO" }); |
| 41 | + } |
| 42 | +}); |
| 43 | +``` |
| 44 | + |
| 45 | +- There is a call to one of the database but not the other |
| 46 | +- The call is using too much data |
| 47 | +- There is no timestamp |
| 48 | +- There is a 503 if the DB is down |
| 49 | + |
| 50 | +### Example 3: Good example |
| 51 | + |
| 52 | +```javascript |
| 53 | +app.get("/health", async (req, res) => { |
| 54 | + try { |
| 55 | + if (nextFetchingDate && nextFetchingDate > Date.now()) { |
| 56 | + return res.status(200).send({ status: 200, message: "OK" }); |
| 57 | + } |
| 58 | + |
| 59 | + nextFetchingDate = Date.now() + TIME_CHECKING_INTERVAL; |
| 60 | + await Promise.all([AppsDynamoRepo.getDynamoHealth(), getHealth()]); |
| 61 | + |
| 62 | + res.status(200).send({ status: 200, message: "OK" }); |
| 63 | + } catch (error) { |
| 64 | + nextFetchingDate = Date.now(); |
| 65 | + handleError(res, error, { status: 503, error: error.message, message: "KO" }); |
| 66 | + } |
| 67 | +}); |
| 68 | +``` |
0 commit comments