def perform_job(self, job): # This gets called in the forked process to trigger # execution of the actual job. We first need to force # register the application we are going to report data # against. Allow up to 5.0 seconds for this to complete. # Normally should take less than 2.0 seconds. If # registration hasn't occurred after 5.0 seconds, the # job will be run anyway. register_application(timeout=10.0) # Execute the actual task. result = _perform_job(self, job) # Now shutdown the agent, which will force a data # harvest. We do this explicitly because RQ calls # os._exit() to force exit the process and that bypasses # atexit callbacks, which is how we normally harvest on # shutdown. Again wait for up to 5.0 seconds to allow # data to be posted to the data collector. shutdown_agent(timeout=10.0) return result
def perform_job(self, *args, **kwargs): self.close_database() agent.register_application(timeout=10.0) try: return super().perform_job(*args, **kwargs) finally: agent.shutdown_agent(timeout=10.0) self.close_database()
def server_config(args): import os import sys import logging import time if len(args) == 0: usage('server-config') sys.exit(1) from newrelic.agent import initialize, register_application if len(args) >= 2: log_file = args[1] else: log_file = '/tmp/python-agent-test.log' log_level = logging.DEBUG try: os.unlink(log_file) except Exception: pass config_file = args[0] environment = os.environ.get('NEW_RELIC_ENVIRONMENT') if config_file == '-': config_file = os.environ.get('NEW_RELIC_CONFIG_FILE') initialize(config_file, environment, ignore_errors=False, log_file=log_file, log_level=log_level) _timeout = 30.0 _start = time.time() _application = register_application(timeout=_timeout) _end = time.time() _duration = _end - _start _logger = logging.getLogger(__name__) if not _application.active: _logger.error( 'Unable to register application for test, ' 'connection could not be established within %s seconds.', _timeout) return _logger.debug('Registration took %s seconds.', _duration) for key, value in sorted(_application.settings): print('%s = %r' % (key, value))
def server_config(args): import os import sys import logging import time if len(args) == 0: usage('server-config') sys.exit(1) from newrelic.agent import initialize, register_application if len(args) >= 2: log_file = args[1] else: log_file = '/tmp/python-agent-test.log' log_level = logging.DEBUG try: os.unlink(log_file) except Exception: pass config_file = args[0] environment = os.environ.get('NEW_RELIC_ENVIRONMENT') if config_file == '-': config_file = os.environ.get('NEW_RELIC_CONFIG_FILE') initialize(config_file, environment, ignore_errors=False, log_file=log_file, log_level=log_level) _timeout = 30.0 _start = time.time() _application = register_application(timeout=_timeout) _end = time.time() _duration = _end - _start _logger = logging.getLogger(__name__) if not _application.active: _logger.error('Unable to register application for test, ' 'connection could not be established within %s seconds.', _timeout) return _logger.debug('Registration took %s seconds.', _duration) for key, value in sorted(_application.settings): print('%s = %r' % (key, value))
def _nr_wrapper_BaseCommand_run_from_argv_(wrapped, instance, args, kwargs): def _args(argv, *args, **kwargs): return argv _argv = _args(*args, **kwargs) subcommand = _argv[1] commands = django_settings.instrumentation.scripts.django_admin startup_timeout = \ django_settings.instrumentation.background_task.startup_timeout if subcommand not in commands: return wrapped(*args, **kwargs) application = register_application(timeout=startup_timeout) with BackgroundTask(application, subcommand, 'Django'): return wrapped(*args, **kwargs)
import sys import time from concurrent.futures import ThreadPoolExecutor from django.core.management.base import BaseCommand from django.db import connection from django.utils import timezone from newrelic import agent from hc.api.models import Check executor = ThreadPoolExecutor(max_workers=10) logger = logging.getLogger(__name__) agent.initialize() agent.register_application() def _stdout(message): sys.stdout.write(message) sys.stdout.write('\n') sys.stdout.flush() @agent.background_task() def handle_many(): """ Send alerts for many checks simultaneously. """ query = Check.objects.filter(user__isnull=False) now = timezone.now() going_down = query.filter(alert_after__lt=now, status="up")
def validate_config(args): import os import sys import logging import time if len(args) == 0: usage('validate-config') sys.exit(1) from newrelic.agent import (global_settings, initialize, register_application) if len(args) >= 2: log_file = args[1] else: log_file = '/tmp/python-agent-test.log' log_level = logging.DEBUG try: os.unlink(log_file) except Exception: pass config_file = args[0] environment = os.environ.get('NEW_RELIC_ENVIRONMENT') if config_file == '-': config_file = os.environ.get('NEW_RELIC_CONFIG_FILE') initialize(config_file, environment, ignore_errors=False, log_file=log_file, log_level=log_level) _logger = logging.getLogger(__name__) _logger.debug('Starting agent validation.') _settings = global_settings() app_name = os.environ.get('NEW_RELIC_TEST_APP_NAME', 'Python Agent Test') _settings.app_name = app_name _settings.transaction_tracer.transaction_threshold = 0 _settings.shutdown_timeout = 30.0 _settings.debug.log_malformed_json_data = True _settings.debug.log_data_collector_payloads = True _settings.debug.log_transaction_trace_payload = True print(_user_message % dict(app_name=app_name, log_file=log_file)) _logger.debug('Register test application.') _logger.debug('Collector host is %r.', _settings.host) _logger.debug('Collector port is %r.', _settings.port) _logger.debug('Proxy scheme is %r.', _settings.proxy_scheme) _logger.debug('Proxy host is %r.', _settings.proxy_host) _logger.debug('Proxy port is %r.', _settings.proxy_port) _logger.debug('Proxy user is %r.', _settings.proxy_user) _logger.debug('SSL enabled is %r.', _settings.ssl) _logger.debug('License key is %r.', _settings.license_key) _timeout = 30.0 _start = time.time() _application = register_application(timeout=_timeout) _end = time.time() _duration = _end - _start if not _application.active: _logger.error( 'Unable to register application for test, ' 'connection could not be established within %s seconds.', _timeout) return if hasattr(_application.settings, 'messages'): for message in _application.settings.messages: if message['message'].startswith('Reporting to:'): parts = message['message'].split('Reporting to:') url = parts[1].strip() print('Registration successful. Reporting to:') print() print(' %s' % url) print() break _logger.debug('Registration took %s seconds.', _duration) _logger.debug('Run the validation test.') _run_validation_test()
def validate_config(args): import os import sys import logging import time if len(args) == 0: usage('validate-config') sys.exit(1) from newrelic.agent import (global_settings, initialize, register_application) if len(args) >= 2: log_file = args[1] else: log_file = '/tmp/python-agent-test.log' log_level = logging.DEBUG try: os.unlink(log_file) except Exception: pass config_file = args[0] environment = os.environ.get('NEW_RELIC_ENVIRONMENT') if config_file == '-': config_file = os.environ.get('NEW_RELIC_CONFIG_FILE') initialize(config_file, environment, ignore_errors=False, log_file=log_file, log_level=log_level) _logger = logging.getLogger(__name__) _logger.debug('Starting agent validation.') _settings = global_settings() app_name = os.environ.get('NEW_RELIC_TEST_APP_NAME', 'Python Agent Test') _settings.app_name = app_name _settings.transaction_tracer.transaction_threshold = 0 _settings.shutdown_timeout = 30.0 _settings.debug.log_malformed_json_data = True _settings.debug.log_data_collector_payloads = True _settings.debug.log_transaction_trace_payload = True print(_user_message % dict(app_name=app_name, log_file=log_file)) _logger.debug('Register test application.') _logger.debug('Collector host is %r.', _settings.host) _logger.debug('Collector port is %r.', _settings.port) _logger.debug('Proxy scheme is %r.', _settings.proxy_scheme) _logger.debug('Proxy host is %r.', _settings.proxy_host) _logger.debug('Proxy port is %r.', _settings.proxy_port) _logger.debug('Proxy user is %r.', _settings.proxy_user) _logger.debug('SSL enabled is %r.', _settings.ssl) _logger.debug('License key is %r.', _settings.license_key) _timeout = 30.0 _start = time.time() _application = register_application(timeout=_timeout) _end = time.time() _duration = _end - _start if not _application.active: _logger.error('Unable to register application for test, ' 'connection could not be established within %s seconds.', _timeout) return if hasattr(_application.settings, 'messages'): for message in _application.settings.messages: if message['message'].startswith('Reporting to:'): parts = message['message'].split('Reporting to:') url = parts[1].strip() print('Registration successful. Reporting to:') print() print(' %s' % url) print() break _logger.debug('Registration took %s seconds.', _duration) _logger.debug('Run the validation test.') _run_validation_test()