Tracing from the Node.js SDK with Couchbase Server

    Use tracing from the SDK to discover timeouts and bottlenecks across the network and cluster.

    For our example, we will customize the threshold logging tracer settings with lcb_cntl. By default it will log every 10 seconds (if something is found) and only sample the top 10 slowest operations. The default threshold for key/value operation is 500 milliseconds. We shall set them so low that almost everything gets logged - not something you should do in production!

    // set kv threshold to 1 microsecond
    // lower flush interval to 100 microseconds
    var cluster = new couchbase.Cluster("couchbase://localhost/travel-sample?tracing_threshold_kv=0.000001&tracing_threshold_queue_flush_interval=0.0001");

    This will set our threshold for key/value operations to one microsecond, and log the found operations every second.

    With these configs in place we are ready to run some operations. Below, we read some documents from the travel-sample bucket and, if found, write them back with an upsert - giving us both read and write operations to log.

    var cluster = new couchbase.Cluster("couchbase://localhost/travel-sample?tracing_threshold_kv=0.000001&tracing_threshold_queue_flush_interval=0.0001");
    cluster.authenticateAs('Administrator', 'password');
    var bucket = cluster.openBucket('travel-sample');
    for (var i = 0; i < 5; ++i) {
        bucket.get('airline_10', function(err, res) {
            if (err) {
                throw err;
            // completed, it should be in our traces!

    Run the code, and you will see something like the following in the logs:

    22ms [I3fc6ae73] {27957} [INFO] (tracer - L:148) Operations over threshold: {"count":6,"service":"kv","top":[{"last_local_address":"","last_local_id":"000000003fc6ae73/277953824e30913b","last_operation_id":"get:0x0","last_remote_address":"","server_us":27,"total_us":1761},{"last_local_address":"","last_local_id":"000000003fc6ae73/277953824e30913b","last_operation_id":"get:0x1","last_remote_address":"","server_us":8,"total_us":2189},{"last_local_address":"","last_local_id":"000000003fc6ae73/277953824e30913b","last_operation_id":"get:0x2","last_remote_address":"","server_us":3,"total_us":2356},{"last_local_address":"","last_local_id":"000000003fc6ae73/277953824e30913b","last_operation_id":"get:0x3","last_remote_address":"","server_us":3,"total_us":2477},{"last_local_address":"","last_local_id":"000000003fc6ae73/277953824e30913b","last_operation_id":"upsert:0x5","last_remote_address":"","server_us":100,"total_us":2489},{"last_local_address":"","last_local_id":"000000003fc6ae73/277953824e30913b","last_operation_id":"get:0x4","last_remote_address":"","server_us":3,"total_us":2592}]}

    For each service (only kv-based on this workload), the threshold log reporter will show you the total number of recorded ops (through count), and give you the top slowest ops sorted by their latency. Since only airline_10 exists in the bucket you will see five document fetches but only one mutation.

    Output fields in detail.

    Let’s highlight a single operation, and explain each field in a little more detail:

        "total_us": 1761,
        "server_us": 27,
        "last_remote_address": "",
        "last_operation_id": "get:0x0",
        "last_local_id": "000000003fc6ae73/277953824e30913b",
        "last_local_address": ""

    This tells us the following:

    • total_us: The total time it took to perform the full operation: here around 1.8 milliseconds.

    • server_us: The server reported that its work performed took 27 microseconds (this does not include network time or time in the buffer before picked up at the cluster).

    • last_local_address: The local socket used for this operation.

    • last_remote_address: The remote socket on the server used for this operation. Useful to figure out which node is affected.

    • last_operation_id: A combination of type of operation and id (in this case the opaque value), useful for diagnosing and troubleshooting in combination with the last_local_id.

    • last_local_id: With Server 5.5 and later, this id is negotiated with the server and can be used to correlate logging information on both sides in a simpler fashion.

    You can see that if the thresholds are set the right way based on production requirements, without much effort slow operations can be logged and pinpointed more easily than before.