なっく日報

技術やら生活やらのメモ

node-mongodb-nativeのAPM(Application Performance Monitoring)のメモ

node-mongodb-nativeのv2からAPM(Application Performance Monitoring)が入ったそうなので、メモっておきます。

APMとは何か?

Mongoドライバにいい感じにフックをかけて、モニタリングできるようにしたAPIです。

ココに仕様が github.com

Node.js版ドライバのドキュメントでAPMの説明は↓
http://mongodb.github.io/node-mongodb-native/2.0/reference/management/apm/

どんなことができるか?

  • クエリのレスポンスタイムを計ったり
  • どのコレクションにどの種類のクエリが多いか記録したり

などなど。

MongoDBのサーバ側でもdb.setProfilingLevel(2)とかすれば、モニタリングできますが、mongod単位らしくシャーディング環境だと使いづらい場合もあるかもしれません。 docs.mongodb.org

アプリ側で計測できる機能があるというのはありがたいですね。

ちなみに↓のようなコードを書いて、明示的に設定しないと有効にならないそう。

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)
});

まだ、少し触っているレベルなので、こんなところで。

この機能を求めていた!という人もいるかと思うので、軽くご紹介しました。