diff --git a/config/config-sample.json b/config/config-sample.json index b7b322b..bc34c92 100644 --- a/config/config-sample.json +++ b/config/config-sample.json @@ -7,6 +7,7 @@ "WORKER_MAX_MEM_MB" : 53, "DB_UPDATES_ENABLE" : false, + "STATSD_ENABLE" : true, "BUCKET_NO_MIN" : 0, "BUCKET_NO_MAX" : 512, diff --git a/config/config.readme b/config/config.readme index 1c443de..667d5cd 100644 --- a/config/config.readme +++ b/config/config.readme @@ -26,6 +26,9 @@ DB_UPDATES_ENABLE WARNING: Do not enabled this on production hosts. This should only be enabled on local docker test environments and never in production. Set to true to allow Jetmon to update the jetpack_monitor_sites table. Without this, it is difficult to test how effective the code is working when in a local docker test environment. +STATSD_ENABLE +When set to true, metrics will be sent to StatsD. Set to false to disable these metrics. This config was added in order to test how enabling and disabling StatsD metrics affect overall server load and performance. + BUCKET_NO_MIN The first bucket in the range of jetpack_monitor_sites buckets that this host should process when checking sites. Each host should be configured to have a unique set of buckets that it is responsible for. The buckets currently range from 0 to 511. diff --git a/lib/config.js b/lib/config.js index 06316ed..b8fe1a3 100644 --- a/lib/config.js +++ b/lib/config.js @@ -9,8 +9,16 @@ var config = { this._cache = JSON.parse( fs.readFileSync( CONFIG_FILE ).toString() ); return this._cache; }, - get: function( key ) { - return this._cache[ key ]; + get: function( key, default_value ) { + if ( 'undefined' !== typeof this._cache[ key ] ) { + return this._cache[ key ]; + } else if ( 'undefined' !== typeof default_value ) { + console.log( {key: key, value: default_value, source: 'default'} ); + return default_value; + } + + console.log( {key: key, value: null, source: 'null'} ); + return null; } }; diff --git a/lib/statsd.js b/lib/statsd.js index 8df092f..f333522 100644 --- a/lib/statsd.js +++ b/lib/statsd.js @@ -50,13 +50,16 @@ const statsdClient = { this.logger = logger; this.buffer = ''; + this.enabled = global.config.get( 'STATSD_ENABLE', true ); - this.socket = dgram.createSocket( 'udp4' ); - this.socket.on( 'error', (error) => this.logger.error( error ) ); + if ( this.enabled ) { + this.socket = dgram.createSocket( 'udp4' ); + this.socket.on( 'error', (error) => this.logger.error( error ) ); - this.interval = setInterval( () => { - this.flush(); - }, flushInterval ); + this.interval = setInterval( () => { + this.flush(); + }, flushInterval ); + } }, increment: function( metric, value = 1, sampleRate = 1) { @@ -72,19 +75,21 @@ const statsdClient = { }, send: function( message ) { - message = `${this.prefix}${message}\n`; + if ( this.enabled ) { + message = `${this.prefix}${message}\n`; - // If the total buffer size is already at the maximum size, flush it first - if ( this.buffer.length + message.length >= this.maxBufferSize ) { - this.flush(); - } + // If the total buffer size is already at the maximum size, flush it first + if ( this.buffer.length + message.length >= this.maxBufferSize ) { + this.flush(); + } - // Append the message to the buffer - this.buffer += message; + // Append the message to the buffer + this.buffer += message; + } }, flush: function() { - if ( this.buffer.length > 0 ) { + if ( this.enabled && this.buffer.length > 0 ) { const buffer = this.buffer; this.buffer = ''; try { @@ -101,8 +106,10 @@ const statsdClient = { }, close: function() { - clearInterval( this.interval ); - this.socket.close(); + if ( this.enabled ) { + clearInterval( this.interval ); + this.socket.close(); + } } }