# @goodandco/nest-health

> Health package based for NestJS app based on @nestjs/terminus package

Latest version **2.0.2** (published 2023-08-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @goodandco/nest-health
pnpm add @goodandco/nest-health
yarn add @goodandco/nest-health
bun add @goodandco/nest-health
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2023-08-01 |
| First published | 2023-06-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 145 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Oleksandr Hudenko |
| Maintainers | goodandco |
| Keywords | nestjs, nest, terminus, health, nestjs health module |

## Links

- npm: https://www.npmjs.com/package/@goodandco/nest-health
- Repository: https://github.com/goodandco/nest-health
- Homepage: https://github.com/goodandco/nest-health#readme
- Issues: https://github.com/goodandco/nest-health/issues
- npm.io page: https://npm.io/package/@goodandco/nest-health

## Dependencies (9)

- [rxjs](https://npm.io/package/rxjs.md) ^7.8.1
- [@nestjs/core](https://npm.io/package/@nestjs/core.md) ^10.0.0
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^10.0.0
- [@nestjs/config](https://npm.io/package/@nestjs/config.md) ^3.0.0
- [@nestjs/typeorm](https://npm.io/package/@nestjs/typeorm.md) ^10.0.0
- [@nestjs/terminus](https://npm.io/package/@nestjs/terminus.md) ^10.0.1
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [@nestjs/microservices](https://npm.io/package/@nestjs/microservices.md) ^10.1.3
- [@nestjs/platform-express](https://npm.io/package/@nestjs/platform-express.md) ^10.0.0

## Recent versions

- 2.0.2 (latest) — 2023-08-01
- 2.0.1 — 2023-08-01
- 2.0.0 — 2023-08-01
- 1.0.0 — 2023-06-19

## README

## Installation

```bash
$ npm install @goodandco/nest-health
```
## Using

Health Module consists of controller for health check and specific configuration setup.

If you use yaml config module from `@goodnadco/nest-config` the configuration file for using this module
should look like this one:

```yaml
x-kafka-brokers: &KafkaBrokers
  - 'localhost:29092'

x-kafka-consumer-options: &KafkaConsumerOptions
  client:
    clientId: 'my-kafka-client'
    brokers: *KafkaBrokers
  consumer:
    groupId: 'my-kafka-consumer-group'


app:
  healthcheck:
    memoryHeap:
      key: 'memory_heap'
      options:
        heapUsedTreshhold: 209715200 # 200 * 1024 * 1024
        memoryRSS:
          key: 'memory_rss'
          options:
            rssTreshhold: 3145728000 # 3000 * 1024 * 1024
    microservices:
      - key: 'main kafka consumer'
        options:
          transport: 6
          options:
            client:
              clientId: 'health-consumer'
              brokers: *KafkaBrokers
            consumer:
              groupId: 'health-consumer'
            producerOnlyMode: true
    dbs:
      - key: 'mongodb'
        options:
          timeout: 300
```

For kafka, it needs to specify transport for kafka which is specifed in constant `Transport.KAFKA`.
Also the `producerOnlyMode` flag should be turned on.

In case of using yaml configuration loader your configuration will look like:

```typescript
import { HealthModule, THealthControllerOptions } from '@goodandco/health';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    ...
    ConfigModule.forRoot({ 
      load: ConfigLoader.load<TConfig>(),
      isGlobal: true,
    }),
    HealthModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) =>
        configService.get<THealthControllerOptions>('app.healthcheck'),
    }),
    ...
```

The simple configuration with `forRoot` method:

```typescript
@Module({
  imports: [
    ...
    HealthModule.forRoot({ 
      memoryHeap: {
        key: 'memory_heap',
        options: {
          heapUsedTreshhold: 209715200
        }
      }, 
      memoryRSS: {
        key: 'memory_rss',
        options: {
          rssTreshhold: 209715200
        }
      },
      dbs: [
        {
          key: 'mongodb',
          options: {
            timeout: 300
          }
        }
      ],
      microservices: [
        {
          key: 'main kafka consumer',
          options: {
            transport: Transport.KAFKA,
            options: {
              producerOnlyMode: true,
              client: {
                clientId: 'health-consumer',
                brokers: ['localhost:29092']
              },
              consumer: {
                 groupId: 'health-consumer'
              }
            }
          }
        }
      ]
    }),
    ...
```

Go to ```http://localhost:<PORT>/health``` and there should be:
```json
{
  "status": "ok",
  "info": {
    "memory_heap": {
      "status": "up"
    },
    "memory_rss": {
      "status": "up"
    },
    "mongodb": {
      "status": "up"
    },
    "kafka": {
      "status": "up"
    }
  },
  "error": {},
  "details": {
    "memory_heap": {
      "status": "up"
    },
    "memory_rss": {
      "status": "up"
    },
    "mongodb": {
      "status": "up"
    },
    "kafka": {
      "status": "up"
    }
  }
}
```

---
_Source: https://npm.io/package/@goodandco/nest-health · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
