def conn_with_scout(): with app_with_scout() as app: app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" db = SQLAlchemy(app) # Setup according to http://help.apm.scoutapp.com/#flask-sqlalchemy instrument_sqlalchemy(db) conn = db.engine.connect() try: yield conn finally: conn.close()
def conn_with_scout(): with app_with_scout() as app: app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False db = SQLAlchemy(app) # Setup according to https://docs.scoutapm.com/#flask-sqlalchemy instrument_sqlalchemy(db) conn = db.engine.connect() try: yield conn finally: conn.close()
def configure_scout(app): try: from scout_apm.flask import ScoutApm from scout_apm.flask.sqlalchemy import instrument_sqlalchemy except ImportError: return False app.config.setdefault("SCOUT_NAME", "Zeus") app.config.setdefault("SCOUT_MONITOR", True) ScoutApm(app) instrument_sqlalchemy(db)
def app_with_scout(): with flask_app_with_scout() as app: app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False db = SQLAlchemy(app) # Setup according to https://docs.scoutapm.com/#flask-sqlalchemy instrument_sqlalchemy(db) conn = db.engine.connect() @app.route("/sqlalchemy/") def sqlalchemy(): result = conn.execute("SELECT 'Hello from the DB!'") return list(result)[0][0] try: yield app finally: conn.close()
def load_scout_apm(app, db, celery=False): logger = _get_logger(app, celery) try: if celery: import scout_apm.celery from scout_apm.api import Config Config.set(key=app.config['SCOUT_KEY'], monitor=app.config['SCOUT_MONITOR'], name=app.config['SCOUT_NAME']) scout_apm.celery.install() else: from scout_apm.flask import ScoutApm from scout_apm.flask.sqlalchemy import instrument_sqlalchemy ScoutApm(app) instrument_sqlalchemy(db) except ImportError: logger.warning('Scout APM modules not found') else: logger.info('Scout APM initialized')
db = SQLAlchemy(app) class Role(Enum): """These are used with the 'login_required_with_role' function.""" Normal = 'N' # Normal users, without admin and mod powers, but with a profile. Mod = 'M' # Mod users, with mod powers but without a profile/account page. Admin = 'A' # Admin user(s), with admin powers but without a profile/account page. if os.environ.get("HEROKU"): # We're running on Heroku, so we'll add this for statistics. from scout_apm.flask.sqlalchemy import instrument_sqlalchemy instrument_sqlalchemy(db) def login_required_with_role(role): """Used instead of "login_required" when a specific role is needed.""" role = role.value # Because it's an enum, we'll get its value here. def wrapper(fn): @wraps(fn) def decorated_view(*args, **kwargs): if not current_user.is_authenticated: return login_manager.unauthorized() if current_user.role != role: return login_manager.unauthorized()