def construct_ctx():
    """
    Clean up context storage on each test run and begin a segment
    so that later subsegment can be attached. After each test run
    it cleans up context storage again.
    """
    pre_run_modules = set(module for module in sys.modules.keys())

    xray_recorder.configure(service='test', sampling=False, context=Context())
    xray_recorder.clear_trace_entities()
    xray_recorder.begin_segment('name')
    yield
    xray_recorder.end_segment()
    xray_recorder.clear_trace_entities()

    # Reload wrapt.importer references to modules to start off clean
    reload(wrapt)
    reload(wrapt.importer)
    # Reload patcher references to already patched modules
    reload(patcher)
    # Cleanup the already imported module references in the system
    for module_name, module in sorted(sys.modules.items(),
                                      key=lambda m: len(m[0]),
                                      reverse=True):
        if module_name not in pre_run_modules and inspect.ismodule(module):
            reload(module)

    for module_name in sorted(sys.modules.keys(),
                              key=lambda m: len(m),
                              reverse=True):
        if module_name not in pre_run_modules:
            del sys.modules[module_name]
示例#2
0
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 configure_xray(service):
    if not os.environ.get('DISABLE_XRAY', False):
        xray_recorder.configure(
            context_missing='LOG_ERROR',
            service=service
        )
        patch_all()
示例#4
0
    def ready(self):
        """
        Configure global XRay recorder based on django settings
        under XRAY_RECORDER namespace.
        This method could be called twice during server startup
        because of base command and reload command.
        So this function must be idempotent
        """
        if not settings.AWS_XRAY_TRACING_NAME:
            raise SegmentNameMissingException('Segment name is required.')

        xray_recorder.configure(
            daemon_address=settings.AWS_XRAY_DAEMON_ADDRESS,
            sampling=settings.SAMPLING,
            sampling_rules=settings.SAMPLING_RULES,
            context_missing=settings.AWS_XRAY_CONTEXT_MISSING,
            plugins=settings.PLUGINS,
            service=settings.AWS_XRAY_TRACING_NAME,
            dynamic_naming=settings.DYNAMIC_NAMING,
            streaming_threshold=settings.STREAMING_THRESHOLD,
            max_trace_back=settings.MAX_TRACE_BACK,
        )

        # if turned on subsegment will be generated on
        # built-in database and template rendering
        if settings.AUTO_INSTRUMENT:
            try:
                patch_db()
            except Exception:
                log.debug('failed to patch Django built-in database')
            try:
                patch_template()
            except Exception:
                log.debug('failed to patch Django built-in template engine')
示例#5
0
def maybe_use_xray():
    if "AWS_XRAY_DAEMON_ADDRESS" in os.environ:
        from aws_xray_sdk.core import xray_recorder
        from aws_xray_sdk.core import patch_all

        xray_recorder.configure()
        patch_all()
示例#6
0
def recorder(loop):
    """
    Initiate a recorder and clear it up once has been used.
    """
    xray_recorder.configure(service='test', sampling=False, context=AsyncContext(loop=loop))
    xray_recorder.clear_trace_entities()
    yield recorder
    xray_recorder.clear_trace_entities()
示例#7
0
 def initialize(cls):
     context = XrayContext()
     xray_recorder.configure(emitter=cls.use_daemon is False and cls.emitter
                             or None,
                             context=context,
                             sampling=True,
                             context_missing='LOG_ERROR')
     patch(['boto3', 'requests'])
     logging.getLogger('aws_xray_sdk.core').setLevel(logging.ERROR)
示例#8
0
def reset_recorder(insanic_application):
    Incendiary.load_config(insanic_application.config)

    yield
    from incendiary.xray.sampling import IncendiaryDefaultSampler

    insanic_application.sampler = IncendiaryDefaultSampler(insanic_application)
    xray_recorder.configure(**Incendiary.xray_config(insanic_application))
    xray_recorder.sampler = DefaultSampler()
示例#9
0
async def test_no_segment_raise(loop, recorder):
    xray_recorder.configure(context_missing='RUNTIME_ERROR')
    trace_config = aws_xray_trace_config()
    status_code = 200
    url = 'http://{}/status/{}?foo=bar'.format(BASE_URL, status_code)
    with pytest.raises(SegmentNotFoundException):
        async with ClientSession(loop=loop, trace_configs=[trace_config]) as session:
            async with session.get(url):
                pass
示例#10
0
def session():
    """Test Fixture to Create DataBase Tables and start a trace segment"""
    xray_recorder.configure(service='test', sampling=False, context=Context())
    xray_recorder.clear_trace_entities()
    xray_recorder.begin_segment('SQLAlchemyTest')
    db.create_all()
    yield
    xray_recorder.end_segment()
    xray_recorder.clear_trace_entities()
示例#11
0
 def initialize(cls):
     context = XrayContext()
     xray_recorder.configure(
         emitter=cls.use_daemon is False and cls.emitter or None,
         context=context,
         sampling=True,
         context_missing='LOG_ERROR')
     patch(['boto3', 'requests'])
     logging.getLogger('aws_xray_sdk.core').setLevel(logging.ERROR)
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
示例#13
0
 def configured_xray_recorder(self, factory_xray_context):
     self.counter = 0
     xray_recorder.configure(
         service="test",
         sampling=False,
         context=factory_xray_context,
         # emitter=StubbedEmitter()
         daemon_address="localhost:2000",
     )
     return xray_recorder
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)
示例#15
0
def recorder(loop):
    """
    Clean up before and after each test run
    """
    xray_recorder.configure(service='test',
                            sampling=False,
                            context=AsyncContext(loop=loop))
    xray_recorder.clear_trace_entities()
    yield xray_recorder
    xray_recorder.clear_trace_entities()
示例#16
0
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()
示例#17
0
def connection(engine):
    conn = engine.connect()
    xray_recorder.configure(service='test', sampling=False, context=Context())
    xray_recorder.clear_trace_entities()
    xray_recorder.begin_segment('SQLAlchemyTest')
    Session = XRaySessionMaker(bind=conn)
    Base.metadata.create_all(engine)
    session = Session()
    yield session
    xray_recorder.end_segment()
    xray_recorder.clear_trace_entities()
示例#18
0
async def test_no_segment_not_raise(loop, recorder):
    xray_recorder.configure(context_missing='LOG_ERROR')
    trace_config = aws_xray_trace_config()
    status_code = 200
    url = 'http://{}/status/{}?foo=bar'.format(BASE_URL, status_code)
    async with ClientSession(loop=loop, trace_configs=[trace_config]) as session:
        async with session.get(url) as resp:
            status_received = resp.status

    # Just check that the request was done correctly
    assert status_received == status_code
示例#19
0
def construct_ctx():
    """
    Clean up context storage on each test run and begin a segment
    so that later subsegment can be attached. After each test run
    it cleans up context storage again.
    """
    xray_recorder.configure(service='test', sampling=False, context=Context())
    xray_recorder.clear_trace_entities()
    xray_recorder.begin_segment('name')
    yield
    xray_recorder.clear_trace_entities()
示例#20
0
async def init_func(argv=None):
    middlewares = list()
    if is_xray_on():
        xray_recorder.configure(service="translation-api",
                                sampling=False,
                                context=AsyncContext(),
                                daemon_address="xray-aws-xray:2000")
        middlewares.append(xray_middleware)
    app = aiohttp.web.Application(middlewares=middlewares)
    app.add_routes([aiohttp.web.get('/translate', handle)])
    setup_swagger(app)
    return app
示例#21
0
def session():
    """Test Fixture to Create DataBase Tables and start a trace segment"""
    engine = create_engine('sqlite:///:memory:')
    xray_recorder.configure(service='test', sampling=False, context=Context())
    xray_recorder.clear_trace_entities()
    xray_recorder.begin_segment('SQLAlchemyTest')
    Session = XRaySessionMaker(bind=engine)
    Base.metadata.create_all(engine)
    session = Session()
    yield session
    xray_recorder.end_segment()
    xray_recorder.clear_trace_entities()
示例#22
0
    def test_it(self):
        loop = asyncio.new_event_loop()
        xray_recorder.configure(context=AsyncContext(loop=loop))

        async def worker(sleep_time):
            await asyncio.sleep(sleep_time)

        async def launch():
            with xray_recorder.in_segment_async("seg1"):
                async with xray_recorder.capture_async("bal"):
                    await asyncio.gather(worker(0.3), worker(2), worker(1))

        loop.run_until_complete(launch())
示例#23
0
async def test_context_missing_not_suppress_exception(loop, recorder):
    xray_recorder.configure(service='test',
                            sampling=False,
                            context=AsyncContext(loop=loop),
                            context_missing='LOG_ERROR')

    session = aiobotocore.get_session(loop=loop)
    async with session.create_client('dynamodb',
                                     region_name='eu-west-2') as client:
        with Stubber(client) as stubber:
            stubber.add_client_error('describe_table',
                                     expected_params={'TableName': ANY})
            with pytest.raises(ClientError):
                await client.describe_table(TableName='mytable')
示例#24
0
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)
示例#25
0
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"])
示例#26
0
 async def set_context(future):
     service_name = os.environ["AWS_LAMBDA_FUNCTION_NAME"]
     current = xray_recorder.current_segment()
     original_context = xray_recorder.context
     xray_context = AsyncContext()
     xray_context.set_trace_entity(current)
     xray_recorder.configure(service=service_name, context=xray_context)
     try:
         result = await future
         return result
     finally:
         xray_recorder.configure(service=service_name,
                                 context=original_context)
         xray_context.set_trace_entity(current)
示例#27
0
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.")
示例#28
0
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 engine():
    """
    Clean up context storage on each test run and begin a segment
    so that later subsegment can be attached. After each test run
    it cleans up context storage again.
    """
    from aws_xray_sdk.ext.sqlalchemy_core import unpatch
    patch(('sqlalchemy_core',))
    engine = create_engine('sqlite:///:memory:')
    xray_recorder.configure(service='test', sampling=False, context=Context())
    xray_recorder.begin_segment('name')
    Base.metadata.create_all(engine)
    xray_recorder.clear_trace_entities()
    xray_recorder.begin_segment('name')
    yield engine
    xray_recorder.clear_trace_entities()
    unpatch()
示例#30
0
def construct_ctx():
    """
    Clean up context storage on each test run and begin a segment
    so that later subsegment can be attached. After each test run
    it cleans up context storage again.
    """
    from aws_xray_sdk.ext.httplib import unpatch, reset_ignored

    patch(('httplib',))
    xray_recorder.configure(service='test', sampling=False, context=Context())
    xray_recorder.clear_trace_entities()
    xray_recorder.begin_segment('name')

    yield
    xray_recorder.clear_trace_entities()
    unpatch()
    reset_ignored()
示例#31
0
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
import os
import socket
import os
from flask import Flask, request

#https://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/basic.html
# #Lets try to use AWS X-ray for metrics / logging if available to us
try:
    from aws_xray_sdk.core import xray_recorder
    from aws_xray_sdk.core import patch_all
    from aws_xray_sdk.ext.flask.middleware import XRayMiddleware
    # xray_recorder.configure(context_missing=LOG_ERROR)
    # xray_recorder.configure(sampling=False)
    plugins = ('EC2Plugin','ECSPlugin')
    xray_recorder.configure(plugins=plugins)
    patch_all()
except:
    logging.exception('Failed to import X-ray')

app = Flask(__name__)


try:
    xray_recorder.configure(service='Flask App')
    XRayMiddleware(app, xray_recorder)
except:
    logging.exception('Failed to load X-ray')


@app.route('/')
def hello():