src/transaction.js
function httpTransaction(req, res) {
    let initializationPromise;

    /* Architecture Note #3.1: New Transaction
    The idea is to maintain a hash of each pending
     transaction. To do so, we create a transaction
     object that contains useful informations about
     the transaction and we store it into the
     `TRANSACTIONS` hash.

    Each transaction has a unique id that is either
     generated or picked up in the `Transaction-Id`
     request header. This allows to trace
     transactions end to end with that unique id.
    */
    return Promise.resolve().then(() => {
      const request = {
        url: req.url,
        method: req.method.toLowerCase(),
        headers: req.headers,
        body: req,
      };
      const transaction = {
        protocol: req.connection.encrypted ? 'https' : 'http',
        ip:
          (req.headers['x-forwarded-for'] || '').split(',')[0] ||
          req.connection.remoteAddress,
        startInBytes: req.socket.bytesRead,
        startOutBytes: req.socket.bytesWritten,
        startTime: time(),
        url: req.url,
        method: req.method,
        reqHeaders: req.headers,
        errored: false,
      };
      const delayPromise = delay.create(TIMEOUT);
      let id = req.headers['transaction-id'] || uniqueId();

      // Handle bad client transaction ids
      if (TRANSACTIONS[id]) {
        initializationPromise = Promise.reject(
          new HTTPError(400, 'E_TRANSACTION_ID_NOT_UNIQUE', id),
        );
        id = uniqueId();
      } else {
        initializationPromise = Promise.resolve();
      }

      transaction.id = id;
      TRANSACTIONS[id] = transaction;

      return [
        request,
        {
          id,
          start: startTransaction.bind(
            null,
            { id, req, res, delayPromise },
            initializationPromise,
          ),
          catch: catchTransaction.bind(null, { id, req, res }),
          end: endTransaction.bind(null, { id, req, res, delayPromise }),
        },
      ];
    });
  }
function httpTransaction(req, res) {
    let initializationPromise;

    /* Architecture Note #3.1: New Transaction
    The idea is to maintain a hash of each pending
     transaction. To do so, we create a transaction
     object that contains useful informations about
     the transaction and we store it into the
     `TRANSACTIONS` hash.

    Each transaction has a unique id that is either
     generated or picked up in the `Transaction-Id`
     request header. This allows to trace
     transactions end to end with that unique id.
    */
    return Promise.resolve().then(() => {
      const request = {
        url: req.url,
        method: req.method.toLowerCase(),
        headers: req.headers,
        body: req,
      };
      const transaction = {
        protocol: req.connection.encrypted ? 'https' : 'http',
        ip:
          (req.headers['x-forwarded-for'] || '').split(',')[0] ||
          req.connection.remoteAddress,
        startInBytes: req.socket.bytesRead,
        startOutBytes: req.socket.bytesWritten,
        startTime: time(),
        url: req.url,
        method: req.method,
        reqHeaders: req.headers,
        errored: false,
      };
      const delayPromise = delay.create(TIMEOUT);
      let id = req.headers['transaction-id'] || uniqueId();

      // Handle bad client transaction ids
      if (TRANSACTIONS[id]) {
        initializationPromise = Promise.reject(
          new HTTPError(400, 'E_TRANSACTION_ID_NOT_UNIQUE', id),
        );
        id = uniqueId();
      } else {
        initializationPromise = Promise.resolve();
      }

      transaction.id = id;
      TRANSACTIONS[id] = transaction;

      return [
        request,
        {
          id,
          start: startTransaction.bind(
            null,
            { id, req, res, delayPromise },
            initializationPromise,
          ),
          catch: catchTransaction.bind(null, { id, req, res }),
          end: endTransaction.bind(null, { id, req, res, delayPromise }),
        },
      ];
    });
  }
Create a new HTTP transaction
@param  {HTTPRequest}  req
A raw NodeJS HTTP incoming message
@param  {HTTPResponse} res
A raw NodeJS HTTP response
@return {Array}
The normalized request and the HTTP
transaction created in an array.