示例#1
0
    def post_dispatch_handler(context, handler, instance, result, start_time,
                              end_time, args):
        """Perform global operations after command dispatch.


        For now,  we will use this to handle build system telemetry.
        """
        # Don't do anything when...
        if should_skip_dispatch(context, handler):
            return

        # We have not opted-in to telemetry
        if not context.settings.build.telemetry:
            return

        from mozbuild.telemetry import gather_telemetry
        from mozbuild.base import MozbuildObject

        if not isinstance(instance, MozbuildObject):
            instance = MozbuildObject.from_environment()

        try:
            substs = instance.substs
        except Exception:
            substs = {}

        # We gather telemetry for every operation...
        gather_telemetry(command=handler.name,
                         success=(result == 0),
                         start_time=start_time,
                         end_time=end_time,
                         mach_context=context,
                         substs=substs,
                         paths=[instance.topsrcdir, instance.topobjdir])

        # But only submit about every n-th operation
        if random.randint(1, TELEMETRY_SUBMISSION_FREQUENCY) != 1:
            return

        with open(os.devnull, 'wb') as devnull:
            subprocess.Popen([
                sys.executable,
                os.path.join(topsrcdir, 'build', 'submit_telemetry_data.py'),
                get_state_dir()[0]
            ],
                             stdout=devnull,
                             stderr=devnull)
示例#2
0
    def post_dispatch_handler(context, handler, instance, result,
                              start_time, end_time, args):
        """Perform global operations after command dispatch.


        For now,  we will use this to handle build system telemetry.
        """
        # Don't do anything when...
        if should_skip_dispatch(context, handler):
            return

        # We have not opted-in to telemetry
        if not context.settings.build.telemetry:
            return

        from mozbuild.telemetry import gather_telemetry
        from mozbuild.base import MozbuildObject

        if not isinstance(instance, MozbuildObject):
            instance = MozbuildObject.from_environment()

        try:
            substs = instance.substs
        except Exception:
            substs = {}

        # We gather telemetry for every operation...
        gather_telemetry(command=handler.name, success=(result == 0),
                         start_time=start_time, end_time=end_time,
                         mach_context=context, substs=substs,
                         paths=[instance.topsrcdir, instance.topobjdir])

        # But only submit about every n-th operation
        if random.randint(1, TELEMETRY_SUBMISSION_FREQUENCY) != 1:
            return

        with open(os.devnull, 'wb') as devnull:
            subprocess.Popen([sys.executable,
                              os.path.join(topsrcdir, 'build',
                                           'submit_telemetry_data.py'),
                              get_state_dir()[0]],
                             stdout=devnull, stderr=devnull)
示例#3
0
    def post_dispatch_handler(context, handler, instance, result,
                              start_time, end_time, depth, args):
        """Perform global operations after command dispatch.


        For now,  we will use this to handle build system telemetry.
        """
        # Don't write telemetry data if this mach command was invoked as part of another
        # mach command.
        if depth != 1 or os.environ.get('MACH_MAIN_PID') != str(os.getpid()):
            return

        # Don't write telemetry data for 'mach' when 'DISABLE_TELEMETRY' is set.
        if os.environ.get('DISABLE_TELEMETRY') == '1':
            return

        # We have not opted-in to telemetry
        if not context.settings.build.telemetry:
            return

        from mozbuild.telemetry import gather_telemetry
        from mozbuild.base import MozbuildObject
        import mozpack.path as mozpath

        if not isinstance(instance, MozbuildObject):
            instance = MozbuildObject.from_environment()

        try:
            substs = instance.substs
        except Exception:
            substs = {}

        command_attrs = getattr(context, 'command_attrs', {})

        # We gather telemetry for every operation.
        paths = {
            instance.topsrcdir: '$topsrcdir/',
            instance.topobjdir: '$topobjdir/',
            mozpath.normpath(os.path.expanduser('~')): '$HOME/',
        }
        # This might override one of the existing entries, that's OK.
        # We don't use a sigil here because we treat all arguments as potentially relative
        # paths, so we'd like to get them back as they were specified.
        paths[mozpath.normpath(os.getcwd())] = ''
        data = gather_telemetry(command=handler.name, success=(result == 0),
                                start_time=start_time, end_time=end_time,
                                mach_context=context, substs=substs,
                                command_attrs=command_attrs, paths=paths)
        if data:
            telemetry_dir = os.path.join(get_state_dir(), 'telemetry')
            try:
                os.mkdir(telemetry_dir)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise
            outgoing_dir = os.path.join(telemetry_dir, 'outgoing')
            try:
                os.mkdir(outgoing_dir)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise

            with open(os.path.join(outgoing_dir, str(uuid.uuid4()) + '.json'),
                      'w') as f:
                json.dump(data, f, sort_keys=True)

        if should_skip_telemetry_submission(handler):
            return True

        state_dir = get_state_dir()

        machpath = os.path.join(instance.topsrcdir, 'mach')
        with open(os.devnull, 'wb') as devnull:
            subprocess.Popen([sys.executable, machpath, 'python',
                              '--no-virtualenv',
                              os.path.join(topsrcdir, 'build',
                                           'submit_telemetry_data.py'),
                              state_dir],
                             stdout=devnull, stderr=devnull)
示例#4
0
    def post_dispatch_handler(context, handler, instance, result,
                              start_time, end_time, depth, args):
        """Perform global operations after command dispatch.


        For now,  we will use this to handle build system telemetry.
        """
        # Don't write telemetry data if this mach command was invoked as part of another
        # mach command.
        if depth != 1 or os.environ.get('MACH_MAIN_PID') != str(os.getpid()):
            return

        # Don't write telemetry data for 'mach' when 'DISABLE_TELEMETRY' is set.
        if os.environ.get('DISABLE_TELEMETRY') == '1':
            return

        # We have not opted-in to telemetry
        if not context.settings.build.telemetry:
            return

        from mozbuild.telemetry import gather_telemetry
        from mozbuild.base import MozbuildObject
        import mozpack.path as mozpath

        if not isinstance(instance, MozbuildObject):
            instance = MozbuildObject.from_environment()

        try:
            substs = instance.substs
        except Exception:
            substs = {}

        # We gather telemetry for every operation.
        paths = {
            instance.topsrcdir: '$topsrcdir/',
            instance.topobjdir: '$topobjdir/',
            mozpath.normpath(os.path.expanduser('~')): '$HOME/',
        }
        # This might override one of the existing entries, that's OK.
        # We don't use a sigil here because we treat all arguments as potentially relative
        # paths, so we'd like to get them back as they were specified.
        paths[mozpath.normpath(os.getcwd())] = ''
        data = gather_telemetry(command=handler.name, success=(result == 0),
                                start_time=start_time, end_time=end_time,
                                mach_context=context, substs=substs,
                                paths=paths)
        if data:
            telemetry_dir = os.path.join(get_state_dir(), 'telemetry')
            try:
                os.mkdir(telemetry_dir)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise
            outgoing_dir = os.path.join(telemetry_dir, 'outgoing')
            try:
                os.mkdir(outgoing_dir)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise

            with open(os.path.join(outgoing_dir, str(uuid.uuid4()) + '.json'),
                      'w') as f:
                json.dump(data, f, sort_keys=True)

        if should_skip_telemetry_submission(handler):
            return True

        state_dir = get_state_dir()

        machpath = os.path.join(instance.topsrcdir, 'mach')
        with open(os.devnull, 'wb') as devnull:
            subprocess.Popen([sys.executable, machpath, 'python',
                              '--no-virtualenv',
                              os.path.join(topsrcdir, 'build',
                                           'submit_telemetry_data.py'),
                              state_dir],
                             stdout=devnull, stderr=devnull)
示例#5
0
def _finalize_telemetry_legacy(context, instance, handler, success, start_time,
                               end_time, topsrcdir):
    """Record and submit legacy telemetry.

    Parameterized by the raw gathered telemetry, this function handles persisting and
    submission of the data.

    This has been designated as "legacy" telemetry because modern telemetry is being
    submitted with "Glean".
    """
    from mozboot.util import get_state_dir
    from mozbuild.base import MozbuildObject
    from mozbuild.telemetry import gather_telemetry
    from mach.telemetry import (is_telemetry_enabled,
                                is_applicable_telemetry_environment)

    if not (is_applicable_telemetry_environment()
            and is_telemetry_enabled(context.settings)):
        return

    if not isinstance(instance, MozbuildObject):
        instance = MozbuildObject.from_environment()

    command_attrs = getattr(context, 'command_attrs', {})

    # We gather telemetry for every operation.
    data = gather_telemetry(command=handler.name,
                            success=success,
                            start_time=start_time,
                            end_time=end_time,
                            mach_context=context,
                            instance=instance,
                            command_attrs=command_attrs)
    if data:
        telemetry_dir = os.path.join(get_state_dir(), 'telemetry')
        try:
            os.mkdir(telemetry_dir)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
        outgoing_dir = os.path.join(telemetry_dir, 'outgoing')
        try:
            os.mkdir(outgoing_dir)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        with open(os.path.join(outgoing_dir,
                               str(uuid.uuid4()) + '.json'), 'w') as f:
            json.dump(data, f, sort_keys=True)

    # The user is performing a maintenance command, skip the upload
    if handler.name in (
            'bootstrap',
            'doctor',
            'mach-commands',
            'vcs-setup',
            'create-mach-environment',
            # We call mach environment in client.mk which would cause the
            # data submission to block the forward progress of make.
            'environment'):
        return False

    if 'TEST_MACH_TELEMETRY_NO_SUBMIT' in os.environ:
        # In our telemetry tests, we want telemetry to be collected for analysis, but
        # we don't want it submitted.
        return False

    state_dir = get_state_dir()

    machpath = os.path.join(instance.topsrcdir, 'mach')
    with open(os.devnull, 'wb') as devnull:
        subprocess.Popen([
            sys.executable, machpath, 'python', '--no-virtualenv',
            os.path.join(topsrcdir, 'build', 'submit_telemetry_data.py'),
            state_dir
        ],
                         stdout=devnull,
                         stderr=devnull)