Tracing from the Python 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 couchbase.Bucket. By default it will log every minute (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!

options = dict(bucket="default", enable_tracing="yes")
hostname ="localhost"
cluster = Cluster("couchbase://{hostname}".format(hostname=hostname));
cluster.authenticate("username", "password");
bucket = cluster.open_bucket(**options)
queue_size=100000000
bucket.tracing_threshold_kv=1
bucket.threshold_logging_tracer_interval=1e9
bucket.tracing_threshold_queue_size=queue_size
bucket.tracing_orphaned_queue_size=queue_size

This will set our threshold for key/value operations to one microsecond, log the found operations every second, and set the sample size to a very large value so everything will be logged.

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

// Connect


// Load a couple of docs and write them back
for i in range(0,5):
    keyname = "airline_1" + str(i)
    doc = bucket.get(keyname);
    if doc and doc.value:
        bucket.upsert(keyname, doc.value);

time.sleep(60)

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

Apr 04, 2018 9:42:57 AM com.couchbase.client.core.tracing.ThresholdLogReporter logOverThreshold
[4141233127] Operations over threshold:
{
    "count": 99,
    "service": "kv",
    "top": [
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1ca4",
                "operation_name": "upsert",
                "server_us": 19,
                "total_us": 8898
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1ca7",
                "operation_name": "upsert",
                "server_us": 8,
                "total_us": 10334
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1caa",
                "operation_name": "upsert",
                "server_us": 6,
                "total_us": 11273
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cb0",
                "operation_name": "upsert",
                "server_us": 6,
                "total_us": 11746
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cb4",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 12232
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cb6",
                "operation_name": "upsert",
                "server_us": 6,
                "total_us": 12750
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cb9",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 13241
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cbd",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 13687
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cbf",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 14292
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cc4",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 14764
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cc8",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 15346
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cc9",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 16046
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1ccd",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 16602
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cd7",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 17054
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cdb",
                "operation_name": "upsert",
                "server_us": 6,
                "total_us": 17622
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cdf",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 18123
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1ce8",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 18531
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cef",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 18987
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cf4",
                "operation_name": "upsert",
                "server_us": 6,
                "total_us": 19523
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cfc",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 19894
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cfe",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 20497
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1cff",
                "operation_name": "upsert",
                "server_us": 3,
                "total_us": 21137
            },
            {
                "last_local_address": "192.168.99.1:49414",
                "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
                "last_remote_address": "192.168.99.1:49398",
                "operation_id": "0x1d01",
                "operation_name": "upsert",
                "server_us": 2,
                "total_us": 21838
            },
        ...,
    "total_us":69233
}

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:

{
        {
        "last_local_address": "192.168.99.1:49414",
        "last_local_id": "00000000f6d633e7/1816c785a1ee1ffe",
        "last_remote_address": "192.168.99.1:49398",
        "operation_id": "0x1d01",
        "operation_name": "upsert",
        "server_us": 2,
        "total_us": 21838
        }
}

This tells us the following:

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

  • server_us: The server reported that its work performed took 2 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.

  • operation_id: The id (in this case the opaque value), useful for diagnosing and troubleshooting in combination with the last_local_id.

  • operation_name: The operation name, 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.

Timeout Visibility.

Previously, when an operation takes longer than the timeout specified allows, a TimeoutError is thrown. It usually looks like this:.

root: ERROR: Got exception [<Key='key', RC=0x17[Client-Side timeout exceeded for operation. Inspect network conditions or increase the timeout], Operational Error, Results=1, C Source=(src/multiresult.c,316), Tracing Output={"key": {"s": "kv:get", "c": "00000000d5ba1b67/360fbfab17ec265c", "i": 18232203239736973189, "b": "default", "l": "10.143.180.1:57847", "r": "10.143.180.1:57834", "t": 900}}>]

Now the timeout itself provides you valuable information like the local and remote sockets, and the operation id, as well as the timeout set and the local ID used for troubleshooting. You can take this information and correlate it to the top slow operations in the threshold log.

The TimeoutError now provides you more information into what went wrong and then you can go look at the log to figure out why it was slow.