Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"WORKER_MAX_MEM_MB" : 53,

"DB_UPDATES_ENABLE" : false,
"STATSD_ENABLE" : true,

"BUCKET_NO_MIN" : 0,
"BUCKET_NO_MAX" : 512,
Expand Down
3 changes: 3 additions & 0 deletions config/config.readme
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand Down
37 changes: 22 additions & 15 deletions lib/statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -101,8 +106,10 @@ const statsdClient = {
},

close: function() {
clearInterval( this.interval );
this.socket.close();
if ( this.enabled ) {
clearInterval( this.interval );
this.socket.close();
}
}
}

Expand Down