diff --git a/api/api.py b/api/api.py index a37936269f3558016b2e82c2689f901638307ff4..34b2b380fbfd5c26a520c339724d37e0c7f58c87 100644 --- a/api/api.py +++ b/api/api.py @@ -129,10 +129,6 @@ def dispatcher(router, request, response): response.headers['Content-Type'] = 'application/json; charset=utf-8' -try: - import newrelic.agent - app = newrelic.agent.WSGIApplicationWrapper(webapp2.WSGIApplication(routes)) -except ImportError: - app = webapp2.WSGIApplication(routes) +app = webapp2.WSGIApplication(routes) app.router.set_dispatcher(dispatcher) diff --git a/bin/api.wsgi b/bin/api.wsgi index 1e3d3a43f588f5485f5181e07f1b08053a808cd8..a572504884401d99688b145f279adf4c9a2c472d 100644 --- a/bin/api.wsgi +++ b/bin/api.wsgi @@ -1,6 +1,7 @@ # vim: filetype=python import os +import sys import toml import logging import argparse @@ -19,6 +20,7 @@ ap.add_argument('--insecure', help='allow user info as urlencoded param', action ap.add_argument('--central_uri', help='scitran central api', default='https://sdmc.scitran.io/api') ap.add_argument('--log_level', help='log level [info]', default='info') ap.add_argument('--drone_secret', help='shared drone secret') +ap.add_argument('--new_relic', help='path to New Relic .ini file') ap.add_argument('--config', help='path to config file') @@ -73,15 +75,6 @@ from api import api from api import centralclient, jobs from api import jobs -try: - import newrelic.agent - newrelic.agent.initialize('../../newrelic.ini') - log.info('New Relic detected and loaded. Monitoring enabled.') -except ImportError: - log.info('New Relic not detected. Monitoring disabled.') -except newrelic.api.exceptions.ConfigurationError: - log.warn('New Relic detected but configuration was not valid. Please ensure newrelic.ini is present. Monitoring disabled.') - log.setLevel(getattr(logging, args.log_level.upper())) api.app.config = vars(args) @@ -102,6 +95,20 @@ if not os.path.exists(api.app.config['data_path']): api.app.db = mongo.db +if args.new_relic is not None: + try: + import newrelic.agent, newrelic.api.exceptions + newrelic.agent.initialize(args.new_relic) + api.app = newrelic.agent.WSGIApplicationWrapper(api.app) + log.info('New Relic detected and loaded. Monitoring enabled.') + except ImportError: + log.critical('New Relic libraries not found.') + sys.exit(1) + except newrelic.api.exceptions.ConfigurationError: + log.critical('New Relic detected, but configuration invalid.') + sys.exit(1) + + if __name__ == '__main__': api.app.debug = True # send stack trace for uncaught exceptions to client paste.httpserver.serve(api.app, host=args.host, port=args.port, ssl_pem=args.ssl_cert)