Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 33 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Tests

on:
pull_request:

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ["20"]

steps:
- name: Checkout code
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # https://github.com/actions/checkout/releases/tag/v4.1.1

- name: Set up Node.js ${{ matrix.node-version }}
# https://github.com/actions/setup-node/releases/tag/v4.0.1
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: |
npm ci

- name: Run linters
run: |
npm run lint

- name: Run tests
run: |
npm run test
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Dependency directories
/node_modules

# Build output
/dist

# IDE and editor files
/.vscode
/.idea
*.iml

# Compiled output
*.js
*.js.map
*.d.ts

# Test coverage
/coverage

# Environment variables
.env
.env.local
.env.*.local

# Log files
*.log

# OS generated files
.DS_Store
Thumbs.db
21 changes: 21 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Contributing to hathor-healthcheck-lib

Thank you for considering contributing to hathor-healthcheck-lib! This document outlines the guidelines for contributing to this project.

## Before You Start

1. Make sure you open an issue to discuss the changes you want to make before you start working on them. This will help you get feedback on your ideas and avoid wasting time on something that might not be merged.

## How to Contribute

1. Fork the repository and create your branch from `master`.
2. If you've added code that should be tested, add tests.
3. If you've changed APIs, update the documentation.
4. Ensure the test suite passes. You can run it with `npm run test`.
5. Make sure your code lints. You can run the linter with `npm run lint`. You can automatically format it with `npm run format`.
6. Submit a pull request.

## License

By contributing to @hathor/healthcheck-lib, you agree that your contributions will be licensed under the [MIT License](https://opensource.org/licenses/MIT).

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Hathor Network

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
115 changes: 115 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Hathor Healthcheck Lib

This is an opinionated library to help with structuring healthchecks in your javascript application.

It will not help you with the healthcheck logic itself, but it will help you build the response in a standard format.

We aim at supporting almost exactly the standard described in https://datatracker.ietf.org/doc/html/draft-inadarei-api-health-check. Although it's just a expired draft, it seems to be the better standard on healthchecks out there as of this writing.

This standard is not fully supported yet, see the [roadmap](#roadmap) for more information.

This is a sister project of [python-healthchecklib](https://github.com/HathorNetwork/python-healthcheck-lib)

## Installation

```bash
npm install @hathor/healthcheck-lib
```

## Concepts

The main concepts to keep in mind when using this library are the `service` and `components` concepts.

We consider a `service` to be the application as a whole. It's the thing that is running and that you want to check the health of.

We consider `components` to be the different parts of the application that you want to check the health of, being them internal or external. For example, if you have a web application, you might want to check the health of the database, the cache, internal components, etc.

You'll be responsible for implementing the healthcheck logic for each component, but this library will help you build the response in a standard format and define the health of the `service` based on the health of its `components`.

## Usage

```typescript
import { Healthcheck, HealthcheckDatastoreComponent, HealthcheckCallbackResponse } from 'hathor-healthcheck-lib';

// Create a healthcheck instance
healthcheck = new Healthcheck({
name: "My Service"
})

// Create a component
db_component = new HealthcheckDatastoreComponent({
name: "MySQL",
})

// Define the healthcheck logic for the component.
// They should be async functions that return a HealthcheckCallbackResponse
async function db_healthcheck() {
// Implement some logic to check the health of the database
return new HealthcheckCallbackResponse({
status: "pass",
output: "Database is healthy",
affects_service_health: true
})
}

// Add the healthcheck logic to the component
db_component.add_healthcheck(db_healthcheck)

// You can add more than one healthcheck to a component, which means that this is a component made of multiple instances.
async function db_healthcheck_2() {
// Implement some logic to check the health of the database
return new HealthcheckCallbackResponse({
status: "warn",
output: "Responsive but high latency",
affects_service_health: false
})

db_component.add_healthcheck(db_healthcheck_2)

// Add the component to the healthcheck
healthcheck.add_component(db_component)

// Get the health status of the service
status = await healthcheck.run()

// Print the status
console.log(status.toJson())

// {
// "status": "pass",
// "description": "Health status of My Service",
// "checks": {
// "MySQL": [
// {
// "status": "pass",
// "output": "Database is healthy",
// "componentType": "datastore",
// "componentName": "MySQL
// },
// {
// "status": "warn",
// "output": "Responsive but high latency",
// "componentType": "datastore",
// "componentName": "MySQL
// }
// ]
// }
// }
```

## Roadmap

- [ ] Support for manually setting the component health status (instead of passing a callback)
- [ ] Support for the optional fields in the service status described in https://datatracker.ietf.org/doc/html/draft-inadarei-api-health-check#name-status
- [ ] Support for the optional fields in the component status described in https://datatracker.ietf.org/doc/html/draft-inadarei-api-health-check#name-the-checks-object
- [ ] Support for defining customized keys for the component status object

We welcome contributions to help us achieve these goals. See below how to contribute.

## Contributing

See the [contributing guide](CONTRIBUTING.md) for more information.

## License

By contributing to hathor-healthcheck-lib, you agree that your contributions will be licensed under the [MIT License](https://opensource.org/licenses/MIT).
Loading