import type { TDefaultedPromsterOptions , TGcMetrics } from '@promster/types' ; import merge from 'merge-options' ; import { configure , Prometheus } from '../client' ; const defaultLabels = [ 'gc_type' ] ; const asArray = ( maybeArray ) => Array . isArray ( maybeArray ) ? maybeArray : [ maybeArray ] ; const defaultOptions = { getLabelValues : ( ) => ( { } ) , labels : [ ] , metricPrefix : '' , metricNames : { up : [ 'up' ] , countOfGcs : [ 'nodejs_gc_runs_total' ] , durationOfGc : [ 'nodejs_gc_pause_seconds_total' ] , reclaimedInGc : [ 'nodejs_gc_reclaimed_bytes_total' ] , } , } ; const getMetrics = ( options ) => ( { up : asArray ( options . metricNames . up ) . map ( ( nameOfUpMetric ) => new Prometheus . Gauge ( { name : `${ options . metricPrefix }${ nameOfUpMetric }` , help : '1 = up, 0 = not up' , } ) ) , countOfGcs : asArray ( options . metricNames . countOfGcs ) . map ( ( nameOfCountOfGcsMetric ) => new Prometheus . Counter ( { name : `${ options . metricPrefix }${ nameOfCountOfGcsMetric }` , help : 'Count of total garbage collections.' , labelNames : defaultLabels , } ) ) , durationOfGc : asArray ( options . metricNames . durationOfGc ) . map ( ( nameOfDurationOfGcMetric ) => new Prometheus . Counter ( { name : `${ options . metricPrefix }${ nameOfDurationOfGcMetric }` , help : 'Time spent in GC Pause in seconds.' , labelNames : defaultLabels , } ) ) , reclaimedInGc : asArray ( options . metricNames . reclaimedInGc ) . map ( ( nameOfReclaimedInGcMetric ) => new Prometheus . Counter ( { name : `${ options . metricPrefix }${ nameOfReclaimedInGcMetric }` , help : 'Total number of bytes reclaimed by GC.' , labelNames : defaultLabels , } ) ) , } ) ; const createGcMetrics = ( options ) : TGcMetrics => { const defaultedOptions = merge ( defaultOptions , options ) ; configure ( { prefix : defaultedOptions . metricPrefix , } ) ; const gcMetrics = getMetrics ( defaultedOptions ) ; return gcMetrics ; } ; createGcMetrics . defaultOptions = defaultOptions ; export { createGcMetrics } ;