@@ -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,132 @@ 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 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
+
255
411
#### Custom health indicator
256
412
257
413
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 {
347
503
@HealthCheck ()
348
504
healthCheck() {
349
505
return this .health .check ([
350
- async () => this .dogHealthIndicator .isHealthy (' dog' ),
506
+ () => this .dogHealthIndicator .isHealthy (' dog' ),
351
507
])
352
508
}
353
509
}
@@ -368,12 +524,12 @@ export class HealthController {
368
524
@HealthCheck ()
369
525
healthCheck() {
370
526
return this .health .check ([
371
- async () => this .dogHealthIndicator .isHealthy (' dog' ),
527
+ () => this .dogHealthIndicator .isHealthy (' dog' ),
372
528
])
373
529
}
374
530
}
375
531
```
376
532
377
- #### Examples
533
+ #### More Examples
378
534
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