Skip to content
This repository was archived by the owner on Jan 7, 2019. It is now read-only.
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions ops/health-check-route.s.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# [Standard] Writing a health check route

## Owner: Arthur Levoyer

# Why

In order to monitor correctly their environments, backend services require a HealthCheck route which returns a status depending on how the API is running.

## Checks

- [ ] Make a call to every database used by the API
- [ ] Send a 2xx status code status in case of API running correctly and 5xx status code if not
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Send a 2xx status code status if the API is running correctly, 5xx status code if not

- [ ] Make the less data usage database calls: the health check route is likely to be called very often in short period of time
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make database calls retrieving as little data as possible


## Examples

In the examples below the API is concentrating calls to one database RDS and one DynamoDB
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concentrating? Did you mean making?


### Example 1: Bad example

```javascript
app.get("/health-check", (req, res) => {
res.status(200).send({ status: "OK" });
});
```

- There is no call to any of the two databases
- There is no 5xx status code if the API is not running

### Example 2: Bad example

```javascript
app.get("/health", async (req, res) => {
try {
await RDS.getAllEntries();
res.status(200).send({ status: "OK" });
} catch (error) {
res.status(503).send({ status: "KO" });
}
});
```

- There is a call to one of the database but not the other
- The call is using too much data
- There is a 503 if the DB is down
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain why 503


### Example 3: Good example

```javascript
app.get("/health", async (req, res) => {
try {
await Promise.all([DynamoDB.getDynamoHealth(), RDS.getHealth()]);
res.status(200).send({ status: 200, message: "OK" });
} catch (error) {
handleError(res, error, { status: 503, error: error.message, message: "KO" });
}
});

RDS.getHealth = async () => {
await knex.raw("select 1+1 as result");
};

DynamoDB.getDynamoHealth = (): Promise<Array<Object>> => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe remove typing as it is not used anywhere else

return new Promise((resolve, reject) => {
dynamodb.describeTable({ TableName: dynamodbTableName }, (error, data) => {
if (error) {
return reject(error);
}
resolve(data);
});
});
};
```