in glean/src/core/glean.ts [224:371]
export function initialize(
applicationId: string,
uploadEnabled: boolean,
config?: ConfigurationInterface
): void {
if (Context.initialized) {
log(
LOG_TAG,
"Attempted to initialize Glean, but it has already been initialized. Ignoring.",
LoggingLevel.Warn
);
return;
}
if (!isString(applicationId)) {
log(
LOG_TAG,
"Unable to initialize Glean, applicationId must be a string.",
LoggingLevel.Error
);
return;
}
if (!isBoolean(uploadEnabled)) {
log(
LOG_TAG,
"Unable to initialize Glean, uploadEnabled must be a boolean.",
LoggingLevel.Error
);
return;
}
if (applicationId.length === 0) {
log(
LOG_TAG,
"Unable to initialize Glean, applicationId cannot be an empty string.",
LoggingLevel.Error
);
return;
}
if (!Context.platform) {
log(
LOG_TAG,
"Unable to initialize Glean, platform has not been set.",
LoggingLevel.Error
);
return;
}
Context.coreMetrics = new CoreMetrics();
Context.corePings = new CorePings();
Context.applicationId = sanitizeApplicationId(applicationId);
// The configuration constructor will throw in case config has any incorrect prop.
Context.config = new Configuration(config);
// Pre-set debug options for Glean from browser SessionStorage values.
setDebugOptionsFromSessionStorage();
if (preInitLogPings) Context.config.logPings = preInitLogPings;
if (preInitDebugViewTag) Context.config.debugViewTag = preInitDebugViewTag;
if (preInitSourceTags) Context.config.sourceTags = preInitSourceTags;
Context.metricsDatabase = new MetricsDatabase();
Context.eventsDatabase = new EventsDatabase();
Context.pingsDatabase = new PingsDatabase();
Context.errorManager = new ErrorManager();
pingUploader = new PingUploadManager(Context.config, Context.pingsDatabase);
Context.initialized = true;
Context.uploadEnabled = uploadEnabled;
// Initialize the events database.
//
// It's important this happens _after_ the upload state is set,
// because initializing the events database may record the execution_counter and
// glean.restarted metrics. If the upload state is not defined these metrics cannot be recorded.
//
// This may also submit an 'events' ping,
// so it also needs to happen before application lifetime metrics are cleared.
Context.eventsDatabase.initialize();
// The upload enabled flag may have changed since the last run, for
// example by the changing of a config file.
if (uploadEnabled) {
// IMPORTANT!
// Any pings we want to send upon initialization should happen before this line.
//
// Clear application lifetime metrics.
//
// If upload is disabled we don't need to do this,
// all metrics will be cleared anyways and we want
// application lifetime metrics intact in case
// we need to send a deletion-request ping.
Context.metricsDatabase.clear(Lifetime.Application);
// If upload is enabled,
// just follow the normal code path to instantiate the core metrics.
onUploadEnabled();
Context.coreMetrics.initialize();
// Record a page load event if the client has auto page-load events enabled.
if (config?.enableAutoPageLoadEvents) {
// This function call has no parameters because auto-instrumentation
// means there are no overrides.
GleanMetrics.pageLoad();
}
// Record click events if the client has auto element click events enabled.
if (config?.enableAutoElementClickEvents) {
document.addEventListener("click", (event) => {
GleanMetrics.handleClickEvent(event);
});
}
} else {
// If upload is disabled, and we've never run before, only set the
// client_id to KNOWN_CLIENT_ID, but do not send a deletion request
// ping.
// If we have run before, and if the client_id is not equal to
// the KNOWN_CLIENT_ID, do the full upload disabled operations to
// clear metrics, set the client_id to KNOWN_CLIENT_ID, and send a
// deletion request ping.
const clientId = Context.metricsDatabase.getMetric(
CLIENT_INFO_STORAGE,
Context.coreMetrics.clientId
);
if (clientId) {
if (clientId !== KNOWN_CLIENT_ID) {
onUploadDisabled(true);
}
} else {
// Call `clearMetrics` directly here instead of `onUploadDisabled` to avoid sending
// a deletion-request ping for a user that has already done that.
clearMetrics();
}
}
// We only scan the pending pings **after** dealing with the upload state.
// If upload is disabled, pending pings files are deleted
// so we need to know that state **before** scanning the pending pings
// to ensure we don't enqueue pings before their files are deleted.
Context.pingsDatabase.scanPendingPings();
}