All files swssettings.js

64.29% Statements 36/56
0% Branches 0/19
50% Functions 1/2
64.29% Lines 36/56

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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166    1x 1x 1x 1x 1x               1x     1x     1x     1x     1x         1x           1x     1x             1x             1x             1x       1x                 1x       1x                     1x       1x       1x     1x     1x     1x     1x     1x     1x   1x 1x 1x 1x 1x 1x                                                                                 1x 1x  
/* swagger-stats Settings */
 
const os = require('os');
const path = require('path');
const swsUtil = require('./swsUtil');
const packageInfo = require('../package.json');
const debug = require('debug')('sws:settings');
 
/* swagger=stats settings */
class SwsSettings {
 
    constructor() {
 
        // Hostname. Will attempt to detect if not explicitly provided
        this.hostname = os.hostname();
 
        // Name. Defaults to hostname if not specified
        this.name = this.hostname;
 
        // Version
        this.version='';
 
        // IP Address. Will attempt to detect if not provided
        this.ip="";
 
        // Swagger specification JSON document. Should be pre-validated and with resolved references. Optional.
        this.swaggerSpec = null;
 
        // Base path for API described in swagger spec.
        // Specify this when using openapi: "3.0.0" specifications
        // For example, setting basePath='/api' with petrstore spec would match requests /api/pet/{id}, etc ...
        this.basePath = '';
 
        // Base path for swagger-stats internal APIs.
        // If specified, will be used to serve UI, stats and metrics like this:
        // /<uriPath>/ui, /<uriPath>/stats, /<uriPath>/metrics
        // overriding default /swagger-stats/ui
        this.uriPath= '/swagger-stats';
 
        // Duration of timeline bucket in milliseconds, 60000 by default
        this.timelineBucketDuration  = 60000;
 
        // Buckets for duration histogram metrics, in Milliseconds
        // Optional. Default value:
        // [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]
        // The default buckets are tailored to broadly measure API response time.
        // Most likely needs to be defined per app to account for application specifics.
        this.durationBuckets = [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000];
 
        // Buckets for request size histogram metric, in Bytes.
        // Optional. Default value:
        // [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]
        // The default buckets are tailored to broadly measure API request size.
        // Most likely needs to be defined per app to account for application specifics.
        this.requestSizeBuckets = [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000];
 
        // Buckets for response size histogram metric, in Bytes
        // Optional. Default value:
        // [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]
        // The default buckets are tailored to broadly measure API response size.
        // Most likely needs to be defined per app to account for application specifics.
        this.responseSizeBuckets = [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000];
 
        // Apdex threshold, in milliseconds
        // 25 ms by default
        this.apdexThreshold = 25;
 
        // Callback to invoke when response is finished - https://github.com/slanatech/swagger-stats/issues/5
        // Application may implement it to trace Request Response Record (RRR), which is passed as parameter
        // the following parameters are passed to this callback:
        // onResponseFinish(req,res,rrr)
        // - req - request
        // - res - response
        // - rrr - Request Response Record (RRR)
        this.onResponseFinish = null;
 
        // Enable Basic authentication: true or false. Default false.
        // Only Basic authentication is supported
        this.authentication = false;
 
        // Callback to invoke to authenticate request to /swagger-stats/stats and /swagger-stats/metrics
        // If authentication is enabled (option authentication=true),
        // Application must implement onAuthenticate to validate user credentials
        // the following parameters are passed to this callback:
        // onAuthenticate(req,username,password)
        // - req - request
        // - username - username
        // - password - password
        // callback must return true if user authenticated, false if not
        this.onAuthenticate = null;
 
        // Max Age of the session, if authentication is enabled, in seconds
        // Default is 900 seconds
        this.sessionMaxAge = 900;
 
        // ElasticSearch URL. Enables storing of request response records in Elasticsearch.
        // Default is empty (disabled).
        this.elasticsearch = null;
 
        // Prefix for Elasticsearch index. Default is "api-"
        this.elasticsearchIndexPrefix = 'api-';
 
        // Username for Elasticsearch, if anonymous user is disbaled . Default is empty (disabled)
        this.elasticsearchUsername = null;
 
        // Password for Elasticsearch, if anonymous user is disbaled . Default is empty (disabled)
        this.elasticsearchPassword = null;
 
        // Set to true to track only requests defined in swagger spec. Default false.
        this.swaggerOnly = false;
 
        // Prometheus metrics prefix. Will be prepended to metric name if specified.
        this.metricsPrefix = '';
 
        // Enables Egress HTTP monitoring, true or false. Disabled by default.
        this.enableEgress = false;
 
        this.pathUI = '/swagger-stats/ui';
        this.pathDist = '/swagger-stats/dist';
        this.pathUX = '/swagger-stats/ux';
        this.pathStats = '/swagger-stats/stats';
        this.pathMetrics = '/swagger-stats/metrics';
        this.pathLogout = '/swagger-stats/logout';
    }
 
    init(options) {
        if(typeof options === 'undefined' || !options){
            return;
        }
 
        for(let op of Object.keys(this)){
            if(op in options){
                this[op] = options[op];
            }
        }
 
        // Set or detect node address
        if(!('ip' in options)) {
            // Attempt to detect network address
            // Use first found interface name which starts from "e" ( en0, em0 ... )
            let address = null;
            let ifaces = os.networkInterfaces();
            for( let ifacename in ifaces ){
                let iface = ifaces[ifacename];
                if( !address && !iface.internal && (ifacename.charAt(0)=='e') ){
                    if((iface instanceof Array) && (iface.length>0) ) {
                        address = iface[0].address;
                    }
                }
            }
            this.ip = address ? address : '127.0.0.1';
        }
 
        this.pathUI = this.uriPath+'/ui';
        this.pathDist = this.uriPath+'/dist';
        this.pathUX = this.uriPath+'/ux';
        this.pathStats = this.uriPath+'/stats';
        this.pathMetrics = this.uriPath+'/metrics';
        this.pathLogout = this.uriPath+'/logout';
    }
 
}
 
let swsSettings = new SwsSettings();
module.exports = swsSettings;