tevent: add request chain id tracking

We use tevent heavily in SSSD. There is an incoming user request that creates a chain of nested tevent requests until a non-blocking I/O is required. There are usually multiple user requests in parallel which produces lots of overlapping log messages and it is very difficult to orient in the log messages and request flow without a deep understanding of the project.

What we would like to have is a unique identifier of the user request that is visible in the log message. The identifier must be visible in all nested tevent requests and other tevent objects that are related to each other so we can easily track the user request from its creation to its end. We would like to avoid passing down the id manually since this would require tremendous amount of changes, so we found a solution in the tevent library. The solution works for us and can be also reused by other projects as is but it is of course opened for discussion.

Solution overview

  • The current chain id is stored in a global variable tevent_chain_id.
  • When a new tevent request/fd/timer/immediate object is created it inherits the current chain id.
    req->chain_id = tevent_chain_id;
  • Request's chain id is set to the global variable when it is about to call the callback and then restored immediately to its previous value.
    old_id = tevent_set_chain_id(req->chain_id);
    req->async.fn(req);
    tevent_set_chain_id(old_id);
  • Chain id 0 is reserved for cases when we are not in any user request.
  • When a user request is received by the application it calls tevent_set_chain_id(id) to set the current id prior creating the top level tevent request:
    old_chain_id = tevent_set_chain_id(req_id);
    req = send_fn(...);
    tevent_set_chain_id(old_chain_id);
  • It is then consumed in the log messages by calling tevent_get_chain_id()

You can find example usage in my SSSD branch.

Please note that this version is not currently thread-safe. But it can be made thread-safe if it is required and you generally agree with this feature.

Checklist

  • Commits have Signed-off-by: with name/author being identical to the commit author
  • (optional) This MR is just one part towards a larger feature.
  • (optional, if backport required) Bugzilla bug filed and BUG: tag added
  • Test suite updated with functionality tests
  • Test suite updated with negative tests
  • Documentation updated
  • CI timeout is 3h or higher (see Settings/CICD/General pipelines/ Timeout)

Reviewer's checklist:

  • There is a test suite reasonably covering new functionality or modifications
  • Function naming, parameters, return values, types, etc., are consistent and according to README.Coding.md
  • This feature/change has adequate documentation added
  • No obvious mistakes in the code

Merge request reports

Loading