Exemplo n.º 1
0
def test_queued_recorded_metrics_correctly_during_init():
    Glean._reset()

    # Enable queueing
    Dispatcher.set_task_queueing(True)

    counter_metric = CounterMetricType(
        disabled=False,
        category="telemetry",
        lifetime=Lifetime.APPLICATION,
        name="counter_metric",
        send_in_pings=["store1"],
    )

    for i in range(2):
        counter_metric.add()

    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        upload_enabled=True,
    )

    assert counter_metric.test_has_value()
    assert 2 == counter_metric.test_get_value()
Exemplo n.º 2
0
def test_initializing_twice_is_a_no_op():
    before_config = Glean._configuration

    Glean.initialize(application_id=GLEAN_APP_ID,
                     application_version=glean_version)

    assert before_config is Glean._configuration
Exemplo n.º 3
0
def reset_glean(*,
                application_id: str,
                application_version: str,
                configuration: Optional[Configuration] = None,
                clear_stores: bool = True):
    """
    Resets the Glean singleton.

    Args:
        application_id (str): The application id to use when sending pings.
        application_version (str): The version of the application sending
            Glean data.
        configuration (glean.config.Configuration): (optional) An object with
            global settings.
    """
    from glean import Glean
    from glean._dispatcher import Dispatcher

    Dispatcher._testing_mode = True

    data_dir = None  # type: Optional[Path]
    if not clear_stores:
        Glean._destroy_data_dir = False
        data_dir = Glean._data_dir

    Glean._reset()
    Glean.initialize(
        application_id=application_id,
        application_version=application_version,
        upload_enabled=True,
        configuration=configuration,
        data_dir=data_dir,
    )
Exemplo n.º 4
0
def test_no_sending_deletion_ping_if_unchanged_outside_of_run(safe_httpserver, tmpdir):
    safe_httpserver.serve_content(b"", code=200)
    Glean._reset()
    config = Configuration(server_endpoint=safe_httpserver.url)

    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        upload_enabled=False,
        data_dir=Path(str(tmpdir)),
        configuration=config,
    )

    assert 0 == len(safe_httpserver.requests)

    Glean._reset()

    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        upload_enabled=False,
        data_dir=Path(str(tmpdir)),
        configuration=config,
    )

    assert 0 == len(safe_httpserver.requests)
Exemplo n.º 5
0
def test_other_label_without_predefined_labels_before_glean_init():
    labeled_counter_metric = metrics.LabeledCounterMetricType(
        disabled=False,
        category="telemetry",
        lifetime=Lifetime.APPLICATION,
        name="labeled_counter_metric",
        send_in_pings=["metrics"],
    )

    Glean._reset()
    Dispatcher.set_task_queueing(True)

    for i in range(21):
        labeled_counter_metric["label_{}".format(i)].add(1)
    labeled_counter_metric["label_0"].add(1)

    Glean.initialize(
        application_id="glean-python-test",
        application_version=glean_version,
        upload_enabled=True,
    )

    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()
Exemplo n.º 6
0
def burnham(
    verbose: bool,
    test_run: str,
    test_name: str,
    enable_telemetry: bool,
    platform: str,
    spore_drive: str,
    missions: Tuple[Mission],
) -> None:
    """Travel through space and complete missions with the Discovery crew.

    If telemetry is enabled, measure, collect, and submit non-personal
    information to the specified data platform with Glean.
    """

    if verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    Glean.initialize(
        application_id=__title__,
        application_version=__version__,
        upload_enabled=enable_telemetry is True,
        data_dir=Path(TemporaryDirectory().name),
        configuration=Configuration(server_endpoint=platform),
    )

    metrics.test.run.set(test_run)
    metrics.test.name.set(test_name)

    space_ship = Discovery(
        warp_drive=WarpDrive(),
        spore_drive=SporeDrive(branch=spore_drive,
                               active=spore_drive is not None),
    )
    pings.space_ship_ready.submit()

    try:
        for mission in missions:
            complete_mission(space_ship=space_ship, mission=mission)

            # When mission "MISSION H: DISABLE GLEAN UPLOAD" disables the Glean
            # SDK ping upload all pending events, metrics and pings are
            # cleared, except for first_run_date. We need to restore values for
            # test.run and test.name after re-enabling ping upload, so that we
            # can properly correlate new pings with the test scenario.
            if mission.identifier == "MISSION I: ENABLE GLEAN UPLOAD":
                metrics.test.run.set(test_run)
                metrics.test.name.set(test_name)

        secs = 5
        logger.info("All missions completed.")
        logger.info(f" Waiting {secs}s for telemetry to be sent.")
        time.sleep(secs)

    except BurnhamError as err:
        click.echo(f"Error: {err}", err=True)
        sys.exit(1)
Exemplo n.º 7
0
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,
    )
Exemplo n.º 8
0
def fixture_initialize_glean(tmp_path_factory):
    """Initialize the Glean SDK for the test session."""
    Glean.initialize(
        application_id="burnham_testing",
        application_version="0.1.0",
        upload_enabled=False,
        data_dir=tmp_path_factory.mktemp("glean"),
    )
Exemplo n.º 9
0
def initialize_telemetry(upload_enabled):
    mozregression_path = Path.home() / ".mozilla" / "mozregression"
    Glean.initialize(
        application_id="org.mozilla.mozregression",
        application_version=__version__,
        upload_enabled=upload_enabled,
        configuration=Configuration(allow_multiprocessing=False),
        data_dir=mozregression_path / "data",
    )
Exemplo n.º 10
0
def test_the_app_channel_must_be_correctly_set():
    Glean.reset()
    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        configuration=Configuration(channel="my-test-channel"),
    )
    assert ("my-test-channel" == _builtins.metrics.glean.internal.metrics.
            app_channel.test_get_value())
Exemplo n.º 11
0
def test_set_application_id_and_version():
    Glean.reset()

    Glean.initialize(application_id="my-id", application_version="my-version")

    assert ("my-id" == _builtins.metrics.glean.internal.metrics.app_build.
            test_get_value())
    assert ("my-version" == _builtins.metrics.glean.internal.metrics.
            app_display_version.test_get_value())
Exemplo n.º 12
0
def test_data_dir_is_required():
    Glean._reset()

    with pytest.raises(TypeError):
        Glean.initialize(
            application_id=GLEAN_APP_ID,
            application_version=glean_version,
            upload_enabled=True,
            configuration=Glean._configuration,
        )
Exemplo n.º 13
0
def test_recording_upload_errors_doesnt_clobber_database(
        tmpdir, safe_httpserver, monkeypatch):
    """
    Test that running the ping uploader subprocess doesn't clobber the
    database. If, under some bug, the subprocess had "upload_enabled" set to
    True, it could record upload errors in the database, clobbering any metrics
    that might have meanwhile been recorded in the main process.

    This test is known to fail if "upload_enabled" is set to `True` in the
    subprocess.
    """
    tmpdir = Path(tmpdir)

    Glean._reset()
    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        upload_enabled=True,
        data_dir=tmpdir,
    )

    counter_metric = CounterMetricType(
        disabled=False,
        category="telemetry",
        lifetime=Lifetime.PING,
        name="counter_metric",
        send_in_pings=["baseline"],
    )
    counter_metric.add(10)

    safe_httpserver.serve_content(b"", code=400)

    # Force the ping upload worker into a separate process
    monkeypatch.setattr(PingUploadWorker, "process", PingUploadWorker._process)
    Glean._configuration._server_endpoint = safe_httpserver.url
    Glean._submit_ping_by_name("baseline")
    ProcessDispatcher._wait_for_last_process()

    assert 1 == len(safe_httpserver.requests)

    # Force a reload of the database from disk
    Glean._reset()
    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        upload_enabled=True,
        data_dir=tmpdir,
    )

    metric = get_upload_failure_metric()
    assert not metric["status_code_4xx"].test_has_value()
Exemplo n.º 14
0
    def __init__(self):
        """Initiate Glean, load pings and metrics."""

        logger.debug("Initializing Glean...")
        Glean.initialize(
            application_id="MozPhab",
            application_version=MOZPHAB_VERSION,
            upload_enabled=config.telemetry_enabled,
            configuration=Configuration(),
            data_dir=Path(environment.MOZBUILD_PATH) / "telemetry-data",
        )

        self.pings = load_pings(environment.MOZPHAB_MAIN_DIR / "pings.yaml")
        self.metrics = load_metrics(environment.MOZPHAB_MAIN_DIR / "metrics.yaml")
Exemplo n.º 15
0
def test_experiments_recording_before_glean_inits():
    # This test relies on Glean not being initialized and task
    # queuing to be on.
    Glean.reset()

    Glean.set_experiment_active("experiment_set_preinit", "branch_a")
    Glean.set_experiment_active("experiment_preinit_disabled", "branch_a")

    Glean.set_experiment_inactive("experiment_preinit_disabled")

    # This will init Glean and flush the dispatcher's queue.
    Glean.initialize(application_id=GLEAN_APP_ID,
                     application_version=glean_version)

    assert Glean.test_is_experiment_active("experiment_set_preinit")
    assert not Glean.test_is_experiment_active("experiment_preinit_disabled")
Exemplo n.º 16
0
def reset_glean(
    *,
    application_id: str,
    application_version: str,
    configuration: Optional[Configuration] = None,
    clear_stores: bool = True
) -> None:
    """
    Resets the Glean singleton.

    Args:
        application_id (str): The application id to use when sending pings.
        application_version (str): The version of the application sending
            Glean data.
        configuration (glean.config.Configuration): (optional) An object with
            global settings.
    """
    from glean import Glean
    from glean._dispatcher import Dispatcher

    data_dir: Optional[Path] = None
    if not clear_stores:
        Glean._destroy_data_dir = False
        data_dir = Glean._data_dir

    Glean._reset()

    # `_testing_mode` should be changed *after* `Glean._reset()` is run, so
    # that `Glean` properly joins on the worker thread when `_testing_mode` is
    # False.
    Dispatcher._testing_mode = True

    if data_dir is None:
        Glean._initialize_with_tempdir_for_testing(
            application_id=application_id,
            application_version=application_version,
            upload_enabled=True,
            configuration=configuration,
        )
    else:
        Glean.initialize(
            application_id=application_id,
            application_version=application_version,
            upload_enabled=True,
            data_dir=data_dir,
            configuration=configuration,
        )
Exemplo n.º 17
0
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")
Exemplo n.º 18
0
def test_clear_application_lifetime_metrics(tmpdir):
    Glean._reset()

    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        upload_enabled=True,
        data_dir=Path(str(tmpdir)),
    )

    counter_metric = CounterMetricType(
        disabled=False,
        category="test.telemetry",
        lifetime=Lifetime.APPLICATION,
        name="lifetime_reset",
        send_in_pings=["store1"],
    )

    # Additionally get metrics using the loader.
    metrics = load_metrics(ROOT / "data" / "core.yaml",
                           config={"allow_reserved": True})

    counter_metric.add(10)
    metrics.core_ping.seq.add(10)

    assert counter_metric.test_has_value()
    assert counter_metric.test_get_value() == 10

    assert metrics.core_ping.seq.test_has_value()
    assert metrics.core_ping.seq.test_get_value() == 10

    Glean._reset()

    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        upload_enabled=True,
        data_dir=Path(str(tmpdir)),
    )

    assert not counter_metric.test_has_value()
    assert not metrics.core_ping.seq.test_has_value()
Exemplo n.º 19
0
def test_initialize_must_not_crash_if_data_dir_is_messed_up(tmpdir):
    filename = tmpdir / "dummy_file"

    # Create a file in a temporary directory
    with open(filename, "w") as fd:
        fd.write("Contents\n")

    Glean.reset()
    assert False is Glean.is_initialized()

    # Pass in the filename as the data_dir
    Glean.initialize(
        application_id=GLEAN_APP_ID,
        application_version=glean_version,
        data_dir=filename,
    )

    # This should cause initialization to fail
    assert False is Glean.is_initialized()

    shutil.rmtree(tmpdir)
Exemplo n.º 20
0
def create_telemetry_from_environment(settings):
    """Creates and a Telemetry instance based on system details.

    If telemetry isn't enabled, the current interpreter isn't Python 3, or Glean
    can't be imported, then a "mock" telemetry instance is returned that doesn't
    set or record any data. This allows consumers to optimistically set telemetry
    data without needing to specifically handle the case where the current system
    doesn't support it.
    """

    is_mach_virtualenv = mozpack.path.normpath(sys.executable) == mozpack.path.normpath(
        get_mach_virtualenv_binary()
    )

    if not (
        is_applicable_telemetry_environment()
        # Glean is not compatible with Python 2
        and sys.version_info >= (3, 0)
        # If not using the mach virtualenv (e.g.: bootstrap uses native python)
        # then we can't guarantee that the glean package that we import is a
        # compatible version. Therefore, don't use glean.
        and is_mach_virtualenv
    ):
        return NoopTelemetry(False)

    try:
        from glean import Glean
    except ImportError:
        return NoopTelemetry(True)

    from pathlib import Path

    Glean.initialize(
        "mozilla.mach",
        "Unknown",
        is_telemetry_enabled(settings),
        data_dir=Path(get_state_dir()) / "glean",
    )
    return GleanTelemetry()