export function getSequenceNumber()

in glean/src/core/pings/maker.ts [114:146]


export function getSequenceNumber(ping: CommonPingData): number {
  const seq = new CounterMetricType({
    category: "",
    name: `${ping.name}#sequence`,
    sendInPings: [PING_INFO_STORAGE],
    lifetime: Lifetime.User,
    disabled: false
  });

  const currentSeqData = Context.metricsDatabase.getMetric(
    PING_INFO_STORAGE,
    seq
  );
  seq.add(1);

  if (currentSeqData) {
    // Creating a new counter metric validates that the metric stored is actually a number.
    // When we `add` we deal with getting rid of that number from storage,
    // no need to worry about that here.
    try {
      const metric = new CounterMetric(currentSeqData);
      return metric.payload();
    } catch (e) {
      log(
        PINGS_MAKER_LOG_TAG,
        `Unexpected value found for sequence number in ping ${ping.name}. Ignoring.`,
        LoggingLevel.Warn
      );
    }
  }

  return 0;
}