Skip to content

Commit 5a462af

Browse files
committed
docs(terminus): add disk, memory and http.responseCheck
1 parent d15ab28 commit 5a462af

File tree

1 file changed

+162
-6
lines changed

1 file changed

+162
-6
lines changed

content/recipes/terminus.md

Lines changed: 162 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class HealthController {
102102
@HealthCheck()
103103
healthCheck() {
104104
return this.health.check([
105-
async () => this.http.pingCheck('nestjs-docs', 'https://docs.nestjs.com'),
105+
() => this.http.pingCheck('nestjs-docs', 'https://docs.nestjs.com'),
106106
])
107107
}
108108
}
@@ -163,6 +163,36 @@ The interface of this response object can be accessed from the `@nestjs/terminus
163163
| `error` | Object containing information of each health indicator which is of status `'down'`, or in other words "unhealthy". | `object` |
164164
| `details` | Object containing all information of each health indicator | `object` |
165165

166+
##### Check for specific HTTP response codes
167+
168+
In certain cases, you might want to check for specific criteria and validate the response. As an example, let's assume
169+
`https://my-external-service.com` returns a response code `204`. With `HttpHealthIndicator.responseCheck` you can
170+
check for that response code specifically and determine all other codes as unhealthy.
171+
172+
In case any other response code other than `204` gets returned, the following example would be unhealthy. The third parameter
173+
requires you to provide a function (sync or async) which returns a boolean whether the response is considered
174+
healthy `true` or unhealthy `false`.
175+
176+
177+
```typescript
178+
@@filename(health.controller)
179+
// Within the `HealthController`-class
180+
181+
@Get()
182+
@HealthCheck()
183+
check() {
184+
return this.health.check([
185+
() =>
186+
this.http.responseCheck(
187+
'my-external-service',
188+
'https://my-external-service.com',
189+
(res) => res.status === 204,
190+
),
191+
]);
192+
}
193+
```
194+
195+
166196
#### TypeOrm health indicator
167197

168198
Terminus offers the capability to add database checks to your health check. In order to get started with this health indicator, you
@@ -200,7 +230,7 @@ export class HealthController {
200230
@HealthCheck()
201231
healthCheck() {
202232
return this.health.check([
203-
async () => this.db.pingCheck('database'),
233+
() => this.db.pingCheck('database'),
204234
])
205235
}
206236
}
@@ -252,6 +282,132 @@ export class HealthController {
252282
}
253283
```
254284

285+
286+
#### Disk health indicator
287+
288+
With the `DiskHealthIndicator` we can check how much storage is in use. To get started, make sure to inject the `DiskHealthIndicator`
289+
into your `HealthController`. The following example checks the storage used of the path `/` (or on Windows you can use `C:\\`).
290+
If that exceeds more than 50% of the total storage space it would response with an unhealthy Health Check.
291+
292+
```typescript
293+
@@filename(health.controller)
294+
@Controller('health')
295+
export class HealthController {
296+
constructor(
297+
private health: HealthCheckService,
298+
private disk: DiskHealthIndicator,
299+
) {}
300+
301+
@Get()
302+
@HealthCheck()
303+
check() {
304+
return this.health.check([
305+
() => this.disk.checkStorage('storage', { path: '/', thresholdPercent: 0.5 }),
306+
]);
307+
}
308+
}
309+
@@switch
310+
@Controller('health')
311+
@Dependencies(HealthCheckService, DiskHealthIndicator)
312+
export class HealthController {
313+
constructor(
314+
private health,
315+
private disk,
316+
) { }
317+
318+
@Get()
319+
@HealthCheck()
320+
healthCheck() {
321+
return this.health.check([
322+
() => this.disk.checkStorage('storage', { path: '/', thresholdPercent: 0.5 }),
323+
])
324+
}
325+
}
326+
```
327+
328+
With the `DiskHealthIndicator.checkStorage` function you also have the possibility to check for a fixed amount of space.
329+
The following example would be unhealthy in case the path `/my-app/` would exceed 250GB.
330+
331+
```typescript
332+
@@filename(health.controller)
333+
// Within the `HealthController`-class
334+
335+
@Get()
336+
@HealthCheck()
337+
check() {
338+
return this.health.check([
339+
() => this.disk.checkStorage('storage', { path: '/', threshold: 250 * 1024 * 1024 * 1024, })
340+
]);
341+
}
342+
```
343+
344+
#### Memory health indicator
345+
346+
To make sure your process does not exceed a certain memory limit the `MemoryHealthIndicator` can be used.
347+
The following example can be used to check the heap of your process.
348+
349+
> info **Hint** Heap is the portion of memory where dynamically allocated memory resides (i.e. memory allocated via malloc). Memory allocated from the heap will remain allocated until one of the following occurs:
350+
> - The memory is _free_'d
351+
> - The program terminates
352+
353+
```typescript
354+
@@filename(health.controller)
355+
@Controller('health')
356+
export class HealthController {
357+
constructor(
358+
private health: HealthCheckService,
359+
private memory: MemoryHealthIndicator,
360+
) {}
361+
362+
@Get()
363+
@HealthCheck()
364+
check() {
365+
return this.health.check([
366+
() => this.memory.checkHeap('memory_heap', 150 * 1024 * 1024),
367+
]);
368+
}
369+
}
370+
@@switch
371+
@Controller('health')
372+
@Dependencies(HealthCheckService, MemoryHealthIndicator)
373+
export class HealthController {
374+
constructor(
375+
private health,
376+
private memory,
377+
) { }
378+
379+
@Get()
380+
@HealthCheck()
381+
healthCheck() {
382+
return this.health.check([
383+
() => this.memory.checkHeap('memory_heap', 150 * 1024 * 1024),
384+
])
385+
}
386+
}
387+
```
388+
389+
It is also possible to verify the memory RSS of your process with `MemoryHealthIndicator.checkRSS`. This example
390+
would return an unhealthy response code in case your process does have more than 150MB allocated.
391+
392+
> info **Hint** RSS is the Resident Set Size and is used to show how much memory is allocated to that process and is in RAM.
393+
> It does not include memory that is swapped out. It does include memory from shared libraries as long as the pages from
394+
> those libraries are actually in memory. It does include all stack and heap memory.
395+
396+
397+
```typescript
398+
@@filename(health.controller)
399+
// Within the `HealthController`-class
400+
401+
@Get()
402+
@HealthCheck()
403+
check() {
404+
return this.health.check([
405+
() => this.memory.checkRSS('memory_rss', 150 * 1024 * 1024),
406+
]);
407+
}
408+
```
409+
410+
255411
#### Custom health indicator
256412

257413
In some cases, the predefined health indicators provided by `@nestjs/terminus` do not cover all of your health check requirements. In that case, you can set up a custom health indicator according to your needs.
@@ -347,7 +503,7 @@ export class HealthController {
347503
@HealthCheck()
348504
healthCheck() {
349505
return this.health.check([
350-
async () => this.dogHealthIndicator.isHealthy('dog'),
506+
() => this.dogHealthIndicator.isHealthy('dog'),
351507
])
352508
}
353509
}
@@ -368,12 +524,12 @@ export class HealthController {
368524
@HealthCheck()
369525
healthCheck() {
370526
return this.health.check([
371-
async () => this.dogHealthIndicator.isHealthy('dog'),
527+
() => this.dogHealthIndicator.isHealthy('dog'),
372528
])
373529
}
374530
}
375531
```
376532

377-
#### Examples
533+
#### More Examples
378534

379-
Some working examples are available [here](https://github.com/nestjs/terminus/tree/master/sample).
535+
More working examples are available [here](https://github.com/nestjs/terminus/tree/master/sample).

0 commit comments

Comments
 (0)