export function collectPing()

in glean/src/core/pings/maker.ts [235:284]


export function collectPing(ping: CommonPingData, reason?: string): PingPayload | undefined {
  // !IMPORTANT! Events data needs to be collected BEFORE other metrics,
  // because events collection may result in recording of error metrics.
  const eventsData = Context.eventsDatabase.getPingEvents(ping.name, true);
  let metricsData = Context.metricsDatabase.getPingMetrics(
    ping.name,
    true
  );
  if (!metricsData && !eventsData) {
    if (!ping.sendIfEmpty) {
      log(PINGS_MAKER_LOG_TAG, `Storage for ${ping.name} empty. Bailing out.`, LoggingLevel.Info);
      return;
    }
    log(
      PINGS_MAKER_LOG_TAG,
      `Storage for ${ping.name} empty. Ping will still be sent.`,
      LoggingLevel.Info
    );
  }

  // Insert the experimentation id if the metrics aren't empty
  if (ping.includeClientId && Context.config.experimentationId) {
    if (metricsData !== undefined) {
      metricsData = {
        ...metricsData,
        string: {
          ...metricsData?.string || undefined,
          "glean.client.annotation.experimentation_id": Context.config.experimentationId
        }
      };
    } else {
      metricsData = {
        "string": {
          "glean.client.annotation.experimentation_id": Context.config.experimentationId
        }
      };
    }
  }

  const metrics = metricsData ? { metrics: metricsData } : {};
  const events = eventsData ? { events: eventsData } : {};
  const pingInfo = buildPingInfoSection(ping, reason);
  const clientInfo = buildClientInfoSection(ping);
  return {
    ...metrics,
    ...events,
    ping_info: pingInfo,
    client_info: clientInfo
  };
}