setStopAndAccumulate()

in glean/src/core/metrics/types/timing_distribution.ts [166:221]


  setStopAndAccumulate(id: number, stopTime: number) {
    if (!this.shouldRecord(Context.uploadEnabled)) {
      delete this.startTimes[id];
      return;
    }

    const startTime = this.startTimes[id];
    if (startTime !== undefined) {
      delete this.startTimes[id];
    } else {
      Context.errorManager.record(
        this,
        ErrorType.InvalidState,
        "Timing not running"
      );
      return;
    }

    // Duration is in nanoseconds.
    let duration = stopTime - startTime;
    if (duration < 0) {
      Context.errorManager.record(
        this,
        ErrorType.InvalidValue,
        "Timer stopped with negative duration"
      );
      return;
    }

    const minSampleTime = convertTimeUnitToNanos(1, this.timeUnit as TimeUnit);
    const maxSampleTime = convertTimeUnitToNanos(MAX_SAMPLE_TIME, this.timeUnit as TimeUnit);

    if (duration < minSampleTime) {
      // If measurement is less than the minimum, just truncate. This is
      // not recorded as an error.
      duration = minSampleTime;
    } else if (duration > maxSampleTime) {
      Context.errorManager.record(
        this,
        ErrorType.InvalidState,
        `Sample is longer than the max for a timeUnit of ${this.timeUnit} (${duration} ns)`
      );
      duration = maxSampleTime;
    }

    try {
      Context.metricsDatabase.transform(
        this,
        this.setStopAndAccumulateTransformFn(duration)
      );
    } catch (e) {
      if (e instanceof MetricValidationError) {
        e.recordError(this);
      }
    }
  }