Tracing from the PHP 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 connection string. 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
$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.

<?php

ini_set("couchbase.log_level", "INFO");
ini_set("error_log", ""); // will use default logging device
// set kv threshold to 1 microsecond


$cluster = new \Couchbase\Cluster("couchbase://localhost/travel-sample?tracing_threshold_kv=0.000001&tracing_threshold_queue_flush_interval=0.0001");
$cluster->authenticateAs('Administrator', 'password');
$bucket = $cluster->openBucket('travel-sample');

for ($i = 0; $i < 5; $i++) {
  try {
    $id = "airline_1" . $i;
    $res = $bucket->get($id);
    $bucket->upsert($id, $res->value);
  } catch (Couchbase\Exception $ex) {
    if ($ex->getCode() != COUCHBASE_KEY_ENOENT) {
      throw new Exception("Unexpected Couchbase exception", $ex);
    }
  }
}

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

[cb,INFO] (tracer L:151 I:2265433480) Operations over threshold: {"count":3,"service":"kv","top":[{"last_local_address":"127.0.0.1:38374","last_local_id":"000000008707c588/71cf014552d1ebf7","last_operation_id":"get:0x2","last_remote_address":"localhost:11210","server_us":11,"total_us":155},{"last_local_address":"127.0.0.1:38374","last_local_id":"000000008707c588/71cf014552d1ebf7","last_operation_id":"get:0x0","last_remote_address":"localhost:11210","server_us":23,"total_us":230},{"last_local_address":"127.0.0.1:38374","last_local_id":"000000008707c588/71cf014552d1ebf7","last_operation_id":"upsert:0x1","last_remote_address":"localhost:11210","server_us":76,"total_us":253}]}

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 up to five document fetches but only one mutation. In our case we’ve got 2 gets and 1 upsert (depends on flush interval).

Output fields in detail.

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

{
    "last_local_address": "127.0.0.1:38374",
    "last_local_id": "000000008707c588/71cf014552d1ebf7",
    "last_operation_id": "get:0x2",
    "last_remote_address": "localhost:11210",
    "server_us": 11,
    "total_us": 155
}

This tells us the following:

  • total_us: The total time it took to perform the full operation: here around 0.1 milliseconds (so fast because server is deployed on localhost).

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