def test_core_metrics_should_be_cleared_with_disabling_and_enabling_uploading( ): assert _builtins.metrics.glean.internal.metrics.os.test_has_value() Glean.set_upload_enabled(False) assert not _builtins.metrics.glean.internal.metrics.os.test_has_value() Glean.set_upload_enabled(True) assert _builtins.metrics.glean.internal.metrics.os.test_has_value()
def test_setting_upload_enabled_before_initialization_should_not_crash(): Glean._reset() Glean.set_upload_enabled(True) Glean.initialize( application_id=GLEAN_APP_ID, application_version=glean_version, upload_enabled=True, )
def complete(self, space_ship: Discovery) -> None: # Wait for 5 seconds to wait for the upload of pending pings to # complete. This is required for testing the deletion-request ping # functionality when we join the discovery table with the # deletion-request table using the client ID to then verify that the # deletion-request ping was submitted with the expected client ID. time.sleep(5) Glean.set_upload_enabled(False)
def test_disabling_upload_sends_deletion_request(safe_httpserver): safe_httpserver.serve_content(b"", code=200) Glean._configuration.server_endpoint = safe_httpserver.url # Ensure nothing was received yet assert 0 == len(safe_httpserver.requests) # Disabling upload will trigger a deletion-request ping Glean.set_upload_enabled(False) assert 1 == len(safe_httpserver.requests)
def test_disabling_upload_should_disable_metrics_recording(): counter_metric = CounterMetricType( disabled=False, category="telemetry", lifetime=Lifetime.APPLICATION, name="counter_metric", send_in_pings=["store1"], ) Glean.set_upload_enabled(False) counter_metric.add(1) assert False is counter_metric.test_has_value()
def save_prefs(self): options = get_prefs() ui = self.ui options["persist"] = str(ui.persist.line_edit.text()) or None options["http_timeout"] = ui.http_timeout.value() options["persist_size_limit"] = ui.persist_size_limit.value() options["background_downloads"] = ui.bg_downloads.isChecked() options["approx_policy"] = ui.approx.isChecked() options["archive_base_url"] = str(ui.archive_base_url.text()) options["enable_telemetry"] = ui.enable_telemetry.isChecked() # if telemetry went from enabled to disabled, we will send a deletion # ping Glean.set_upload_enabled(options["enable_telemetry"]) save_prefs(options)
def test_flipping_upload_enabled_respects_order_of_events(tmpdir, monkeypatch): Glean._reset() info_path = Path(str(tmpdir)) / "info.txt" # We create a ping and a metric before we initialize Glean ping = PingType( name="sample_ping_1", include_client_id=True, send_if_empty=True, reason_codes=[], ) # This test relies on testing mode to be disabled, since we need to prove the # real-world async behaviour of this. Dispatcher._testing_mode = False Dispatcher._queue_initial_tasks = True configuration = Glean._configuration configuration.ping_uploader = _RecordingUploader(info_path) Glean._initialize_with_tempdir_for_testing( application_id=GLEAN_APP_ID, application_version=glean_version, upload_enabled=True, configuration=Glean._configuration, ) # Glean might still be initializing. Disable upload. Glean.set_upload_enabled(False) # Submit a custom ping. ping.submit() # Wait until the work is complete Dispatcher._task_worker._queue.join() while not info_path.exists(): time.sleep(0.1) with info_path.open("r") as fd: url_path = fd.readline() # Validate we got the deletion-request ping assert "deletion-request" == url_path.split("/")[3]
def test_basic_metrics_should_be_cleared_when_disabling_uploading(): counter_metric = CounterMetricType( disabled=False, category="telemetry", lifetime=Lifetime.APPLICATION, name="counter_metric", send_in_pings=["store1"], ) counter_metric.add(10) assert counter_metric.test_has_value() Glean.set_upload_enabled(False) assert not counter_metric.test_has_value() counter_metric.add(10) assert not counter_metric.test_has_value() Glean.set_upload_enabled(True) assert not counter_metric.test_has_value() counter_metric.add(10) assert counter_metric.test_has_value()
def test_events_should_not_record_when_upload_is_disabled(): class EventKeys(enum.Enum): TEST_NAME = 0 event_metric = metrics.EventMetricType( disabled=False, category="ui", lifetime=Lifetime.PING, name="click", send_in_pings=["store1"], allowed_extra_keys=["test_name"], ) Glean.set_upload_enabled(True) event_metric.record({EventKeys.TEST_NAME: "event1"}) snapshot1 = event_metric.test_get_value() assert 1 == len(snapshot1) Glean.set_upload_enabled(False) event_metric.record({EventKeys.TEST_NAME: "event2"}) with pytest.raises(ValueError): event_metric.test_get_value() Glean.set_upload_enabled(True) event_metric.record({EventKeys.TEST_NAME: "event3"}) snapshot3 = event_metric.test_get_value() assert 1 == len(snapshot3)
def test_dont_schedule_pings_if_metrics_disabled(safe_httpserver): safe_httpserver.serve_content(b"", code=200) counter_metric = CounterMetricType( disabled=False, category="telemetry", lifetime=Lifetime.APPLICATION, name="counter_metric", send_in_pings=["store1"], ) custom_ping = PingType( name="store1", include_client_id=True, send_if_empty=False, reason_codes=[] ) counter_metric.add(10) Glean.set_upload_enabled(False) custom_ping.submit() assert 0 == len(safe_httpserver.requests)
def complete(self, space_ship: Discovery) -> None: Glean.set_upload_enabled(True)
def disable(self, write=True): Glean.set_upload_enabled(False) config.telemetry_enabled = False if write: config.write()
def enable(self): Glean.set_upload_enabled(True) config.telemetry_enabled = True config.write()