@@ -23,15 +23,6 @@ import log, { LoggingLevel } from "./log.js";
2323const LOG_TAG = "core.Glean" ;
2424
2525namespace Glean {
26- // The below properties are exported for testing purposes.
27- //
28- // Instances of Glean's core metrics.
29- //
30- // Disabling the lint, because we will actually re-assign this variable in the testInitializeGlean API.
31- // eslint-disable-next-line prefer-const
32- export let coreMetrics = new CoreMetrics ( ) ;
33- // Instances of Glean's core pings.
34- export const corePings = new CorePings ( ) ;
3526 // An instance of the ping uploader.
3627 export let pingUploader : PingUploadManager ;
3728
@@ -51,7 +42,7 @@ namespace Glean {
5142 */
5243 async function onUploadEnabled ( ) : Promise < void > {
5344 Context . uploadEnabled = true ;
54- await coreMetrics . initialize ( ) ;
45+ await Context . coreMetrics . initialize ( ) ;
5546 }
5647
5748 /**
@@ -70,7 +61,7 @@ namespace Glean {
7061 // We need to use an undispatched submission to guarantee that the
7162 // ping is collected before metric are cleared, otherwise we end up
7263 // with malformed pings.
73- await corePings . deletionRequest . submitUndispatched ( ) ;
64+ await Context . corePings . deletionRequest . submitUndispatched ( ) ;
7465 await clearMetrics ( ) ;
7566 }
7667
@@ -97,7 +88,7 @@ namespace Glean {
9788 firstRunDate = new DatetimeMetric (
9889 await Context . metricsDatabase . getMetric (
9990 CLIENT_INFO_STORAGE ,
100- coreMetrics . firstRunDate
91+ Context . coreMetrics . firstRunDate
10192 )
10293 ) . date ;
10394 } catch {
@@ -124,10 +115,10 @@ namespace Glean {
124115 // Store a "dummy" KNOWN_CLIENT_ID in the client_id metric. This will
125116 // make it easier to detect if pings were unintentionally sent after
126117 // uploading is disabled.
127- await coreMetrics . clientId . setUndispatched ( KNOWN_CLIENT_ID ) ;
118+ await Context . coreMetrics . clientId . setUndispatched ( KNOWN_CLIENT_ID ) ;
128119
129120 // Restore the first_run_date.
130- await coreMetrics . firstRunDate . setUndispatched ( firstRunDate ) ;
121+ await Context . coreMetrics . firstRunDate . setUndispatched ( firstRunDate ) ;
131122
132123 Context . uploadEnabled = false ;
133124 }
@@ -194,6 +185,9 @@ namespace Glean {
194185 return ;
195186 }
196187
188+ Context . coreMetrics = new CoreMetrics ( ) ;
189+ Context . corePings = new CorePings ( ) ;
190+
197191 Context . applicationId = sanitizeApplicationId ( applicationId ) ;
198192
199193 // The configuration constructor will throw in case config has any incorrect prop.
@@ -224,14 +218,20 @@ namespace Glean {
224218 //
225219 // The dispatcher will catch and log any exceptions.
226220 Context . dispatcher . flushInit ( async ( ) => {
227- // We need to mark Glean as initialized before dealing with the upload status,
228- // otherwise we will not be able to submit deletion-request pings if necessary.
229- //
230- // This is fine, we are inside a dispatched task that is guaranteed to run before any
231- // other task. No external API call will be executed before we leave this task.
232221 Context . initialized = true ;
233222
234223 Context . uploadEnabled = uploadEnabled ;
224+
225+ // Initialize the events database.
226+ //
227+ // It's important this happens _after_ the upload state is set,
228+ // because initializing the events database may record the execution_counter and
229+ // glean.restarted metrics. If the upload state is not defined these metrics cannot be recorded.
230+ //
231+ // This may also submit an 'events' ping,
232+ // so it also needs to happen before application lifetime metrics are cleared.
233+ await Context . eventsDatabase . initialize ( ) ;
234+
235235 // The upload enabled flag may have changed since the last run, for
236236 // example by the changing of a config file.
237237 if ( uploadEnabled ) {
@@ -259,7 +259,7 @@ namespace Glean {
259259 // deletion request ping.
260260 const clientId = await Context . metricsDatabase . getMetric (
261261 CLIENT_INFO_STORAGE ,
262- coreMetrics . clientId
262+ Context . coreMetrics . clientId
263263 ) ;
264264
265265 if ( clientId ) {
@@ -273,13 +273,6 @@ namespace Glean {
273273 }
274274 }
275275
276- // Initialize the events database.
277- //
278- // It's important this happens _after_ the upload state is dealt with,
279- // because initializing the events database may record the execution_counter and
280- // glean.restarted metrics. If the upload state is not defined these metrics can't be recorded.
281- await Context . eventsDatabase . initialize ( ) ;
282-
283276 // We only scan the pendings pings **after** dealing with the upload state.
284277 // If upload is disabled, pending pings files are deleted
285278 // so we need to know that state **before** scanning the pending pings
0 commit comments