startManagedTransaction()

in packages/rum-core/src/performance-monitoring/transaction-service.js [121:202]


  startManagedTransaction(name, type, perfOptions) {
    let tr = this.getCurrentTransaction()
    let isRedefined = false
    if (!tr) {
      tr = this.createCurrentTransaction(name, type, perfOptions)
    } else if (tr.canReuse() && perfOptions.canReuse) {
      /*
       * perfOptions could also have `canReuse:true` in which case we
       * allow a redefinition until there's a call that doesn't have that
       * or the threshold is exceeded.
       */
      let redefineType = tr.type
      const currentTypeOrder = TRANSACTION_TYPE_ORDER.indexOf(tr.type)
      const redefineTypeOrder = TRANSACTION_TYPE_ORDER.indexOf(type)

      /**
       * Update type based on precedence defined in TRANSACTION_TYPE_ORDER.
       * 1. If both orders doesn't exist, we don't redefine the type.
       * 2. If only the redefined type is not present in predefined order, that implies
       * it's a user defined type and it is of higher precedence
       */
      if (currentTypeOrder >= 0 && redefineTypeOrder < currentTypeOrder) {
        redefineType = type
      }
      if (__DEV__) {
        this._logger.debug(
          `redefining transaction(${tr.id}, ${tr.name}, ${tr.type})`,
          'to',
          `(${name || tr.name}, ${redefineType})`,
          tr
        )
      }
      tr.redefine(name, redefineType, perfOptions)
      isRedefined = true
    } else {
      if (__DEV__) {
        this._logger.debug(
          `ending previous transaction(${tr.id}, ${tr.name})`,
          tr
        )
      }
      tr.end()
      tr = this.createCurrentTransaction(name, type, perfOptions)
    }

    if (tr.type === PAGE_LOAD) {
      if (!isRedefined) {
        this.recorder.start(LARGEST_CONTENTFUL_PAINT)
        this.recorder.start(PAINT)
        this.recorder.start(FIRST_INPUT)
        this.recorder.start(LAYOUT_SHIFT)
      }
      if (perfOptions.pageLoadTraceId) {
        tr.traceId = perfOptions.pageLoadTraceId
      }
      if (perfOptions.pageLoadSampled) {
        tr.sampled = perfOptions.pageLoadSampled
      }
      /**
       * The name must be set as soon as the transaction is started
       * Ex: Helps to decide sampling based on name
       */
      if (tr.name === NAME_UNKNOWN && perfOptions.pageLoadTransactionName) {
        tr.name = perfOptions.pageLoadTransactionName
      }
    }
    /**
     * Start observing for long tasks for all managed transactions
     */
    if (!isRedefined && this._config.get('monitorLongtasks')) {
      this.recorder.start(LONG_TASK)
    }
    /**
     * For unsampled transactions, avoid capturing various timing information
     * as spans since they are dropped before sending to the server
     */
    if (tr.sampled) {
      tr.captureTimings = true
    }

    return tr
  }