コード例 #1
0
async def test_capture(loop):
    xray_recorder.configure(service='test',
                            sampling=False,
                            context=AsyncContext(loop=loop))

    segment = xray_recorder.begin_segment('name')

    await async_method()

    # Check subsegment is created from async_method
    assert len(segment.subsegments) == 1
    assert segment.subsegments[0].name == 'test_1'

    # Check nested subsegment is created from async_method2
    subsegment = segment.subsegments[0]
    assert len(subsegment.subsegments) == 1
    assert subsegment.subsegments[0].name == 'test_2'

    # Check runtime context is correctly attached
    xray_meta = segment.aws.get('xray')
    assert 'X-Ray for Python' == xray_meta.get('sdk')
    assert VERSION == xray_meta.get('sdk_version')

    service = segment.service
    assert platform.python_implementation() == service.get('runtime')
    assert platform.python_version() == service.get('runtime_version')
コード例 #2
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()
コード例 #3
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()
コード例 #4
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
コード例 #5
0
async def test_async_context_managers(loop):
    xray_recorder.configure(service='test',
                            sampling=False,
                            context=AsyncContext(loop=loop))

    async with xray_recorder.in_segment_async('segment') as segment:
        async with xray_recorder.capture_async('aio_capture') as subsegment:
            assert segment.subsegments[0].name == 'aio_capture'
        assert subsegment.in_progress is False
        async with xray_recorder.in_subsegment_async('in_sub') as subsegment:
            assert segment.subsegments[1].name == 'in_sub'
            assert subsegment.in_progress is True
        assert subsegment.in_progress is False
コード例 #6
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')
コード例 #7
0
def recorder(loop):
    """
    Clean up context storage before and after each test run
    """
    xray_recorder = get_new_stubbed_recorder()
    xray_recorder.configure(service='test', sampling=False, context=AsyncContext(loop=loop))

    patcher = patch('aws_xray_sdk.ext.aiohttp.middleware.xray_recorder', xray_recorder)
    patcher.start()

    xray_recorder.clear_trace_entities()
    yield xray_recorder
    xray_recorder.clear_trace_entities()
    patcher.stop()
コード例 #8
0
ファイル: test_xray_middleware.py プロジェクト: jmmshn/api
def recorder():
    """
    Clean up context storage before and after each test run
    """

    loop = asyncio.get_event_loop()
    xray_recorder = get_new_stubbed_recorder()
    xray_recorder.configure(service="test",
                            sampling=False,
                            context=AsyncContext(loop=loop))

    xray_recorder.clear_trace_entities()
    yield xray_recorder

    global_sdk_config.set_sdk_enabled(True)
    xray_recorder.clear_trace_entities()
コード例 #9
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)
コード例 #10
0
async def test_context_missing_not_swallow_return(loop, recorder):
    xray_recorder.configure(service='test',
                            sampling=False,
                            context=AsyncContext(loop=loop),
                            context_missing='LOG_ERROR')

    response = {
        'ResponseMetadata': {
            'RequestId': '1234',
            'HTTPStatusCode': 403
        }
    }

    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_response('describe_table', response,
                                 {'TableName': 'mytable'})
            actual_resp = await client.describe_table(TableName='mytable')

    assert actual_resp == response
コード例 #11
0
from aws_xray_sdk.core import xray_recorder, patch_all
from aws_xray_sdk.core.async_context import AsyncContext


def is_xray_available():
    return os.environ.get("XRAY", None) is not None


def is_lambda():
    if os.environ.get(
            'AWS_REGION') and not os.environ.get('CODEBUILD_AGENT_NAME'):
        return True
    return False


# if is_lambda() and is_xray_available():
xray_recorder.configure(sampling=False,
                        context_missing='LOG_ERROR',
                        daemon_address='127.0.0.1:3000',
                        context=AsyncContext())
patch_all()


def xray_profile(func):
    async def wrapper(*args, **kwargs):
        async with xray_recorder.in_segment_async(name=func.__name__):
            return await func(*args, **kwargs)

    return wrapper
コード例 #12
0
import aiohttp
import asyncio

from aws_xray_sdk.core.async_context import AsyncContext
from aws_xray_sdk.core import xray_recorder

xray_recorder.configure(service='repro_xray_issue', context=AsyncContext())


@xray_recorder.capture_async('download_file')
async def download_file(url, session):
    async with session.get(url) as response:
        data = await response.read()
        print(response.status, len(data))


async def download_files():
    urls = [
        'https://file-examples.com/wp-content/uploads/2017/10/file_example_JPG_100kB.jpg',
        'https://file-examples.com/wp-content/uploads/2017/10/file_example_JPG_500kB.jpg',
        'https://file-examples.com/wp-content/uploads/2017/10/file_example_JPG_1MB.jpg',
        'https://file-examples.com/wp-content/uploads/2017/10/file_example_JPG_2500kB.jpg',
    ]
    async with aiohttp.ClientSession() as session:
        tasks = []
        for url in urls:
            tasks.append(asyncio.create_task(download_file(url, session)))
        for task in tasks:
            await task