def test_other_label_without_predefined_labels(ping_schema_url): labeled_counter_metric = metrics.LabeledCounterMetricType( disabled=False, category="telemetry", lifetime=Lifetime.APPLICATION, name="labeled_counter_metric", send_in_pings=["metrics"], ) for i in range(21): labeled_counter_metric["label_{}".format(i)].add(1) labeled_counter_metric["label_0"].add(1) assert 2 == labeled_counter_metric["label_0"].test_get_value() for i in range(1, 16): assert 1 == labeled_counter_metric["label_{}".format( i)].test_get_value() assert 5 == labeled_counter_metric["__other__"].test_get_value() json_content = Glean.test_collect(_builtins.pings.metrics) assert 0 == validate_ping.validate_ping(io.StringIO(json_content), sys.stdout, schema_url=ping_schema_url)
def test_labeled_string_type(ping_schema_url): labeled_string_metric = metrics.LabeledStringMetricType( disabled=False, category="telemetry", lifetime=Lifetime.APPLICATION, name="labeled_string_metric", send_in_pings=["metrics"], ) labeled_string_metric["label1"].set("foo") labeled_string_metric["label2"].set("bar") assert labeled_string_metric["label1"].test_has_value() assert "foo" == labeled_string_metric["label1"].test_get_value() assert labeled_string_metric["label2"].test_has_value() assert "bar" == labeled_string_metric["label2"].test_get_value() json_content = Glean.test_collect(_builtins.pings.metrics) assert 0 == validate_ping.validate_ping(io.StringIO(json_content), sys.stdout, schema_url=ping_schema_url) tree = json.loads(json_content) assert ("foo" == tree["metrics"]["labeled_string"] ["telemetry.labeled_string_metric"]["label1"]) assert ("bar" == tree["metrics"]["labeled_string"] ["telemetry.labeled_string_metric"]["label2"])
def test_collect(ping_schema_url): 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) json_content = Glean.test_collect(custom_ping) assert isinstance(json_content, str) json_tree = json.loads(json_content) assert 10 == json_tree["metrics"]["counter"]["telemetry.counter_metric"] assert 0 == validate_ping.validate_ping( io.StringIO(json_content), sys.stdout, schema_url=ping_schema_url )
def test_validate_ping(): content = { "experiments": { "experiment2": { "branch": "branch_b", "extra": { "key": "value" } } }, "metrics": { "string": { "telemetry.string_metric": "foo" } }, "ping_info": { "ping_type": "metrics", "telemetry_sdk_build": "0.32.0", "seq": 0, "app_build": "test-placeholder", "client_id": "900b6d8c-34d2-44d4-926d-83bde790474f", "start_time": "2018-11-19T16:19-05:00", "end_time": "2018-11-19T16:19-05:00", }, } input = io.StringIO(json.dumps(content)) output = io.StringIO() schema_url = ( "https://raw.githubusercontent.com/mozilla-services/" "mozilla-pipeline-schemas/3a15121c582ef0cffe430da024a5bf11b7c48740/" "schemas/glean/baseline/baseline.1.schema.json") assert validate_ping.validate_ping(input, output, schema_url=schema_url) == 0 with raises(TypeError): validate_ping.validate_ping(input, output)
def test_presubmit_makes_a_valid_ping(tmpdir, ping_schema_url, monkeypatch): # Bug 1648140: Submitting a ping prior to initialize meant that the core # metrics wouldn't yet be set. info_path = Path(str(tmpdir)) / "info.txt" Glean._reset() ping_name = "preinit_ping" ping = PingType(name=ping_name, 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 # Submit a ping prior to calling initialize ping.submit() Glean._initialize_with_tempdir_for_testing( application_id=GLEAN_APP_ID, application_version=glean_version, upload_enabled=True, configuration=Glean._configuration, ) monkeypatch.setattr(Glean._configuration, "ping_uploader", _RecordingUploader(info_path)) # 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() serialized_ping = fd.readline() print(url_path) assert ping_name == url_path.split("/")[3] assert 0 == validate_ping.validate_ping( io.StringIO(serialized_ping), sys.stdout, schema_url=ping_schema_url, )
def test_sending_deletion_ping_if_disabled_outside_of_run( tmpdir, ping_schema_url): info_path = Path(str(tmpdir)) / "info.txt" data_dir = Path(str(tmpdir)) / "glean" Glean._reset() Glean.initialize( application_id=GLEAN_APP_ID, application_version=glean_version, upload_enabled=True, data_dir=data_dir, configuration=Configuration( ping_uploader=_RecordingUploader(info_path)), ) Glean._reset() Glean.initialize( application_id=GLEAN_APP_ID, application_version=glean_version, upload_enabled=False, data_dir=data_dir, configuration=Configuration( ping_uploader=_RecordingUploader(info_path)), ) while not info_path.exists(): time.sleep(0.1) with info_path.open("r") as fd: url_path = fd.readline() serialized_ping = fd.readline() assert "deletion-request" == url_path.split("/")[3] json_content = json.loads(serialized_ping) assert 0 == validate_ping.validate_ping( io.StringIO(serialized_ping), sys.stdout, schema_url=ping_schema_url, ) assert not json_content["client_info"]["client_id"].startswith("c0ffee")
def test_other_label_with_predefined_labels(): labeled_counter_metric = metrics.LabeledCounterMetricType( disabled=False, category="telemetry", lifetime=Lifetime.APPLICATION, name="labeled_counter_metric", send_in_pings=["metrics"], labels=["foo", "bar", "baz"], ) labeled_counter_metric["foo"].add(1) labeled_counter_metric["foo"].add(2) labeled_counter_metric["bar"].add(1) labeled_counter_metric["not_there"].add(1) labeled_counter_metric["also_not_there"].add(1) labeled_counter_metric["not_me"].add(1) assert 3 == labeled_counter_metric["foo"].test_get_value() assert 1 == labeled_counter_metric["bar"].test_get_value() assert not labeled_counter_metric["baz"].test_has_value() assert 3 == labeled_counter_metric["not_there"].test_get_value() json_content = Glean.test_collect(_builtins.pings.metrics) assert 0 == validate_ping.validate_ping(io.StringIO(json_content), sys.stdout) tree = json.loads(json_content) assert ( 3 == tree["metrics"]["labeled_counter"]["telemetry.labeled_counter_metric"]["foo"] ) assert ( 1 == tree["metrics"]["labeled_counter"]["telemetry.labeled_counter_metric"]["bar"] ) assert ( 3 == tree["metrics"]["labeled_counter"]["telemetry.labeled_counter_metric"][ "__other__" ] )
def test_labeled_counter_type(): labeled_counter_metric = metrics.LabeledCounterMetricType( disabled=False, category="telemetry", lifetime=Lifetime.APPLICATION, name="labeled_counter_metric", send_in_pings=["metrics"], ) labeled_counter_metric["label1"].add(1) labeled_counter_metric["label2"].add(2) assert labeled_counter_metric["label1"].test_has_value() assert 1 == labeled_counter_metric["label1"].test_get_value() assert labeled_counter_metric["label2"].test_has_value() assert 2 == labeled_counter_metric["label2"].test_get_value() json_content = Glean.test_collect(_builtins.pings.metrics) assert 0 == validate_ping.validate_ping(io.StringIO(json_content), sys.stdout) tree = json.loads(json_content) assert ( 1 == tree["metrics"]["labeled_counter"]["telemetry.labeled_counter_metric"][ "label1" ] ) assert ( 2 == tree["metrics"]["labeled_counter"]["telemetry.labeled_counter_metric"][ "label2" ] )
def test_ping_collection_must_happen_after_currently_scheduled_metrics_recordings( tmpdir, ping_schema_url, ): # Given the following block of code: # # metrics.metric.a.set("SomeTestValue") # Glean.submit_pings(["custom-ping-1"]) # # This test ensures that "custom-ping-1" contains "metric.a" with a value of "SomeTestValue" # when the ping is collected. info_path = Path(str(tmpdir)) / "info.txt" real_uploader = Glean._configuration.ping_uploader test_uploader = _RecordingUploader(info_path) Glean._configuration.ping_uploader = test_uploader Glean._configuration.log_pings = True ping_name = "custom_ping_1" ping = PingType( name=ping_name, include_client_id=True, send_if_empty=False, reason_codes=[] ) string_metric = StringMetricType( disabled=False, category="category", lifetime=Lifetime.PING, name="string_metric", send_in_pings=[ping_name], ) # 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 # This is the important part of the test. Even though both the metrics API and # sendPings are async and off the main thread, "SomeTestValue" should be recorded, # the order of the calls must be preserved. test_value = "SomeTestValue" string_metric.set(test_value) 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() serialized_ping = fd.readline() assert ping_name == url_path.split("/")[3] json_content = json.loads(serialized_ping) assert 0 == validate_ping.validate_ping( io.StringIO(serialized_ping), sys.stdout, schema_url=ping_schema_url, ) assert {"category.string_metric": test_value} == json_content["metrics"]["string"] Glean._configuration.ping_uploader = real_uploader