Skip to content

Commit

Permalink
Merge pull request #104 from pelias/support-custom-logging-name
Browse files Browse the repository at this point in the history
feat(logger): Allow adding a custom name to go along with logging
  • Loading branch information
orangejulius committed Feb 22, 2019
2 parents 61a3e64 + 5be4d6e commit 6061562
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
45 changes: 26 additions & 19 deletions src/BatchManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

var Batch = require('./Batch'),
transaction = require('./transaction'),
winston = require( 'pelias-logger' ).get( 'dbclient' );
const Batch = require('./Batch');
const transaction = require('./transaction');
const pelias_logger = require( 'pelias-logger' );
// HealthCheck = require('./HealthCheck'),
// hc = new HealthCheck( client );

Expand All @@ -11,16 +10,24 @@ var Batch = require('./Batch'),
// var debug = console.error.bind( console );
// var debug = function(){};

var stats = require('./stats');
const Stats = require('./stats');

function BatchManager( opts ){


// manager variable options
this._opts = opts || {};
if( !this._opts.flooding ){ this._opts.flooding = {}; }
if( !this._opts.flooding.pause ){ this._opts.flooding.pause = 10; } //50
if( !this._opts.flooding.resume ){ this._opts.flooding.resume = 2; } //8

// set up logger
const logger_name = this._opts.name ? `dbclient-${this._opts.name}` : 'dbclient';
this._logger = pelias_logger.get(logger_name);

// set up stats tracker
this._stats = new Stats(this._logger);

// internal variables
this._current = new Batch( this._opts );
this._transient = 0;
Expand All @@ -30,15 +37,15 @@ function BatchManager( opts ){
// return hc._status.threadpool.nodes;
// }.bind(this));

stats.watch( 'paused', function(){
this._stats.watch( 'paused', function(){
return this.isPaused();
}.bind(this));

stats.watch( 'transient', function(){
this._stats.watch( 'transient', function(){
return this._transient;
}.bind(this));

stats.watch( 'current_length', function(){
this._stats.watch( 'current_length', function(){
return this._current._slots.length;
}.bind(this));
}
Expand All @@ -49,18 +56,18 @@ BatchManager.prototype._dispatch = function( batch, next ){
this._transient++; // record active transactions

// perform the transaction
transaction( this._opts.client )( batch, function( err ){
transaction( this._opts.client , this._logger)( batch, function( err ){

// console.log( 'batch status', batch.status );

if( err ){
stats.inc( 'batch_error', 1 );
winston.error( 'transaction error', err );
this._stats.inc( 'batch_error', 1 );
this._logger.error( 'transaction error', err );
}

else {
stats.inc( 'indexed', batch._slots.length );
stats.inc( 'batch_ok', 1 );
this._stats.inc( 'indexed', batch._slots.length );
this._stats.inc( 'batch_ok', 1 );

var types = {};
var failures = 0;
Expand All @@ -76,11 +83,11 @@ BatchManager.prototype._dispatch = function( batch, next ){
}
});

stats.inc( 'batch_retries', batch.retries );
stats.inc( 'failed_records', failures );
this._stats.inc( 'batch_retries', batch.retries );
this._stats.inc( 'failed_records', failures );

for( var type in types ){
stats.inc( type, types[type] );
this._stats.inc( type, types[type] );
}
}

Expand Down Expand Up @@ -119,7 +126,7 @@ BatchManager.prototype.end = function(next){
BatchManager.prototype._attemptEnd = function(next){
if( this.finished && !this._transient && !this._current._slots.length ){
this._opts.client.close();
stats.end();
this._stats.end();

if ('function' === typeof next) {
next();
Expand All @@ -131,12 +138,12 @@ BatchManager.prototype._attemptEnd = function(next){
BatchManager.prototype._attemptPause = function( next ){
if( this._transient >= this._opts.flooding.pause ){
if( this.isPaused() ){
winston.error( 'FATAL: double pause' );
this._logger.error( 'FATAL: double pause' );
process.exit(1);
}

if( 'function' !== typeof next ){
winston.error( 'FATAL: invalid next', next );
this._logger.error( 'FATAL: invalid next', next );
process.exit(1);
}

Expand Down
9 changes: 5 additions & 4 deletions src/stats.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var winston = require( 'pelias-logger' ).get( 'dbclient' );
const default_pelias_logger = require( 'pelias-logger' ).get( 'dbclient' );
var peliasConfig = require( 'pelias-config' ).generate().dbclient;

function Stats(){
function Stats(parent_logger){
this.logger = parent_logger ? parent_logger : default_pelias_logger;
this.data = {};
this.active = false;
this.watching = {};
Expand Down Expand Up @@ -31,7 +32,7 @@ Stats.prototype.updateStats = function(){
};

Stats.prototype.flush = function(){
winston.info( this.data );
this.logger.info( this.data );
};

Stats.prototype.runWatchers = function(){
Expand Down Expand Up @@ -62,4 +63,4 @@ Stats.prototype.inc = function( key, num ){
this.data[key] += num;
};

module.exports = new Stats();
module.exports = Stats;
15 changes: 9 additions & 6 deletions src/transaction.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
var winston = require( 'pelias-logger' ).get( 'dbclient' );
const pelias_logger = require( 'pelias-logger' );

var max_retries = 5;

function wrapper( client ){
function wrapper( client, parent_logger ){

const logger = parent_logger ? parent_logger : pelias_logger.get('dbclient');

function transaction( batch, cb ){

// reached max retries
Expand Down Expand Up @@ -34,13 +37,13 @@ function wrapper( client ){

// major error
if( err ){
winston.error( 'esclient error', err );
logger.error( 'esclient error', err );
batch.status = 500;
}

// response does not contain items
if( !resp || !resp.items ){
winston.error( 'invalid resp from es bulk index operation' );
logger.error( 'invalid resp from es bulk index operation' );
batch.status = 500;
}

Expand All @@ -58,7 +61,7 @@ function wrapper( client ){
// console.log( 'set task status', task.status, JSON.stringify( action, null, 2 ) );

if( task.status > 201 ){
winston.error( '[' + action.status + ']', action.error );
logger.error( '[' + action.status + ']', action.error );
}
// else {
// delete task.cmd; // reclaim memory
Expand All @@ -75,7 +78,7 @@ function wrapper( client ){
// retry batch
if( batch.status > 201 ){
batch.retries++;
winston.info( 'retrying batch', '[' + batch.status + ']' );
logger.info( 'retrying batch', '[' + batch.status + ']' );
return transaction( batch, cb );
}

Expand Down

0 comments on commit 6061562

Please sign in to comment.