/// <summary> /// Enable or disable Glean collection and upload. /// /// Metric collection is enabled by default. /// /// When uploading is disabled, metrics aren't recorded at all and no data /// is uploaded. /// /// When disabling, all pending metrics, events and queued pings are cleared /// and a `deletion-request` is generated. /// /// When enabling, the core Glean metrics are recreated. /// </summary> /// <param name="enabled">When `true`, enable metric collection.</param> public void SetUploadEnabled(bool enabled) { if (!this.initFinished) { string msg = "Changing upload enabled before Glean is initialized is not supported.\n" + "Pass the correct state into `Glean.initialize()`.\n" + "See documentation at https://mozilla.github.io/glean/book/user/general-api.html#initializing-the-glean-sdk"; Log.Error(msg); return; } // Changing upload enabled always happens asynchronous. // That way it follows what a user expect when calling it inbetween other calls: // It executes in the right order. // // Because the dispatch queue is halted until Glean is fully initialized // we can safely enqueue here and it will execute after initialization. Dispatchers.LaunchAPI(() => { bool originalEnabled = this.GetUploadEnabled(); LibGleanFFI.glean_set_upload_enabled(enabled); if (!enabled) { // Cancel any pending workers here so that we don't accidentally upload or // collect data after the upload has been disabled. // TODO: metricsPingScheduler.cancel() // Cancel any pending workers here so that we don't accidentally upload // data after the upload has been disabled. httpClient.CancelUploads(); } if (!originalEnabled && enabled) { // If uploading is being re-enabled, we have to restore the // application-lifetime metrics. InitializeCoreMetrics(); } if (originalEnabled && !enabled) { // If uploading is disabled, we need to send the deletion-request ping httpClient.TriggerUpload(configuration); } }); }
/// <summary> /// Enable or disable Glean collection and upload. /// /// Metric collection is enabled by default. /// /// When uploading is disabled, metrics aren't recorded at all and no data /// is uploaded. /// /// When disabling, all pending metrics, events and queued pings are cleared /// and a `deletion-request` is generated. /// /// When enabling, the core Glean metrics are recreated. /// </summary> /// <param name="enabled">When `true`, enable metric collection.</param> public void SetUploadEnabled(bool enabled) { if (IsInitialized()) { bool originalEnabled = GetUploadEnabled(); Dispatchers.LaunchAPI(() => { LibGleanFFI.glean_set_upload_enabled(enabled); if (!enabled) { // Cancel any pending workers here so that we don't accidentally upload or // collect data after the upload has been disabled. // TODO: metricsPingScheduler.cancel() // Cancel any pending workers here so that we don't accidentally upload // data after the upload has been disabled. httpClient.CancelUploads(); } if (!originalEnabled && enabled) { // If uploading is being re-enabled, we have to restore the // application-lifetime metrics. InitializeCoreMetrics(); } if (originalEnabled && !enabled) { // If uploading is disabled, we need to send the deletion-request ping httpClient.TriggerUpload(configuration); } }); } else { uploadEnabled = enabled; } }