def create_app(test_config=None): # create and configure the app app = Flask(__name__, instance_relative_config=True) CORS(app) # AWS X-Ray plugins = ('EC2Plugin', 'ECSPlugin') xray_recorder.configure(service='myproject-customer-service', plugins=plugins) XRayMiddleware(app, xray_recorder) patch_all() app.config.from_mapping(SECRET_KEY="dev", DATABASE="sample://db-string") if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile("config.py", silent=True) else: # load the test config if passed in app.config.update(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # Add a blueprint for the customers module app.register_blueprint(customer_module) return app
def create_app(script_info=None): # instantiate the app app = Flask(__name__) flask_bcrypt = Bcrypt(app) jwt = JWTManager(app) app.json_encoder = JSONEncoder # enable CORS CORS(app, resources={r'/*': {'origins': '*'}}) # TODO 9: Review X-ray setting patch_modules = ( 'boto3', 'botocore', 'pynamodb', 'requests', ) plugins = ('EC2Plugin', ) xray_recorder.configure(service='CloudAlbum', plugins=plugins, context_missing='LOG_ERROR', sampling=False) xray_recorder.begin_segment('cloudalbum') XRayMiddleware(app, xray_recorder) patch(patch_modules) # set config app_settings = os.getenv('APP_SETTINGS') app.config.from_object(app_settings) # set logger to STDOUT app.logger.addHandler(logging.StreamHandler(sys.stdout)) app.logger.setLevel(logging.DEBUG) # Create database table, if it is not exists with app.app_context(): create_table() # register blueprints from cloudalbum.api.users import users_blueprint app.register_blueprint(users_blueprint, url_prefix='/users') from cloudalbum.api.photos import photos_blueprint app.register_blueprint(photos_blueprint, url_prefix='/photos') from cloudalbum.api.admin import admin_blueprint app.register_blueprint(admin_blueprint, url_prefix='/admin') # shell context for flask cli @app.shell_context_processor def ctx(): return {'app': app} return app
def enable_xray(name, app): enable_xray = os.environ.get("ENABLE_XRAY", False) if enable_xray is not False: # Initialize xray patch_all() xray_recorder.configure( service=name, sampling=False, context=Context(), context_missing="LOG_ERROR" ) XRayMiddleware(app, xray_recorder) logging.getLogger("aws_xray_sdk").setLevel(logging.ERROR)
def configure_extensions(app): """ Configure extensions of the app. Add additional extensions in this functions if you need to add more flask extensions :param app: the app that will attach to the extensions :return: None """ cors.init_app(app) bcrypt.init_app(app) mail.init_app(app) db.init_app(app) migrate.init_app(app, db) login_manager.init_app(app) mautic.init_app(app) # flask admin setup ########################### f_admin = Admin(app, url="/api/admin", name='Admin', template_mode="bootstrap3", index_view=Dashboard( name='Home', url="/api/admin", )) flask_admin_setup(f_admin, app) ########################### cache.init_app(app, app.config) # limiter.init_app(app) if not app.debug: sentry.init_app(app) client = Client(app.config['SENTRY_DSN']) register_signal(client) # patch_all() xray_recorder.configure(service="beatest-api-v0.1", sampling=False, context_missing='LOG_ERROR', daemon_address='127.0.0.1:2000', dynamic_naming='*beatest.in*') XRayMiddleware(app, xray_recorder) setup_login_manager(login_manager) celery.init_app(app) configure_imports()
def test_lambda_serverless(): TRACE_ID = '1-5759e988-bd862e3fe1be46a994272793' PARENT_ID = '53995c3f42cd8ad8' HEADER_VAR = "Root=%s;Parent=%s;Sampled=1" % (TRACE_ID, PARENT_ID) os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] = HEADER_VAR lambda_context = lambda_launcher.LambdaContext() new_recorder = get_new_stubbed_recorder() new_recorder.configure(service='test', sampling=False, context=lambda_context) new_app = Flask(__name__) @new_app.route('/subsegment') def subsegment(): # Test in between request and make sure Serverless creates a subsegment instead of a segment. # Ensure that the parent segment is a facade segment. assert new_recorder.current_subsegment() assert type(new_recorder.current_segment()) == facade_segment.FacadeSegment return 'ok' @new_app.route('/trace_header') def trace_header(): # Ensure trace header is preserved. subsegment = new_recorder.current_subsegment() header = subsegment.get_origin_trace_header() assert header.data['k1'] == 'v1' return 'ok' middleware = XRayMiddleware(new_app, new_recorder) middleware.in_lambda_ctx = True app_client = new_app.test_client() path = '/subsegment' app_client.get(path) segment = recorder.emitter.pop() assert not segment # Segment should be none because it's created and ended by the middleware path2 = '/trace_header' app_client.get(path2, headers={http.XRAY_HEADER: 'k1=v1'})
def init_xray(app): logging.getLogger("aws_xray_sdk").setLevel('ERROR') if app.debug: # env var AWS_XRAY_SDK_ENABLED can overwrite this global_sdk_config.set_sdk_enabled(False) else: # TODO: configure x-ray service xray_recorder.configure(service="btbapi") # Setup X-Ray Flask Integration XRayMiddleware(app, xray_recorder) # Setup X-Ray psycopg2, boto3 (aws sdk) Integration patch(["psycopg2", "boto3"])
def configure(app, service_name): """ Configure XRay Middleware for the Flask Service. :type app: flask.Flask :param app: Flask app :type service_name: string :param service_name: Service name identifies the originator of instrumentation data in aws x-ray """ xray_recorder.configure(sampling=False, service=service_name, plugins=('EC2Plugin',)) XRayMiddleware(app, xray_recorder)
def create_app(config=None, testing=False): """Application factory, used to create application """ app = Flask('recommender') configure_app(app, testing) register_blueprints(app) plugins = ('EC2Plugin', 'ECSPlugin') xray_recorder.configure(service='recommenderservice',plugins=plugins) XRayMiddleware(app, xray_recorder) patch_all() return app
def lambda_handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]: try: global pcluster_api # pylint: disable=global-statement,invalid-name if not pcluster_api: logger.info("Initializing Flask Application") pcluster_api = _init_flask_app() # Instrument X-Ray recorder to trace requests served by the Flask application if event.get("version") == "2.0": xray_recorder.configure(service="ParallelCluster Flask App") XRayMiddleware(pcluster_api.flask_app, xray_recorder) # Setting default region to region where lambda function is executed os.environ["AWS_DEFAULT_REGION"] = os.environ["AWS_REGION"] return handle_request(pcluster_api.app, event, context) except Exception as e: logger.critical("Unexpected exception: %s", e, exc_info=True) raise Exception("Unexpected fatal exception. Please look at API logs for details on the encountered failure.")
def create_app(config=None, testing=False): """Application factory, used to create application """ app = Flask('cart') configure_app(app, testing) app.config['redis'] = redis.StrictRedis.from_url( app.config['REDIS_ENDPOINT']) register_blueprints(app) plugins = ('EC2Plugin', 'ECSPlugin') xray_recorder.configure(service='cartservice', plugins=plugins) XRayMiddleware(app, xray_recorder) patch_all() return app
def create_app(): # create and configure the app app = Flask(__name__, instance_relative_config=True) CORS(app) # AWS X-Ray # Should only be available in staging and production environment, (AWS cloud) # will not use AWS X-ray if environment is development if os.environ.get("FLASK_ENV") != 'development': plugins = ('EC2Plugin', 'ECSPlugin') xray_recorder.configure(service='myproject-customer-service', plugins=plugins) XRayMiddleware(app, xray_recorder) patch_all() # Add a blueprint for the customers module app.register_blueprint(customer_module) return app
def create_app(config=None, testing=False): """Application factory, used to create application """ app = Flask('catalog') configure_app(app, testing) register_blueprints(app) ddb = boto3.resource('dynamodb', region_name=app.config['AWS_REGION']) #app.config['ddb'] = ddb app.config['db'] = ddb.Table('ProductCatalog') plugins = ('EC2Plugin', 'ECSPlugin') xray_recorder.configure(service='catalogservice',plugins=plugins) XRayMiddleware(app, xray_recorder) patch_all() return app
def test_lambda_default_ctx(): # Track to make sure that Flask will default to generating segments if context is not the lambda context new_recorder = get_new_stubbed_recorder() new_recorder.configure(service='test', sampling=False) new_app = Flask(__name__) @new_app.route('/segment') def subsegment(): # Test in between request and make sure Lambda that uses default context generates a segment. assert new_recorder.current_segment() assert type(new_recorder.current_segment()) == segment.Segment return 'ok' XRayMiddleware(new_app, new_recorder) app_client = new_app.test_client() path = '/segment' app_client.get(path) segment = recorder.emitter.pop() assert not segment # Segment should be none because it's created and ended by the middleware
def create_app(config=None, testing=False): """Application factory, used to create application """ app = Flask('order') configure_app(app, testing) sqs = boto3.client('sqs', region_name=app.config['AWS_REGION']) queue_url = sqs.get_queue_url(QueueName=app.config['ORDER_QUEUE'])['QueueUrl'] app.config['sqs'] = sqs app.config['queue_url'] = queue_url register_blueprints(app) plugins = ('EC2Plugin', 'ECSPlugin') xray_recorder.configure(service='orderservice', plugins=plugins) XRayMiddleware(app, xray_recorder) patch_all() return app
def init_xray(app: App): if not app.config.get("XRAY"): return patcher.patch(("requests", "boto3")) # xray tracing for external requests xray_recorder.configure(service="TEMPLATE") XRayMiddleware(app, xray_recorder)
@app.route('/fault') def fault(): return {}['key'] @app.route('/template') def template(): return render_template_string('hello template') # add X-Ray middleware to flask app recorder = get_new_stubbed_recorder() recorder.configure(service='test', sampling=False, context=Context()) XRayMiddleware(app, recorder) # enable testing mode app.config['TESTING'] = True app = app.test_client() BASE_URL = 'http://localhost{}' @pytest.fixture(autouse=True) def cleanup(): """ Clean up context storage before and after each test run """ recorder.clear_trace_entities() yield
def create_app(): app = Flask(__name__) # Configure xray tracing xray_recorder.configure(service='dashboard') XRayMiddleware(app, xray_recorder) patch_all() # Setup logging setup_logging(log_levels.get(os.environ.get("LOG_LEVEL", "INFO"))) log = logging.getLogger(__name__) @app.errorhandler(HTTPException) def handle_error(error): error_dict = { "message": str(error) if isinstance(error, HTTPException) else '', "exc_type": type(error).__name__, "status_code": error.code } log.exception(error) return make_response( json.dumps({ "message": "Error occured", "code": error.code, "trace_id": get_trace_id(request.headers.get("X-Amzn-Trace-Id")), "request_id": get_request_id() }), 500) @app.route("/") def hello(): try: resp = requests.get("{}/".format(COUNTER_ENDPOINT), headers={"X-Request-Id": get_request_id()}) log.info("Received request, trace_id={}, req_id={}".format( get_trace_id(request.headers.get("X-Amzn-Trace-Id")), get_request_id())) if resp.status_code == 200: resp = json.loads(resp.text) resp = json.dumps({ "message": "Counter is reachable", "count": resp["count"], "counter_service_id": resp["counter_service_id"], "dashboard_service_id": socket.gethostname(), "trace_id": get_trace_id(request.headers.get("X-Amzn-Trace-Id")), "request_id": get_request_id() }) else: log.info( "Error calling {} service! code={}, service={}".format( resp.text, resp.status_code, COUNTER_ENDPOINT)) resp = make_response( json.dumps({ "message": "Error calling %s service" % COUNTER_ENDPOINT, "code": resp.status_code, "error": resp.text, "trace_id": get_trace_id(request.headers.get("X-Amzn-Trace-Id")), "request_id": get_request_id() }), 500) except requests.exceptions.RequestException as ex: log.info("Error connecting to counter at %s, %s" % (COUNTER_ENDPOINT, str(ex))) resp = make_response( json.dumps({ "message": str(ex), "trace_id": get_trace_id(request.headers.get("X-Amzn-Trace-Id")), "request_id": get_request_id() }), 500) return resp @app.route("/fail") def fail(): code = request.args.get('code') log.info( "/fail?code={} called! Relaying to counter service, trace_id={}, req_id={}" .format(code, get_trace_id(request.headers.get("X-Amzn-Trace-Id")), get_request_id())) resp = requests.get("{}/fail?code={}".format(COUNTER_ENDPOINT, code), headers={"X-Request-Id": get_request_id()}) resp = make_response( json.dumps({ "message": "Received code={} on {}/fail?code={}".format( resp.status_code, COUNTER_ENDPOINT, code) }), int(resp.status_code)) return resp @app.route("/health") def health(): return json.dumps({"status": "DASHBOARD_HEALTHY"}) return app
def create_app(): app = Flask(__name__) # Configure xray tracing xray_recorder.configure(service='counter') XRayMiddleware(app, xray_recorder) patch_all() # Setup logging setup_logging(log_levels.get(os.environ.get("LOG_LEVEL", "INFO"))) log = logging.getLogger(__name__) @app.errorhandler(HTTPException) def handle_error(error): error_dict = { "message": str(error) if isinstance(error, HTTPException) else '', "exc_type": type(error).__name__, "status_code": error.code } log.exception(error) return make_response( json.dumps({ "message": "Error occured", "code": error.code, "trace_id": get_trace_id(request.headers.get("X-Amzn-Trace-Id")), "request_id": request.headers.get("X-Request-Id") }), 500) @app.route("/") def hello(): '''Available Headers [ 'Host', 'User-Agent', 'Accept-Encoding', 'Accept', 'X-Amzn-Trace-Id', 'X-Forwarded-Proto', 'X-Request-Id', 'X-Envoy-Expected-Rq-Timeout-Ms', 'Content-Length', 'X-Forwarded-For', 'X-Envoy-Internal', 'X-Envoy-Downstream-Service-Cluster', 'X-Envoy-Downstream-Service-Node' ] ''' global count count = count + 1 log.info("Received request, trace_id={}, req_id={}".format( get_trace_id(request.headers.get("X-Amzn-Trace-Id")), request.headers.get("X-Request-Id"))) return json.dumps({ "count": count, "counter_service_id": socket.gethostname(), "trace_id": get_trace_id(request.headers.get("X-Amzn-Trace-Id")), "request_id": request.headers.get("X-Request-Id") }) @app.route("/fail") def fail(): code = int(request.args.get('code')) if code == 504: # Simulate 504 by timeout time.sleep(100) return make_response(json.dumps({})) elif code == 502: # Simulate 502 by abrupt exit sys.exit(1) else: # For 503 and 500 return the code msg = "/fail?code={} called! Responding with {}, trace_id={}, req_id={}".format( code, code, get_trace_id(request.headers.get("X-Amzn-Trace-Id")), request.headers.get("X-Request-Id")) log.info(msg) resp = make_response( json.dumps({ "message": msg, "trace_id": get_trace_id(request.headers.get("X-Amzn-Trace-Id")), "request_id": request.headers.get("X-Request-Id") }), int(code)) return resp @app.route("/health") def health(): return json.dumps({"status": "COUNTER_HEALTHY"}) return app
# newrelic.agent.initialize() from aws_xray_sdk.core import xray_recorder, patch_all from aws_xray_sdk.core.context import Context from aws_xray_sdk.ext.flask.middleware import XRayMiddleware from hub.app import create_app from shared.log import get_logger logger = get_logger() xray_recorder.configure(service="fxa.hub") patch_all() hub_app = create_app() XRayMiddleware(hub_app.app, xray_recorder) # TODO! # @newrelic.agent.lambda_handler() def handle(event, context): try: logger.info("handling hub event", subhub_event=event, context=context) return serverless_wsgi.handle_request(hub_app.app, event, context) except Exception as e: # pylint: disable=broad-except logger.exception("exception occurred", subhub_event=event, context=context, error=e) # TODO: Add Sentry exception catch here raise
def create_app(script_info=None): # instantiate the app app = Flask(__name__) flask_bcrypt = Bcrypt(app) jwt = JWTManager(app) app.json_encoder = JSONEncoder # enable CORS CORS(app, resources={r'/*': {'origins': '*'}}) # TODO 9: Review X-ray setting patch_modules = ( 'boto3', 'botocore', 'pynamodb', 'requests', ) plugins = ('EC2Plugin', ) xray_recorder.configure(service='CloudAlbum', plugins=plugins, context_missing='LOG_ERROR', sampling=False) XRayMiddleware(app, xray_recorder) patch(patch_modules) # set config app_settings = os.getenv('APP_SETTINGS') app.config.from_object(app_settings) # set logger to STDOUT app.logger.addHandler(logging.StreamHandler(sys.stdout)) app.logger.setLevel(logging.DEBUG) # register blueprints from cloudalbum.api.users import users_blueprint app.register_blueprint(users_blueprint, url_prefix='/users') from cloudalbum.api.photos import photos_blueprint app.register_blueprint(photos_blueprint, url_prefix='/photos') from cloudalbum.api.admin import admin_blueprint app.register_blueprint(admin_blueprint, url_prefix='/admin') @jwt.token_in_blacklist_loader def check_if_token_in_blacklist_DB(decrypted_token): from cloudalbum.util.jwt_helper import is_blacklisted_token_set try: return is_blacklisted_token_set(decrypted_token) except Exception as e: app.logger.error(e) return make_response( jsonify({'msg': 'session already expired'}, 409)) # shell context for flask cli @app.shell_context_processor def ctx(): return {'app': app} return app
if "timestamp" not in event or "message" not in event: continue logs.append(event) return dict(logs=logs) @app.route("/internal/application_secrets", methods=["GET"]) @time_limited(app) def get_application_secrets(): application_secret_file = os.environ["GOOGLE_APPLICATION_SECRETS"] with open(application_secret_file, 'r') as fh: data = json.loads(fh.read()) return chalice.Response( status_code=requests.codes.ok, headers={ 'Content-Type': "application/json", }, body=data, ) return app dss_app = create_app() if DSS_XRAY_TRACE: XRayMiddleware(dss_app.app, xray_recorder) app = get_chalice_app(dss_app.app)
application = Flask(__name__) application.secret_key = config.FLASK_SECRET login_manager = flask_login.LoginManager() login_manager.init_app(application) ### load and cache cognito JSON Web Key (JWK) # https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html JWKS_URL = ("https://cognito-idp.%s.amazonaws.com/%s/.well-known/jwks.json" % (config.AWS_REGION, config.COGNITO_POOL_ID)) JWKS = requests.get(JWKS_URL).json()["keys"] ### x-ray set up plugins = ('EC2Plugin', ) xray_recorder.configure(service='MyApplication', plugins=plugins) XRayMiddleware(application, xray_recorder) patch_all() ### FlaskForm set up class PhotoForm(FlaskForm): """flask_wtf form class the file upload""" photo = FileField('image', validators=[FileRequired()]) description = TextAreaField(u'Image Description') class User(flask_login.UserMixin): """Standard flask_login UserMixin""" pass
"cache-control", "cookie", "session-id", ), supports_credentials=True, ) config = get_config() logger = getLogger(__name__) cis_environment = config("environment", namespace="cis") # Configure the X-Ray recorder to generate segments with our service name xray_recorder.configure( service="{}_profile_retrieval_serivce".format(cis_environment)) # Instrument the Flask application XRayMiddleware(app, xray_recorder) if config("initialize_vault", namespace="person_api", default="false") == "true": logger.debug( "Initializing vault and pre-seeding it, this will take some time...") initialize_vault() seed() logger.debug("Vault is seeded and ready to go!") authorization_middleware = AuthorizationMiddleware() dynamodb_table = get_table_resource() dynamodb_client = get_dynamodb_client() transactions = config("transactions", namespace="cis", default="false")
import logging from aws_xray_sdk.core import xray_recorder from aws_xray_sdk.ext.flask.middleware import XRayMiddleware xray_recorder.configure(context_missing='LOG_ERROR') from aws_xray_sdk.core import patch_all flask_app = Flask(__name__) flask_app.debug = True log_level = logging.INFO flask_app.logger.setLevel(log_level) # enable CORS CORS(flask_app, resources={r'/*': {'origins': '*'}}) #configure SDK code xray_recorder.configure(service='Product-Catalog') XRayMiddleware(flask_app, xray_recorder) AGG_APP_URL = os.environ.get("AGG_APP_URL") if AGG_APP_URL is None: AGG_APP_URL="http://localhost:3000/catalogDetail" flask_app.logger.info('AGG_APP_URL is ' + str(AGG_APP_URL)) # Fix of returning swagger.json on HTTP @property def specs_url(self): """ The Swagger specifications absolute url (ie. `swagger.json`) :rtype: str