@@ -102,7 +102,7 @@ export class HealthController {
102
102
@HealthCheck ()
103
103
healthCheck() {
104
104
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' ),
106
106
])
107
107
}
108
108
}
@@ -163,6 +163,36 @@ The interface of this response object can be accessed from the `@nestjs/terminus
163
163
| ` error ` | Object containing information of each health indicator which is of status ` 'down' ` , or in other words "unhealthy". | ` object ` |
164
164
| ` details ` | Object containing all information of each health indicator | ` object ` |
165
165
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
+
166
196
#### TypeOrm health indicator
167
197
168
198
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 {
200
230
@HealthCheck ()
201
231
healthCheck() {
202
232
return this .health .check ([
203
- async () => this .db .pingCheck (' database' ),
233
+ () => this .db .pingCheck (' database' ),
204
234
])
205
235
}
206
236
}
@@ -252,6 +282,126 @@ export class HealthController {
252
282
}
253
283
```
254
284
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 readonly health : HealthCheckService ,
298
+ private readonly 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 (health , disk ) {}
314
+
315
+ @Get ()
316
+ @HealthCheck ()
317
+ healthCheck() {
318
+ return this .health .check ([
319
+ () => this .disk .checkStorage (' storage' , { path: ' /' , thresholdPercent: 0.5 }),
320
+ ])
321
+ }
322
+ }
323
+ ```
324
+
325
+ With the ` DiskHealthIndicator.checkStorage ` function you also have the possibility to check for a fixed amount of space.
326
+ The following example would be unhealthy in case the path ` /my-app/ ` would exceed 250GB.
327
+
328
+ ``` typescript
329
+ @@filename (health .controller )
330
+ // Within the `HealthController`-class
331
+
332
+ @Get ()
333
+ @HealthCheck ()
334
+ check () {
335
+ return this .health .check ([
336
+ () => this .disk .checkStorage (' storage' , { path: ' /' , threshold: 250 * 1024 * 1024 * 1024 , })
337
+ ]);
338
+ }
339
+ ```
340
+
341
+ #### Memory health indicator
342
+
343
+ To make sure your process does not exceed a certain memory limit the ` MemoryHealthIndicator ` can be used.
344
+ The following example can be used to check the heap of your process.
345
+
346
+ > 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:
347
+ > - The memory is _ free_ 'd
348
+ > - The program terminates
349
+
350
+ ``` typescript
351
+ @@filename (health .controller )
352
+ @Controller (' health' )
353
+ export class HealthController {
354
+ constructor (
355
+ private health : HealthCheckService ,
356
+ private memory : MemoryHealthIndicator ,
357
+ ) {}
358
+
359
+ @Get ()
360
+ @HealthCheck ()
361
+ check() {
362
+ return this .health .check ([
363
+ () => this .memory .checkHeap (' memory_heap' , 150 * 1024 * 1024 ),
364
+ ]);
365
+ }
366
+ }
367
+ @@switch
368
+ @Controller (' health' )
369
+ @Dependencies (HealthCheckService , MemoryHealthIndicator )
370
+ export class HealthController {
371
+ constructor (health , memory ) {}
372
+
373
+ @Get ()
374
+ @HealthCheck ()
375
+ healthCheck() {
376
+ return this .health .check ([
377
+ () => this .memory .checkHeap (' memory_heap' , 150 * 1024 * 1024 ),
378
+ ])
379
+ }
380
+ }
381
+ ```
382
+
383
+ It is also possible to verify the memory RSS of your process with ` MemoryHealthIndicator.checkRSS ` . This example
384
+ would return an unhealthy response code in case your process does have more than 150MB allocated.
385
+
386
+ > 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.
387
+ > It does not include memory that is swapped out. It does include memory from shared libraries as long as the pages from
388
+ > those libraries are actually in memory. It does include all stack and heap memory.
389
+
390
+
391
+ ``` typescript
392
+ @@filename (health .controller )
393
+ // Within the `HealthController`-class
394
+
395
+ @Get ()
396
+ @HealthCheck ()
397
+ check () {
398
+ return this .health .check ([
399
+ () => this .memory .checkRSS (' memory_rss' , 150 * 1024 * 1024 ),
400
+ ]);
401
+ }
402
+ ```
403
+
404
+
255
405
#### Custom health indicator
256
406
257
407
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 +497,7 @@ export class HealthController {
347
497
@HealthCheck ()
348
498
healthCheck() {
349
499
return this .health .check ([
350
- async () => this .dogHealthIndicator .isHealthy (' dog' ),
500
+ () => this .dogHealthIndicator .isHealthy (' dog' ),
351
501
])
352
502
}
353
503
}
@@ -368,12 +518,12 @@ export class HealthController {
368
518
@HealthCheck ()
369
519
healthCheck() {
370
520
return this .health .check ([
371
- async () => this .dogHealthIndicator .isHealthy (' dog' ),
521
+ () => this .dogHealthIndicator .isHealthy (' dog' ),
372
522
])
373
523
}
374
524
}
375
525
```
376
526
377
- #### Examples
527
+ #### More examples
378
528
379
- Some working examples are available [ here] ( https://github.com/nestjs/terminus/tree/master/sample ) .
529
+ More working examples are available [ here] ( https://github.com/nestjs/terminus/tree/master/sample ) .
0 commit comments