All files swsmetrics.js

29.03% Statements 9/31
0% Branches 0/7
33.33% Functions 1/3
29.03% Lines 9/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145    1x 1x 1x 1x                 1x                                                       1x                                             1x                                                                                                                                                         1x 1x  
/* swagger=stats Prometheus metrics */
 
const path = require('path');
const swsUtil = require('./swsUtil');
const promClient = require("prom-client");
const debug = require('debug')('sws:metrics');
 
 
/* swagger=stats Prometheus metrics */
class SwsMetrics {
 
    constructor() {
 
        // Core API Metrics
        this.coreMetricsDefs = {
            api_all_request_total: {
                type: 'counter',
                help: 'The total number of all API requests received'
            },
            api_all_success_total: {
                type: 'counter',
                help: 'The total number of all API requests with success response'
            },
            api_all_errors_total: {
                type: 'counter',
                help: 'The total number of all API requests with error response'
            },
            api_all_client_error_total: {
                type: 'counter',
                help: 'The total number of all API requests with client error response'
            },
            api_all_server_error_total: {
                type: 'counter',
                help: 'The total number of all API requests with server error response'
            },
            api_all_request_in_processing_total: {
                type: 'gauge',
                help: 'The total number of all API requests currently in processing (no response yet)'
            },
        };
 
        // System metrics for node process
        this.systemMetricsDefs = {
            nodejs_process_memory_rss_bytes: {
                type: 'gauge',
                help: 'Node.js process resident memory (RSS) bytes '
            },
            nodejs_process_memory_heap_total_bytes: {
                type: 'gauge',
                help: 'Node.js process memory heapTotal bytes'
            },
            nodejs_process_memory_heap_used_bytes: {
                type: 'gauge',
                help: 'Node.js process memory heapUsed bytes'
            },
            nodejs_process_memory_external_bytes: {
                type: 'gauge',
                help: 'Node.js process memory external bytes'
            },
            nodejs_process_cpu_usage_percentage: {
                type: 'gauge',
                help: 'Node.js process CPU usage percentage'
            },
        };
 
        this.apiMetricsDefs = {
            // API Operation counters, labeled with method, path and code
            api_request_total: {
                type: 'counter',
                help: 'The total number of all API requests',
                labelNames: ['method', 'path', 'code']
            },
 
            // API request duration histogram, labeled with method, path and code
            api_request_duration_milliseconds: {
                type: 'histogram',
                help: 'API requests duration',
                labelNames: ['method', 'path', 'code']
            },
 
            // API request size histogram, labeled with method, path and code
            api_request_size_bytes: {
                type: 'histogram',
                help: 'API requests size',
                labelNames: ['method', 'path', 'code']
            },
 
            // API response size histogram, labeled with method, path and code
            api_response_size_bytes: {
                type: 'histogram',
                help: 'API requests size',
                labelNames: ['method', 'path', 'code']
            }
 
        }
 
    }
 
    // Create Prometheus metrics based on passed definition
    getPrometheusMetrics(prefix, metricDefs) {
        let allMetrics = {};
        for (let metricId of Object.keys(metricDefs)) {
            let metricDef = metricDefs[metricId];
            let metric = null;
            let metricConfig = {
                name: prefix + metricId,
                help: metricDef.help
            };
            if ('labelNames' in metricDef) {
                metricConfig.labelNames = metricDef.labelNames;
            }
            if ('buckets' in metricDef) {
                metricConfig.buckets = metricDef.buckets;
            }
            switch (metricDef.type) {
                case 'counter': {
                    metric = new promClient.Counter(metricConfig);
                    break;
                }
                case 'gauge': {
                    metric = new promClient.Gauge(metricConfig);
                    break;
                }
                case 'histogram': {
                    metric = new promClient.Histogram(metricConfig);
                    break;
                }
            }
            allMetrics[metricId] = metric;
        }
        return allMetrics;
    }
 
    clearPrometheusMetrics(metrics) {
        for (let metricId of Object.keys(metrics)) {
            let metric = metrics[metricId];
            promClient.register.removeSingleMetric(metric.name);
            delete metrics[metricId];
        }
    }
}
 
let swsMetrics = new SwsMetrics();
module.exports = swsMetrics;