setRaw()

in glean/src/core/metrics/types/timespan.ts [200:250]


  setRaw(elapsed: number) {
    if (!this.shouldRecord(Context.uploadEnabled)) {
      return;
    }

    if (!isUndefined(this.startTime)) {
      Context.errorManager.record(
        this,
        ErrorType.InvalidState,
        "Timespan already running. Raw value not recorded."
      );
      return;
    }

    let reportValueExists = false;
    try {
      const transformFn = ((elapsed) => {
        return (old?: JSONValue): TimespanMetric => {
          let metric: TimespanMetric;
          try {
            metric = new TimespanMetric(old);
            // If creating the metric didn't error,
            // there is a valid timespan already recorded for this metric.
            reportValueExists = true;
          } catch {
            // This may still throw in case elapsed in not the correct type.
            metric = new TimespanMetric({
              timespan: elapsed,
              timeUnit: this.timeUnit
            });
          }

          return metric;
        };
      })(elapsed);

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

    if (reportValueExists) {
      Context.errorManager.record(
        this,
        ErrorType.InvalidState,
        "Timespan value already recorded. New value discarded."
      );
    }
  }