Pavneet Kaur
Pavneet Kaur

Reputation: 787

Logging slow redis operations in NodeJS

In the Nodejs app, I am using 'ioredis' npm module to execute all redis operations. Redis is used heavily in the whole service. But I have seen for 1 of 1000 call, redis latency is increased to 400ms.

I am trying to log all redis operations exceeding a certain timestamp in the code itself. Suggest some ways to log such commands, without slowing any other operation. Thanks.

Upvotes: 0

Views: 843

Answers (2)

Pavneet Kaur
Pavneet Kaur

Reputation: 787

For time being, I wasn't able to find any inbuilt solution. But I handled this by creating a wrapper before the execution of Redis operations.

const traceTime = function (fn) {
   return async function (...args) {
      const startTime = Date.now();
      const executionResp = await fn.apply(this, args);
      const endTime = Date.now();
   };
}

/* wrap the redis operations with traceTime */

set = traceTime(set); // redis set operation
get = traceTime(get); // redis get operation


/* use redis set operation, wherever you want to use,           
  it will log startTime and endTime and execute redis operation from there */
set(key, value, (err, response) => { 
   // err and response handler 
});

Upvotes: 0

Prerna Jain
Prerna Jain

Reputation: 1250

You can try this :

var Moment = require('moment');

var start_time = Moment();


//->>> In case of Promises

redisOperation(arguments)
.then(function() {
   var time_taken = Moment() - start_time;
   console.log(time_taken); //However you want to log, use the logger function here
});



//->>> In case of Callback Function

redisOperation(arguments, function() {
    var time_taken = Moment() - start_time;
    console.log(time_taken); //However you want to log, use the logger function here

    cb();
});

Upvotes: 1

Related Questions