示例#1
0
    def __call__(self):
        if self._times is None:
            return

        new_times = os.times()
        elapsed_time = self._timer.restart_timer()
        elapsed_cpu_time = elapsed_time * logical_processor_count()

        user_time = new_times[0] - self._times[0]
        user_utilization = user_time / elapsed_cpu_time

        system_time = new_times[1] - self._times[1]
        system_utilization = system_time / elapsed_cpu_time

        total_time = sum(new_times[0:4]) - sum(self._times[0:4])
        total_utilization = total_time / elapsed_cpu_time

        self._times = new_times

        yield ('CPU/User Time', user_time)
        yield ('CPU/User/Utilization', user_utilization)
        yield ('CPU/System Time', system_time)
        yield ('CPU/System/Utilization', system_utilization)
        yield ('CPU/Total Time', total_time)
        yield ('CPU/Total/Utilization', total_utilization)
    def __call__(self):
        if self._times is None:
            return

        new_times = os.times()
        user_time = new_times[0] - self._times[0]

        elapsed_time = self._timer.restart_timer()
        utilization = user_time / (elapsed_time * logical_processor_count())

        self._times = new_times

        yield ('CPU/User Time', user_time)
        yield ('CPU/User/Utilization', utilization)
示例#3
0
def environment_settings():
    """Returns an array of arrays of environment settings

    """

    env = []

    # Agent information.

    env.append(('Agent Version', '.'.join(map(str, newrelic.version_info))))

    # System information.

    env.append(('Arch', platform.machine()))
    env.append(('OS', platform.system()))
    env.append(('OS version', platform.release()))

    env.append(('Total Physical Memory (MB)', total_physical_memory()))
    env.append(('Logical Processors', logical_processor_count()))

    physical_processor_packages, physical_cores = physical_processor_count()

    # Report this attribute only if it has a valid value.

    if physical_processor_packages:
        env.append(
            ('Physical Processor Packages', physical_processor_packages))

    # Report this attribute only if it has a valid value.

    if physical_cores:
        env.append(('Physical Cores', physical_cores))

    # Python information.

    env.append(('Python Program Name', sys.argv[0]))

    env.append(('Python Executable', sys.executable))

    env.append(('Python Home', os.environ.get('PYTHONHOME', '')))
    env.append(('Python Path', os.environ.get('PYTHONPATH', '')))

    env.append(('Python Prefix', sys.prefix))
    env.append(('Python Exec Prefix', sys.exec_prefix))

    env.append(('Python Runtime', '.'.join(platform.python_version_tuple())))

    env.append(('Python Implementation', platform.python_implementation()))
    env.append(('Python Version', sys.version))

    env.append(('Python Platform', sys.platform))

    env.append(('Python Max Unicode', sys.maxunicode))

    # Extensions information.

    extensions = []

    if 'newrelic.core._thread_utilization' in sys.modules:
        extensions.append('newrelic.core._thread_utilization')

    env.append(('Compiled Extensions', ', '.join(extensions)))

    # Dispatcher information.

    dispatcher = []

    if not dispatcher and 'mod_wsgi' in sys.modules:
        mod_wsgi = sys.modules['mod_wsgi']
        if hasattr(mod_wsgi, 'process_group'):
            if mod_wsgi.process_group == '':
                dispatcher.append(('Dispatcher', 'Apache/mod_wsgi (embedded)'))
            else:
                dispatcher.append(('Dispatcher', 'Apache/mod_wsgi (daemon)'))
            env.append(
                ('Apache/mod_wsgi Process Group', mod_wsgi.process_group))
        else:
            dispatcher.append(('Dispatcher', 'Apache/mod_wsgi'))
        if hasattr(mod_wsgi, 'version'):
            dispatcher.append(('Dispatcher Version', str(mod_wsgi.version)))
        if hasattr(mod_wsgi, 'application_group'):
            env.append(('Apache/mod_wsgi Application Group',
                        mod_wsgi.application_group))

    if not dispatcher and 'uwsgi' in sys.modules:
        dispatcher.append(('Dispatcher', 'uWSGI'))
        uwsgi = sys.modules['uwsgi']
        if hasattr(uwsgi, 'version'):
            dispatcher.append(('Dispatcher Version', uwsgi.version))

    if not dispatcher and 'flup.server.fcgi' in sys.modules:
        dispatcher.append(('Dispatcher', 'flup/fastcgi (threaded)'))

    if not dispatcher and 'flup.server.fcgi_fork' in sys.modules:
        dispatcher.append(('Dispatcher', 'flup/fastcgi (prefork)'))

    if not dispatcher and 'flup.server.scgi' in sys.modules:
        dispatcher.append(('Dispatcher', 'flup/scgi (threaded)'))

    if not dispatcher and 'flup.server.scgi_fork' in sys.modules:
        dispatcher.append(('Dispatcher', 'flup/scgi (prefork)'))

    if not dispatcher and 'flup.server.ajp' in sys.modules:
        dispatcher.append(('Dispatcher', 'flup/ajp (threaded)'))

    if not dispatcher and 'flup.server.ajp_fork' in sys.modules:
        dispatcher.append(('Dispatcher', 'flup/ajp (forking)'))

    if not dispatcher and 'flup.server.cgi' in sys.modules:
        dispatcher.append(('Dispatcher', 'flup/cgi'))

    if not dispatcher and 'gunicorn' in sys.modules:
        if 'gunicorn.workers.ggevent' in sys.modules:
            dispatcher.append(('Dispatcher', 'gunicorn (gevent)'))
        elif 'gunicorn.workers.geventlet' in sys.modules:
            dispatcher.append(('Dispatcher', 'gunicorn (eventlet)'))
        else:
            dispatcher.append(('Dispatcher', 'gunicorn'))
        gunicorn = sys.modules['gunicorn']
        if hasattr(gunicorn, '__version__'):
            dispatcher.append(('Dispatcher Version', gunicorn.__version__))

    if not dispatcher and 'tornado' in sys.modules:
        dispatcher.append(('Dispatcher', 'tornado'))
        tornado = sys.modules['tornado']
        if hasattr(tornado, 'version_info'):
            dispatcher.append(
                ('Dispatcher Version', str(tornado.version_info)))

    env.extend(dispatcher)

    # Module information.

    plugins = []

    # Using six to create create a snapshot of sys.modules can occassionally
    # fail in a rare case when modules are imported in parallel by different
    # threads. This is because list(six.iteritems(sys.modules)) results in
    # list(iter(sys.modules.iteritems())), which means sys.modules could change
    # between the time when the iterable is handed over from the iter() to
    # list().
    #
    # TL;DR: Do NOT use six module for the following iteration.

    for name, module in list(sys.modules.items()):
        if name.startswith('newrelic.hooks.'):
            plugins.append(name)

        elif name.find('.') == -1 and hasattr(module, '__file__'):
            # XXX This is disabled as it can cause notable overhead in
            # pathalogical cases. Will be replaced with a new system
            # where have a whitelist of packages we really want version
            # information for and will work out on case by case basis
            # how to extract that from the modules themselves.

            # try:
            #     if 'pkg_resources' in sys.modules:
            #         version = pkg_resources.get_distribution(name).version
            #         if version:
            #             name = '%s (%s)' % (name, version)
            # except Exception:
            #     pass

            plugins.append(name)

    env.append(('Plugin List', plugins))

    return env
示例#4
0
    def _connect_payload(app_name, linked_applications, environment, settings):
        settings = global_settings_dump(settings)
        app_names = [app_name] + linked_applications

        hostname = system_info.gethostname(
            settings["heroku.use_dyno_names"],
            settings["heroku.dyno_name_prefixes_to_shorten"],
        )

        ip_address = system_info.getips()

        connect_settings = {}
        connect_settings["browser_monitoring.loader"] = settings[
            "browser_monitoring.loader"
        ]
        connect_settings["browser_monitoring.debug"] = settings[
            "browser_monitoring.debug"
        ]

        security_settings = {}
        security_settings["capture_params"] = settings["capture_params"]
        security_settings["transaction_tracer"] = {}
        security_settings["transaction_tracer"]["record_sql"] = settings[
            "transaction_tracer.record_sql"
        ]

        utilization_settings = {}
        # metadata_version corresponds to the utilization spec being used.
        utilization_settings["metadata_version"] = 5
        utilization_settings[
            "logical_processors"
        ] = system_info.logical_processor_count()
        utilization_settings["total_ram_mib"] = system_info.total_physical_memory()
        utilization_settings["hostname"] = hostname
        if ip_address:
            utilization_settings["ip_address"] = ip_address

        boot_id = system_info.BootIdUtilization.detect()
        if boot_id:
            utilization_settings["boot_id"] = boot_id

        utilization_conf = {}
        logical_processor_conf = settings["utilization.logical_processors"]
        total_ram_conf = settings["utilization.total_ram_mib"]
        hostname_conf = settings["utilization.billing_hostname"]
        if logical_processor_conf:
            utilization_conf["logical_processors"] = logical_processor_conf
        if total_ram_conf:
            utilization_conf["total_ram_mib"] = total_ram_conf
        if hostname_conf:
            utilization_conf["hostname"] = hostname_conf
        if utilization_conf:
            utilization_settings["config"] = utilization_conf

        vendors = []
        if settings["utilization.detect_aws"]:
            vendors.append(AWSUtilization)
        if settings["utilization.detect_pcf"]:
            vendors.append(PCFUtilization)
        if settings["utilization.detect_gcp"]:
            vendors.append(GCPUtilization)
        if settings["utilization.detect_azure"]:
            vendors.append(AzureUtilization)

        utilization_vendor_settings = {}
        for vendor in vendors:
            metadata = vendor.detect()
            if metadata:
                utilization_vendor_settings[vendor.VENDOR_NAME] = metadata
                break

        if settings["utilization.detect_docker"]:
            docker = DockerUtilization.detect()
            if docker:
                utilization_vendor_settings["docker"] = docker

        if settings["utilization.detect_kubernetes"]:
            kubernetes = KubernetesUtilization.detect()
            if kubernetes:
                utilization_vendor_settings["kubernetes"] = kubernetes

        if utilization_vendor_settings:
            utilization_settings["vendors"] = utilization_vendor_settings

        display_host = settings["process_host.display_name"]
        if display_host is None:
            display_host = hostname

        metadata = {}
        for env_var in os.environ:
            if env_var.startswith("NEW_RELIC_METADATA_"):
                metadata[env_var] = os.environ[env_var]

        return (
            {
                "host": hostname,
                "pid": os.getpid(),
                "language": "python",
                "app_name": app_names,
                "identifier": ",".join(app_names),
                "agent_version": version,
                "environment": environment,
                "metadata": metadata,
                "settings": connect_settings,
                "security_settings": security_settings,
                "utilization": utilization_settings,
                "high_security": settings["high_security"],
                "event_harvest_config": settings["event_harvest_config"],
                "labels": settings["labels"],
                "display_host": display_host,
            },
        )
示例#5
0
    def _create_connect_payload(app_name, linked_applications, environment,
            settings):
        # Creates the payload to send on the initial connection to the
        # data collector.

        app_names = [app_name] + linked_applications

        hostname = system_info.gethostname()

        connect_settings = {}
        connect_settings['browser_monitoring.loader'] = (
            settings['browser_monitoring.loader'])
        connect_settings['browser_monitoring.debug'] = (
            settings['browser_monitoring.debug'])

        security_settings = {}
        security_settings['capture_params'] = settings['capture_params']
        security_settings['transaction_tracer'] = {}
        security_settings['transaction_tracer']['record_sql'] = (
            settings['transaction_tracer.record_sql'])

        utilization_settings = {}
        # metadata_version corresponds to the utilization spec being used.
        utilization_settings['metadata_version'] = 2
        utilization_settings['logical_processors'] = logical_processor_count()
        utilization_settings['total_ram_mib'] = total_physical_memory()
        utilization_settings['hostname'] = hostname

        utilization_conf = {}
        logical_processor_conf = settings.get('utilization.logical_processors')
        total_ram_conf = settings.get('utilization.total_ram_mib')
        hostname_conf = settings.get('utilization.billing_hostname')
        if logical_processor_conf:
            utilization_conf['logical_processors'] = logical_processor_conf
        if total_ram_conf:
            utilization_conf['total_ram_mib'] = total_ram_conf
        if hostname_conf:
            utilization_conf['hostname'] = hostname_conf
        if utilization_conf:
            utilization_settings['config'] = utilization_conf

        utilization_vendor_settings = {}
        if settings['utilization.detect_aws']:
            aws = aws_data()
            if aws:
                utilization_vendor_settings['aws'] = aws
        if settings['utilization.detect_docker']:
            docker_id = docker_container_id()
            if docker_id:
                utilization_vendor_settings['docker'] = {'id': docker_id}
        if utilization_vendor_settings:
            utilization_settings['vendors'] = utilization_vendor_settings

        display_host = settings['process_host.display_name']
        if display_host is None:
            display_host = hostname

        local_config = {
                'host': hostname,
                'pid': os.getpid(),
                'language': 'python',
                'app_name': app_names,
                'identifier': ','.join(app_names),
                'agent_version': version,
                'environment': environment,
                'settings': connect_settings,
                'security_settings': security_settings,
                'utilization': utilization_settings,
                'high_security': settings['high_security'],
                'labels': settings['labels'],
                'display_host': display_host,
        }
        return (local_config,)
示例#6
0
def test_logical_processor_count():
    assert logical_processor_count() >= 1
def environment_settings():
    """Returns an array of arrays of environment settings"""

    env = []

    # Agent information.

    env.append(("Agent Version", ".".join(map(str, newrelic.version_info))))

    # System information.

    env.append(("Arch", platform.machine()))
    env.append(("OS", platform.system()))
    env.append(("OS version", platform.release()))

    env.append(("Total Physical Memory (MB)", total_physical_memory()))
    env.append(("Logical Processors", logical_processor_count()))

    physical_processor_packages, physical_cores = physical_processor_count()

    # Report this attribute only if it has a valid value.

    if physical_processor_packages:
        env.append(("Physical Processor Packages", physical_processor_packages))

    # Report this attribute only if it has a valid value.

    if physical_cores:
        env.append(("Physical Cores", physical_cores))

    # Python information.

    env.append(("Python Program Name", sys.argv[0]))

    env.append(("Python Executable", sys.executable))

    env.append(("Python Home", os.environ.get("PYTHONHOME", "")))
    env.append(("Python Path", os.environ.get("PYTHONPATH", "")))

    env.append(("Python Prefix", sys.prefix))
    env.append(("Python Exec Prefix", sys.exec_prefix))

    env.append(("Python Runtime", ".".join(platform.python_version_tuple())))

    env.append(("Python Implementation", platform.python_implementation()))
    env.append(("Python Version", sys.version))

    env.append(("Python Platform", sys.platform))

    env.append(("Python Max Unicode", sys.maxunicode))

    # Extensions information.

    extensions = []

    if "newrelic.core._thread_utilization" in sys.modules:
        extensions.append("newrelic.core._thread_utilization")

    env.append(("Compiled Extensions", ", ".join(extensions)))

    # Dispatcher information.

    dispatcher = []

    if not dispatcher and "mod_wsgi" in sys.modules:
        mod_wsgi = sys.modules["mod_wsgi"]
        if hasattr(mod_wsgi, "process_group"):
            if mod_wsgi.process_group == "":
                dispatcher.append(("Dispatcher", "Apache/mod_wsgi (embedded)"))
            else:
                dispatcher.append(("Dispatcher", "Apache/mod_wsgi (daemon)"))
            env.append(("Apache/mod_wsgi Process Group", mod_wsgi.process_group))
        else:
            dispatcher.append(("Dispatcher", "Apache/mod_wsgi"))
        if hasattr(mod_wsgi, "version"):
            dispatcher.append(("Dispatcher Version", str(mod_wsgi.version)))
        if hasattr(mod_wsgi, "application_group"):
            env.append(("Apache/mod_wsgi Application Group", mod_wsgi.application_group))

    if not dispatcher and "uwsgi" in sys.modules:
        dispatcher.append(("Dispatcher", "uWSGI"))
        uwsgi = sys.modules["uwsgi"]
        if hasattr(uwsgi, "version"):
            dispatcher.append(("Dispatcher Version", uwsgi.version))

    if not dispatcher and "flup.server.fcgi" in sys.modules:
        dispatcher.append(("Dispatcher", "flup/fastcgi (threaded)"))

    if not dispatcher and "flup.server.fcgi_fork" in sys.modules:
        dispatcher.append(("Dispatcher", "flup/fastcgi (prefork)"))

    if not dispatcher and "flup.server.scgi" in sys.modules:
        dispatcher.append(("Dispatcher", "flup/scgi (threaded)"))

    if not dispatcher and "flup.server.scgi_fork" in sys.modules:
        dispatcher.append(("Dispatcher", "flup/scgi (prefork)"))

    if not dispatcher and "flup.server.ajp" in sys.modules:
        dispatcher.append(("Dispatcher", "flup/ajp (threaded)"))

    if not dispatcher and "flup.server.ajp_fork" in sys.modules:
        dispatcher.append(("Dispatcher", "flup/ajp (forking)"))

    if not dispatcher and "flup.server.cgi" in sys.modules:
        dispatcher.append(("Dispatcher", "flup/cgi"))

    if not dispatcher and "gunicorn" in sys.modules:
        if "gunicorn.workers.ggevent" in sys.modules:
            dispatcher.append(("Dispatcher", "gunicorn (gevent)"))
        elif "gunicorn.workers.geventlet" in sys.modules:
            dispatcher.append(("Dispatcher", "gunicorn (eventlet)"))
        elif "uvicorn.workers" in sys.modules:
            dispatcher.append(("Dispatcher", "gunicorn (uvicorn)"))
            uvicorn = sys.modules.get("uvicorn")
            if hasattr(uvicorn, "__version__"):
                dispatcher.append(("Worker Version", uvicorn.__version__))
        else:
            dispatcher.append(("Dispatcher", "gunicorn"))

        gunicorn = sys.modules["gunicorn"]
        if hasattr(gunicorn, "__version__"):
            dispatcher.append(("Dispatcher Version", gunicorn.__version__))

    if not dispatcher and "uvicorn" in sys.modules:
        dispatcher.append(("Dispatcher", "uvicorn"))
        uvicorn = sys.modules["uvicorn"]

        if hasattr(uvicorn, "__version__"):
            dispatcher.append(("Dispatcher Version", uvicorn.__version__))

    if not dispatcher and "tornado" in sys.modules:
        dispatcher.append(("Dispatcher", "tornado"))
        tornado = sys.modules["tornado"]
        if hasattr(tornado, "version_info"):
            dispatcher.append(("Dispatcher Version", str(tornado.version_info)))

    env.extend(dispatcher)

    # Module information.

    plugins = []

    # Using any iterable to create a snapshot of sys.modules can occassionally
    # fail in a rare case when modules are imported in parallel by different
    # threads.
    #
    # TL;DR: Do NOT use an iterable on the original sys.modules to generate the
    # list

    for name, module in sys.modules.copy().items():
        # If the module isn't actually loaded (such as failed relative imports
        # in Python 2.7), the module will be None and should not be reported.
        if module is None:
            continue

        if name.startswith("newrelic.hooks."):
            plugins.append(name)

        elif name.find(".") == -1 and hasattr(module, "__file__"):
            # XXX This is disabled as it can cause notable overhead in
            # pathalogical cases. Will be replaced with a new system
            # where have a allowlist of packages we really want version
            # information for and will work out on case by case basis
            # how to extract that from the modules themselves.

            # try:
            #     if 'pkg_resources' in sys.modules:
            #         version = pkg_resources.get_distribution(name).version
            #         if version:
            #             name = '%s (%s)' % (name, version)
            # except Exception:
            #     pass

            plugins.append(name)

    env.append(("Plugin List", plugins))

    return env