from typing import OrderedDict
import json


def request_trace_json(tracedata, parts):
    """Format one tracedata event as part of a request's audit trail.

       tracedata: a string representation of trace event data
       parts: dictionary of log parts
    """
    od = OrderedDict([
        (k, v) for k, v in [
            ('elapsed', parts['elapsed']),
            ('req', parts['reqid']),
            ('trace', tracedata),
            ('client', parts['client_ip']),
            ('user', parts['client_identity_obj']),
        ]
        if v
    ])
    return json.dumps(od, separators=(', ', ':'))