def test_external_submodules_ignores_file():
    patcher.patch(['tests.mock_module'],
                  ignore_module_patterns=['tests.mock_module.mock_file'])
    assert len(xray_recorder.current_segment().subsegments) == 0
    # We want to make sure patching does not load any of the patched modules
    imported_modules = [
        module for module in TEST_MODULES if module in sys.modules
    ]
    assert not imported_modules

    _call_all_mock_functions()

    assert len(xray_recorder.current_segment().subsegments) == 9
    assert xray_recorder.current_segment().subsegments[0].name == 'mock_init'
    assert xray_recorder.current_segment(
    ).subsegments[1].name == 'mock_subinit'
    assert xray_recorder.current_segment(
    ).subsegments[2].name == 'mock_subfunc'
    assert xray_recorder.current_segment(
    ).subsegments[3].name == 'mock_no_doublepatch'
    assert xray_recorder.current_segment(
    ).subsegments[4].name == 'mock_staticmethod'
    assert xray_recorder.current_segment(
    ).subsegments[5].name == 'MockClass.__init__'
    assert xray_recorder.current_segment().subsegments[6].name == 'mock_method'
    assert xray_recorder.current_segment(
    ).subsegments[7].name == 'MockSubclass.__init__'
    assert xray_recorder.current_segment(
    ).subsegments[8].name == 'mock_submethod'
def test_disable_sdk_disables_patching():
    global_sdk_config.set_sdk_enabled(False)
    patcher.patch(['tests.mock_module'])
    imported_modules = [
        module for module in TEST_MODULES if module in sys.modules
    ]
    assert not imported_modules
    assert len(xray_recorder.current_segment().subsegments) == 0
def test_external_file():
    patcher.patch(['tests.mock_module.mock_file'])
    assert len(xray_recorder.current_segment().subsegments) == 0
    # We want to make sure patching does not load any of the patched modules
    imported_modules = [
        module for module in TEST_MODULES if module in sys.modules
    ]
    assert not imported_modules

    _call_all_mock_functions()

    assert len(xray_recorder.current_segment().subsegments) == 2
    assert xray_recorder.current_segment().subsegments[0].name == 'mock_func'
    assert xray_recorder.current_segment().subsegments[
        1].name == 'mock_no_doublepatch'  # It is patched with decorator
Example #4
0
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)
def test_incorrect_import_fails(modules):
    with pytest.raises(Exception) as e:
        patcher.patch(modules)
    assert str(
        e.value
    ) == 'modules nonexisting.module are currently not supported for patching'
Example #6
0
import boto3
from boto3.dynamodb.conditions import Key
from fiveoneone.stop import Stop
from flask import Flask, render_template
from flask_ask import Ask, question, session, statement

TOKEN = os.getenv("FIVEONEONE_TOKEN")
DYNAMO_ENDPOINT = os.getenv("DYNAMO_ENDPOINT", None)
DYNAMO_REGION = os.getenv("DYNAMO_REGION", "us-east-1")
LOGLEVEL = os.getenv("LOGLEVEL", "INFO")
STOPID_KEY = "stopid"
STOPNAME_KEY = "stopname"
BUSES_KEY = "buses"

# Patch the requests module to enable automatic instrumentation
patcher.patch(('requests', ))

# pylint: disable=C0103
app = Flask(__name__)
app.config["ASK_APPLICATION_ID"] = os.getenv("APPLICATION_ID")

# pylint: disable=C0103
ask = Ask(app, "/")

logging.getLogger("flask_ask").setLevel(LOGLEVEL)
logging.getLogger(__name__).setLevel(LOGLEVEL)
log = logging.getLogger(__name__)

# Configure the X-Ray recorder to generate segments with our service name
xray_recorder.configure(service='mybus')