import * as prom from 'prom-client' ; import metrics from './' ; const gaugeSpecificationLabels = { } ; /**
 * Prometheus metrics
 */ export class PrometheusMetrics { private counters : { [ key ] : prom . Counter ; } = { } ; private gauges : { [ key ] : prom . Gauge ; } = { } ; private constructor ( ) { prom . collectDefaultMetrics ( ) ; Object . keys ( metrics ) . forEach ( metricId => { const metricName = metrics [ metricId ] ; if ( metricId . slice ( 0 , 2 ) === 'G_' ) { this . registerGauge ( metricName , metricName , gaugeSpecificationLabels [ metricId ] ) ; this . setGauge ( metricName , 0 , [ ] ) ; } else { this . registerCounter ( metricName , metricName ) ; } } ) ; } private static _instance : PrometheusMetrics ; /**
     * Get singleton instance
     */ static getInstance ( ) { return PrometheusMetrics . _instance = PrometheusMetrics . _instance || new PrometheusMetrics ( ) ; } /**
     * Get headers
     */ getHeaders ( ) { return { 'content-type' : prom . register . contentType } ; } /**
     * Render body for prometheus scrape
     */ getMetrics ( ) { return prom . register . metrics ( ) ; } param name param help registerCounter ( name , help , labels ? ) { return this . counters [ name ] = new prom . Counter ( { name , help , labelNames : labels } ) ; } param name param help registerGauge ( name , help , labels ? ) { return this . gauges [ name ] = new prom . Gauge ( { name , help , labelNames : labels } ) ; } param name param labels param value incCounter ( name , labels , value = 1 ) { this . counters [ name ] . inc ( labels , value , new Date ( ) ) ; } param name param value param labels setGauge ( name , value , labels ) { this . gauges [ name ] . set ( labels , value , new Date ( ) ) ; } param name param value param labels incGauge ( name , value , labels ) { this . gauges [ name ] . inc ( labels , value , new Date ( ) ) ; } param name param value param labels decGauge ( name , value , labels ) { this . gauges [ name ] . dec ( labels , value , new Date ( ) ) ; } param name startGauge ( name ) { return this . gauges [ name ] . startTimer ( ) ; } }