Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve which replica instance was used for mongo query using node.js

I use the official node.js mongodb client for performing Mongo queries targeting a cluster via a connection string similar to: mongodb://euwest1-01,euwest1-02,uswest2-01/dbname?replicaSet=mycluster&readPreference=nearest. As you see, I include in my cluster some differently geo-located instances and "nearest" should guarantee the right replica to be picked.

Nevertheless, I would like to know which one was used to perform any query, so that I can include to each of my operations' log the mongo replica that was used to perform the query.

Hacking around the Cursor object, I can get what I want in an hacky way:

const find = (query, callback) => {

    let cursor = coll.find(query);
    cursor.toArray((err, items) => {
      console.log(cursor.server.ismaster.me);
      callback(err, items);
    });
};

But I feel like this can break in any moment as not documented + it seems limited to the Cursor interactions (so I wouldn't know how to achieve the same for a findOne method).

Is anyone aware about a clean way to do this?

like image 790
matteofigus Avatar asked Oct 10 '16 16:10

matteofigus


People also ask

How do I find the replicas name in MongoDB?

On MongoDB Atlas project page, select the Deployment link on the left hand side menu. Under Processes tab, with Topology view you should see the replica set name (this should be the default view presented when you click on Deployment ). Generally, the replica set name is the cluster name plus -shard-0 .

How do I know if MongoDB is replica?

Check Replica Set Status To display the current state of the replica set and current state of each member, run the rs. status() method in a mongosh session that is connected to the replica set's primary.

What is replica set name in MongoDB?

A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments. This section introduces replication in MongoDB as well as the components and architecture of replica sets.

How do you retrieve data in MongoDB using Javascript?

To select data from a collection in MongoDB, we can use the findOne() method. The findOne() method returns the first occurrence in the selection. The first parameter of the findOne() method is a query object.


1 Answers

You might be interested in using the APM interface.

http://mongodb.github.io/node-mongodb-native/2.2/reference/management/apm/

This lets you access the context of each operation being run by the driver and is really meant for APM providers but if you want to keep track or log how your ops are executing it might be useful for you.

var listener = require('mongodb').instrument({
  operationIdGenerator: {
    operationId: 1,

    next: function() {
      return this.operationId++;
    }
  },

  timestampGenerator: {
    current: function() {
      return new Date().getTime();
    },

    duration: function(start, end) {
      return end - start;
    }
  }  
}, function(err, instrumentations) {
  // Instrument the driver  
});

listener.on('started', function(event) {
  // command start event (see https://github.com/mongodb/specifications/blob/master/source/command-monitoring/command-monitoring.rst)
});

listener.on('succeeded', function(event) {
  // command success event (see https://github.com/mongodb/specifications/blob/master/source/command-monitoring/command-monitoring.rst)
});

listener.on('failed', function(event) {
  // command failure event (see https://github.com/mongodb/specifications/blob/master/source/command-monitoring/command-monitoring.rst)
});
like image 199
christkv Avatar answered Sep 29 '22 21:09

christkv