xt = ping?.event_context;

    if (typeof event_context === "string") {
      try {
        event_context = JSON.parse(event_context);
      } catch (e) {
        // The Empty JSON strings and non-objects often provided by the
        // existing telemetry we need to send failing to parse do not fit in
        // the spirit of what this error is meant to capture. Instead, we want
        // to capture when what we got should have been an object,
        // but failed to parse.
        if (event_context.length && event_context.includes("{")) {
          Glean.messagingSystem.eventContextParseError.add(1);
        }
      }
    }

    // We echo certain properties from event_context into their own metrics
    // to aid analysis.
    if (event_context?.reason) {
      Glean.messagingSystem.eventReason.set(event_context.reason);
    }
    if (event_context?.page) {
      Glean.messagingSystem.eventPage.set(event_context.page);
    }
    if (event_context?.source) {
      Glean.messagingSystem.eventSource.set(event_context.source);
    }
    if (event_context?.screen_family) {
      Glean.messagingSystem.eventScreenFamily.set(event_context.screen_family);
    }
    // Screen_index was being coerced into a boolean value
    // which resulted in 0 (first screen index) being ignored.
    if (Number.isInteger(event_context?.screen_index)) {
      Glean.messagingSystem.eventScreenIndex.set(event_context.screen_index);
    }
    if (event_context?.screen_id) {
      Glean.messagingSystem.eventScreenId.set(event_context.screen_id);
    }
    if (event_context?.screen_initials) {
      Glean.messagingSystem.eventScreenInitials.set(
        event_context.screen_initials
      );
    }

    // The event_context is also provided as-is as stringified JSON.
    if (event_context) {
      Glean.messagingSystem.eventContext.set(JSON.stringify(event_context));
    }

    if ("attribution" in ping) {
      for (const [key, value] of Object.entries(ping.attribution)) {
        const camelKey = this._snakeToCamelCase(key);
        try {
          Glean.messagingSystemAttribution[camelKey].set(value);
        } catch (e) {
          // We here acknowledge that we don't know the full breadth of data
          // being collected. Ideally AttributionCode will later centralize
          // definition and reporting of attribution data and we can be rid of
          // this fail-safe for collecting the names of unknown keys.
          Glean.messagingSystemAttribution.unknownKeys[camelKey].add(1);
        }
      }
    }

    // List of keys handled above.
    const handledKeys = ["event_context", "attribution"];

    for (const [key, value] of Object.entries(ping)) {
      if (handledKeys.includes(key)) {
        continue;
      }
      const camelKey = this._snakeToCamelCase(key);
      try {
        // We here acknowledge that even known keys might have non-scalar
        // values. We're pretty sure we handled them all with handledKeys,
        // but we might not have.
        // Ideally this can later be removed after running for a version or two
        // with no values seen in messaging_system.invalid_nested_data
        if (typeof value === "object") {
          Glean.messagingSystem.invalidNestedData[camelKey].add(1);
        } else {
          Glean.messagingSystem[camelKey].set(value);
        }
      } catch (e) {
        // We here acknowledge that we don't know the full breadth of data being
        // collected. Ideally we will later gain that confidence and can remove
        // this fail-safe for collecting the names of unknown keys.
        Glean.messagingSystem.unknownKeys[camelKey].add(1);
        // TODO(bug 1600008): For testing, also record the overall count.
        Glean.messagingSystem.unknownKeyCount.add(1);
      }
    }

    // With all the metrics set, now it's time to submit this ping.
    GleanPings.messagingSystem.submit();
  }

  _snakeToCamelCase(s) {
    return s.toString().replace(/_([a-z])/gi, (_str, group) => {
      return group.toUpperCase();
    });
  }
}
PK