22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
5+ import { DEFAULT_TELEMETRY_ENDPOINT } from "./constants" ;
6+ import Configuration from "config" ;
57import MetricsDatabase from "metrics/database" ;
68import PingsDatabase from "pings/database" ;
9+ import PingUploader from "upload" ;
710import { isUndefined , sanitizeApplicationId } from "utils" ;
811import { CoreMetrics } from "internal_metrics" ;
912import { Lifetime } from "metrics" ;
@@ -23,9 +26,12 @@ class Glean {
2326 private _initialized : boolean ;
2427 // Instances of Glean's core metrics.
2528 private _coreMetrics : CoreMetrics ;
29+ // The ping uploader.
30+ private _pingUploader : PingUploader
2631
2732 // Properties that will only be set on `initialize`.
2833 private _applicationId ?: string ;
34+ private _serverEndpoint ?: URL ;
2935
3036 private constructor ( ) {
3137 if ( ! isUndefined ( Glean . _instance ) ) {
@@ -34,16 +40,21 @@ class Glean {
3440 Use Glean.instance instead to access the Glean singleton.` ) ;
3541 }
3642
43+ this . _pingUploader = new PingUploader ( ) ;
3744 this . _coreMetrics = new CoreMetrics ( ) ;
3845 this . _initialized = false ;
3946 this . _db = {
4047 metrics : new MetricsDatabase ( ) ,
41- pings : new PingsDatabase ( )
48+ pings : new PingsDatabase ( this . _pingUploader )
4249 } ;
4350 // Temporarily setting this to true always, until Bug 1677444 is resolved.
4451 this . _uploadEnabled = true ;
4552 }
4653
54+ private static get pingUploader ( ) : PingUploader {
55+ return Glean . instance . _pingUploader ;
56+ }
57+
4758 private static get coreMetrics ( ) : CoreMetrics {
4859 return Glean . instance . _coreMetrics ;
4960 }
@@ -60,19 +71,24 @@ class Glean {
6071 * Initialize Glean. This method should only be called once, subsequent calls will be no-op.
6172 *
6273 * @param applicationId The application ID (will be sanitized during initialization).
63- * @param appBuild The build identifier generated by the CI system (e.g. "1234/A").
64- * @param appDisplayVersion The user visible version string fro the application running Glean.js.
74+ * @param config Glean configuration options.
6575 */
66- static async initialize ( applicationId : string , appBuild ?: string , appDisplayVersion ?: string ) : Promise < void > {
76+ static async initialize ( applicationId : string , config ?: Configuration ) : Promise < void > {
6777 if ( Glean . instance . _initialized ) {
6878 console . warn ( "Attempted to initialize Glean, but it has already been initialized. Ignoring." ) ;
6979 return ;
7080 }
7181
7282 Glean . instance . _applicationId = sanitizeApplicationId ( applicationId ) ;
73- await Glean . coreMetrics . initialize ( appBuild , appDisplayVersion ) ;
83+ Glean . instance . _serverEndpoint = config
84+ ? config . serverEndpoint : new URL ( DEFAULT_TELEMETRY_ENDPOINT ) ;
85+ await Glean . coreMetrics . initialize ( config ?. appBuild , config ?. appDisplayVersion ) ;
7486
7587 Glean . instance . _initialized = true ;
88+
89+ await Glean . pingUploader . scanPendingPings ( ) ;
90+ // Even though this returns a promise, there is no need to block on it returning.
91+ Glean . pingUploader . triggerUpload ( ) ;
7692 }
7793
7894 /**
@@ -119,6 +135,14 @@ class Glean {
119135 return Glean . instance . _applicationId ;
120136 }
121137
138+ static get serverEndpoint ( ) : URL | undefined {
139+ if ( ! Glean . instance . _initialized ) {
140+ console . error ( "Attempted to access the Glean.serverEndpoint before Glean was initialized." ) ;
141+ }
142+
143+ return Glean . instance . _serverEndpoint ;
144+ }
145+
122146 /**
123147 * **Test-only API**
124148 *
@@ -139,19 +163,23 @@ class Glean {
139163 * TODO: Only allow this function to be called on test mode (depends on Bug 1682771).
140164 *
141165 * @param applicationId The application ID (will be sanitized during initialization).
142- * @param optionalInitializeArgs Optional arguments to be passed to `initialize` .
166+ * @param config Glean configuration options .
143167 */
144- static async testRestGlean ( applicationId : string , ... optionalInitializeArgs : never [ ] ) : Promise < void > {
168+ static async testRestGlean ( applicationId : string , config ?: Configuration ) : Promise < void > {
145169 // Get back to an uninitialized state.
146170 Glean . instance . _initialized = false ;
147171 // Reset upload enabled state, not to inerfere with other tests.
148172 Glean . uploadEnabled = true ;
173+
174+ // Stop ongoing jobs and clear pending pings queue.
175+ await Glean . pingUploader . clearPendingPingsQueue ( ) ;
176+
149177 // Clear the databases.
150178 await Glean . metricsDatabase . clearAll ( ) ;
151179 await Glean . pingsDatabase . clearAll ( ) ;
152180
153- // Initialize Glean.
154- await Glean . initialize ( applicationId , ... optionalInitializeArgs ) ;
181+ // Re- Initialize Glean.
182+ await Glean . initialize ( applicationId , config ) ;
155183 }
156184}
157185
0 commit comments