22
33const cluster = require ( 'cluster' ) ;
44
5- const ZERO_CUMULATIVE_GC_INTERVAL = {
6- minor : 0 ,
7- major : 0 ,
8- incremental : 0 ,
9- weak : 0
10- } ;
11- const GC_REPORT_INTERVAL = 1000 ;
12-
135class HeapWatch {
146 constructor ( conf , logger , metrics ) {
157 this . conf = conf = conf || { } ;
@@ -19,55 +11,6 @@ class HeapWatch {
1911 this . checkInterval = 60000 ; // Once per minute
2012 this . failCount = 0 ;
2113 this . timeoutHandle = undefined ;
22- this . gcReportInterval = undefined ;
23- this . cumulativeGCTimes = Object . assign ( { } , ZERO_CUMULATIVE_GC_INTERVAL ) ;
24- this . reportStatsHandler = ( stats ) => {
25- // Report GC timings to statsd (in nanoseconds).
26- const type = this . _gcTypeName ( stats . gctype ) ;
27- if ( type !== 'unknown' ) {
28- this . cumulativeGCTimes [ this . _gcTypeName ( stats . gctype ) ] += stats . pause ;
29- }
30- } ;
31- this . _gcStats = null ;
32- }
33-
34- _gcTypeName ( typeID ) {
35- switch ( typeID ) {
36- case 1 : return 'minor' ;
37- case 2 : return 'major' ;
38- case 4 : return 'incremental' ;
39- case 8 : return 'weak' ;
40- case 15 : return 'all' ;
41- default : return 'unknown' ;
42- }
43- }
44-
45- setGCMonitor ( ) {
46- try {
47- this . _gcStats = require ( 'gc-stats' ) ( ) ;
48- this . _gcStats . on ( 'stats' , this . reportStatsHandler ) ;
49- this . gcReportInterval = setInterval ( ( ) => {
50- Object . keys ( this . cumulativeGCTimes ) . forEach ( ( gcType ) => {
51- const totalGCTime = this . cumulativeGCTimes [ gcType ] ;
52- if ( totalGCTime > 0 ) {
53- this . metrics . makeMetric ( {
54- type : 'Histogram' ,
55- name : `gc.${ gcType } ` ,
56- prometheus : {
57- name : `nodejs_gc_${ gcType } _duration_seconds` ,
58- help : 'garbage collection pause duration seconds' ,
59- staticLabels : this . metrics . getServiceLabel ( ) ,
60- buckets : [ 5e-4 , 1e-3 , 5e-3 , 10e-3 , 15e-3 , 30e-3 , 50e-3 ]
61- }
62- } ) . observe ( totalGCTime * 1e-9 ) ; // nanoseconds to seconds
63- }
64- } ) ;
65- this . cumulativeGCTimes = Object . assign ( { } , ZERO_CUMULATIVE_GC_INTERVAL ) ;
66- } , GC_REPORT_INTERVAL ) ;
67- } catch ( e ) {
68- // gc-stats is a binary dependency, so if it's not installed
69- // ignore reporting GC metrics
70- }
7114 }
7215
7316 watch ( ) {
@@ -134,17 +77,10 @@ class HeapWatch {
13477 }
13578
13679 close ( ) {
137- if ( this . _gcStats ) {
138- this . _gcStats . removeListener ( 'stats' , this . reportStatsHandler ) ;
139- }
14080 if ( this . timeoutHandle ) {
14181 clearTimeout ( this . timeoutHandle ) ;
14282 this . timeoutHandle = undefined ;
14383 }
144- if ( this . gcReportInterval ) {
145- clearInterval ( this . gcReportInterval ) ;
146- this . gcReportInterval = undefined ;
147- }
14884 }
14985}
15086
0 commit comments