@@ -9,20 +9,24 @@ import { Lifetime } from "../metrics/index.js";
99import TimeUnit from "../metrics/time_unit.js" ;
1010import { ClientInfo , PingInfo , PingPayload } from "../pings/ping_payload.js" ;
1111import PingType from "../pings/index.js" ;
12- import Glean from "../glean.js" ;
1312import CoreEvents from "../events/index.js" ;
13+ import MetricsDatabase from "../metrics/database.js" ;
14+ import EventsDatabase from "../metrics/events_database.js" ;
15+ import { DebugOptions } from "../debug_options.js" ;
16+ import PingsDatabase from "./database.js" ;
1417
1518// The moment the current Glean.js session started.
1619const GLEAN_START_TIME = new Date ( ) ;
1720
1821/**
1922 * Gets, and then increments, the sequence number for a given ping.
2023 *
24+ * @param metricsDatabase The metrics database.
2125 * @param ping The ping for which we want to get the sequence number.
2226 *
2327 * @returns The current number (before incrementing).
2428 */
25- export async function getSequenceNumber ( ping : PingType ) : Promise < number > {
29+ export async function getSequenceNumber ( metricsDatabase : MetricsDatabase , ping : PingType ) : Promise < number > {
2630 const seq = new CounterMetricType ( {
2731 category : "" ,
2832 name : `${ ping . name } #sequence` ,
@@ -31,7 +35,7 @@ export async function getSequenceNumber(ping: PingType): Promise<number> {
3135 disabled : false
3236 } ) ;
3337
34- const currentSeqData = await Glean . metricsDatabase . getMetric ( PING_INFO_STORAGE , seq ) ;
38+ const currentSeqData = await metricsDatabase . getMetric ( PING_INFO_STORAGE , seq ) ;
3539 await CounterMetricType . _private_addUndispatched ( seq , 1 ) ;
3640
3741 if ( currentSeqData ) {
@@ -53,11 +57,12 @@ export async function getSequenceNumber(ping: PingType): Promise<number> {
5357 * Gets the formatted start and end times for this ping
5458 * and updates for the next ping.
5559 *
60+ * @param metricsDatabase The metrics database.
5661 * @param ping The ping for which we want to get the times.
5762 *
5863 * @returns An object containing start and times in their payload format.
5964 */
60- export async function getStartEndTimes ( ping : PingType ) : Promise < { startTime : string , endTime : string } > {
65+ export async function getStartEndTimes ( metricsDatabase : MetricsDatabase , ping : PingType ) : Promise < { startTime : string , endTime : string } > {
6166 const start = new DatetimeMetricType ( {
6267 category : "" ,
6368 name : `${ ping . name } #start` ,
@@ -68,7 +73,7 @@ export async function getStartEndTimes(ping: PingType): Promise<{ startTime: str
6873
6974 // "startTime" is the time the ping was generated the last time.
7075 // If not available, we use the date the Glean object was initialized.
71- const startTimeData = await Glean . metricsDatabase . getMetric ( PING_INFO_STORAGE , start ) ;
76+ const startTimeData = await metricsDatabase . getMetric ( PING_INFO_STORAGE , start ) ;
7277 let startTime : DatetimeMetric ;
7378 if ( startTimeData ) {
7479 startTime = new DatetimeMetric ( startTimeData ) ;
@@ -90,14 +95,15 @@ export async function getStartEndTimes(ping: PingType): Promise<{ startTime: str
9095/**
9196 * Builds the `ping_info` section of a ping.
9297 *
98+ * @param metricsDatabase The metrics database.
9399 * @param ping The ping to build the `ping_info` section for.
94100 * @param reason The reason for submitting this ping.
95101 *
96102 * @returns The final `ping_info` section in its payload format.
97103 */
98- export async function buildPingInfoSection ( ping : PingType , reason ?: string ) : Promise < PingInfo > {
99- const seq = await getSequenceNumber ( ping ) ;
100- const { startTime, endTime } = await getStartEndTimes ( ping ) ;
104+ export async function buildPingInfoSection ( metricsDatabase : MetricsDatabase , ping : PingType , reason ?: string ) : Promise < PingInfo > {
105+ const seq = await getSequenceNumber ( metricsDatabase , ping ) ;
106+ const { startTime, endTime } = await getStartEndTimes ( metricsDatabase , ping ) ;
101107
102108 const pingInfo : PingInfo = {
103109 seq,
@@ -115,12 +121,13 @@ export async function buildPingInfoSection(ping: PingType, reason?: string): Pro
115121/**
116122 * Builds the `client_info` section of a ping.
117123 *
124+ * @param metricsDatabase The metrics database.
118125 * @param ping The ping to build the `client_info` section for.
119126 *
120127 * @returns The final `client_info` section in its payload format.
121128 */
122- export async function buildClientInfoSection ( ping : PingType ) : Promise < ClientInfo > {
123- let clientInfo = await Glean . metricsDatabase . getPingMetrics ( CLIENT_INFO_STORAGE , true ) ;
129+ export async function buildClientInfoSection ( metricsDatabase : MetricsDatabase , ping : PingType ) : Promise < ClientInfo > {
130+ let clientInfo = await metricsDatabase . getPingMetrics ( CLIENT_INFO_STORAGE , true ) ;
124131 if ( ! clientInfo ) {
125132 // TODO: Watch Bug 1685705 and change behaviour in here accordingly.
126133 console . warn ( "Empty client info data. Will submit anyways." ) ;
@@ -151,18 +158,20 @@ export async function buildClientInfoSection(ping: PingType): Promise<ClientInfo
151158 * - [X-Debug-ID]
152159 * - [X-Source-Tags]
153160 *
161+ * @param debugOptions The debug options.
162+ *
154163 * @returns An object containing all the headers and their values
155164 * or `undefined` in case no custom headers were set.
156165 */
157- export function getPingHeaders ( ) : Record < string , string > | undefined {
166+ export function getPingHeaders ( debugOptions ?: DebugOptions ) : Record < string , string > | undefined {
158167 const headers : Record < string , string > = { } ;
159168
160- if ( Glean . debugViewTag ) {
161- headers [ "X-Debug-ID" ] = Glean . debugViewTag ;
169+ if ( debugOptions ? .debugViewTag ) {
170+ headers [ "X-Debug-ID" ] = debugOptions . debugViewTag ;
162171 }
163172
164- if ( Glean . sourceTags ) {
165- headers [ "X-Source-Tags" ] = Glean . sourceTags ;
173+ if ( debugOptions ? .sourceTags ) {
174+ headers [ "X-Source-Tags" ] = debugOptions . sourceTags . toString ( ) ;
166175 }
167176
168177 if ( Object . keys ( headers ) . length > 0 ) {
@@ -173,15 +182,17 @@ export function getPingHeaders(): Record<string, string> | undefined {
173182/**
174183 * Collects a snapshot for the given ping from storage and attach required meta information.
175184 *
185+ * @param metricsDatabase The metrics database.
186+ * @param eventsDatabase The events database.
176187 * @param ping The ping to collect for.
177188 * @param reason An optional reason code to include in the ping.
178189 *
179190 * @returns A fully assembled JSON representation of the ping payload.
180191 * If there is no data stored for the ping, `undefined` is returned.
181192 */
182- export async function collectPing ( ping : PingType , reason ?: string ) : Promise < PingPayload | undefined > {
183- const metricsData = await Glean . metricsDatabase . getPingMetrics ( ping . name , true ) ;
184- const eventsData = await Glean . eventsDatabase . getPingEvents ( ping . name , true ) ;
193+ export async function collectPing ( metricsDatabase : MetricsDatabase , eventsDatabase : EventsDatabase , ping : PingType , reason ?: string ) : Promise < PingPayload | undefined > {
194+ const metricsData = await metricsDatabase . getPingMetrics ( ping . name , true ) ;
195+ const eventsData = await eventsDatabase . getPingEvents ( ping . name , true ) ;
185196 if ( ! metricsData && ! eventsData && ! ping . sendIfEmpty ) {
186197 console . info ( `Storage for ${ ping . name } empty. Bailing out.` ) ;
187198 return ;
@@ -191,8 +202,8 @@ export async function collectPing(ping: PingType, reason?: string): Promise<Ping
191202
192203 const metrics = metricsData ? { metrics : metricsData } : { } ;
193204 const events = eventsData ? { events : eventsData } : { } ;
194- const pingInfo = await buildPingInfoSection ( ping , reason ) ;
195- const clientInfo = await buildClientInfoSection ( ping ) ;
205+ const pingInfo = await buildPingInfoSection ( metricsDatabase , ping , reason ) ;
206+ const clientInfo = await buildClientInfoSection ( metricsDatabase , ping ) ;
196207 return {
197208 ...metrics ,
198209 ...events ,
@@ -204,18 +215,17 @@ export async function collectPing(ping: PingType, reason?: string): Promise<Ping
204215/**
205216 * Build a pings submition path.
206217 *
218+ * @param applicationId The application id.
207219 * @param identifier The pings UUID identifier.
208220 * @param ping The ping to build a path for.
209221 *
210222 * @returns The final submission path.
211223 */
212- export function makePath ( identifier : string , ping : PingType ) : string {
224+ export function makePath ( applicationId : string , identifier : string , ping : PingType ) : string {
213225 // We are sure that the applicationId is not `undefined` at this point,
214226 // this function is only called when submitting a ping
215227 // and that function return early when Glean is not initialized.
216- //
217- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
218- return `/submit/${ Glean . applicationId ! } /${ ping . name } /${ GLEAN_SCHEMA_VERSION } /${ identifier } ` ;
228+ return `/submit/${ applicationId } /${ ping . name } /${ GLEAN_SCHEMA_VERSION } /${ identifier } ` ;
219229}
220230
221231/**
@@ -225,14 +235,19 @@ export function makePath(identifier: string, ping: PingType): string {
225235 * This event is triggered **after** logging the ping, which happens if `logPings` is set.
226236 * We will log the payload before it suffers any change by plugins listening to this event.
227237 *
238+ * @param metricsDatabase The metrics database.
239+ * @param eventsDatabase The events database.
240+ * @param pingsDatabase The pings database.
241+ * @param applicationId The user-provided application id.
228242 * @param identifier The pings UUID identifier.
229243 * @param ping The ping to submit.
230244 * @param reason An optional reason code to include in the ping.
245+ * @param debugOptions The debug options.
231246 *
232247 * @returns A promise that is resolved once collection and storing is done.
233248 */
234- export async function collectAndStorePing ( identifier : string , ping : PingType , reason ?: string ) : Promise < void > {
235- const collectedPayload = await collectPing ( ping , reason ) ;
249+ export async function collectAndStorePing ( metricsDatabase : MetricsDatabase , eventsDatabase : EventsDatabase , pingsDatabase : PingsDatabase , applicationId : string , identifier : string , ping : PingType , reason ?: string , debugOptions ?: DebugOptions ) : Promise < void > {
250+ const collectedPayload = await collectPing ( metricsDatabase , eventsDatabase , ping , reason ) ;
236251 if ( ! collectedPayload ) {
237252 return ;
238253 }
@@ -251,13 +266,13 @@ export async function collectAndStorePing(identifier: string, ping: PingType, re
251266 return ;
252267 }
253268
254- if ( Glean . logPings ) {
269+ if ( debugOptions ? .logPings ) {
255270 console . info ( JSON . stringify ( collectedPayload , null , 2 ) ) ;
256271 }
257272 const finalPayload = modifiedPayload ? modifiedPayload : collectedPayload ;
258- const headers = getPingHeaders ( ) ;
259- return Glean . pingsDatabase . recordPing (
260- makePath ( identifier , ping ) ,
273+ const headers = getPingHeaders ( debugOptions ) ;
274+ return pingsDatabase . recordPing (
275+ makePath ( applicationId , identifier , ping ) ,
261276 identifier ,
262277 finalPayload ,
263278 headers
0 commit comments