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
async def test_ok_name(loop, recorder): xray_recorder.begin_segment('name') trace_config = aws_xray_trace_config(name='test') 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): pass subsegment = xray_recorder.current_segment().subsegments[0] assert subsegment.name == 'test'
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
async def _fetch_stories(): trace_config = aws_xray_trace_config() # Use a single session for connection pooling async with ClientSession(trace_configs=[trace_config]) as session: story_ids = await _fetch_story_ids(session) if not story_ids: return [] # fetch stories in parallel with async tasks tasks = [ _fetch_story(session, story_ids[i]) for i in range(NUM_STORIES) ] return await asyncio.gather(*tasks)
def aiohttp_trace_config(): """aiohttp extension for X-Ray (aws_xray_trace_config) It expects you to have aiohttp as a dependency. Returns ------- TraceConfig aiohttp trace config """ from aws_xray_sdk.ext.aiohttp.client import aws_xray_trace_config # pragma: no cover aws_xray_trace_config.__doc__ = "aiohttp extension for X-Ray (aws_xray_trace_config)" # pragma: no cover return aws_xray_trace_config() # pragma: no cover
async def test_fault(loop, recorder): xray_recorder.begin_segment('name') trace_config = aws_xray_trace_config() status_code = 500 url = 'http://{}/status/{}'.format(BASE_URL, status_code) async with ClientSession(loop=loop, trace_configs=[trace_config]) as session: async with session.put(url): pass subsegment = xray_recorder.current_segment().subsegments[0] assert subsegment.name == url assert subsegment.fault http_meta = subsegment.http assert http_meta['request']['url'] == url assert http_meta['request']['method'] == 'PUT' assert http_meta['response']['status'] == status_code
async def test_ok(loop, recorder): xray_recorder.begin_segment('name') 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): pass subsegment = xray_recorder.current_segment().subsegments[0] assert subsegment.name == strip_url(url) assert subsegment.namespace == REMOTE_NAMESPACE http_meta = subsegment.http assert http_meta['request']['url'] == url assert http_meta['request']['method'] == 'GET' assert http_meta['response']['status'] == status_code
async def test_invalid_url(loop, recorder): xray_recorder.begin_segment('name') trace_config = aws_xray_trace_config() async with ClientSession(loop=loop, trace_configs=[trace_config]) as session: try: async with session.get('http://doesnt.exist'): pass except Exception: # prevent uncatch exception from breaking test run pass subsegment = xray_recorder.current_segment().subsegments[0] assert subsegment.namespace == LOCAL_NAMESPACE assert subsegment.fault exception = subsegment.cause['exceptions'][0] assert exception.type == 'ClientConnectorError'
async def test_throttle(loop, recorder): xray_recorder.begin_segment('name') trace_config = aws_xray_trace_config() status_code = 429 url = 'http://{}/status/{}'.format(BASE_URL, status_code) async with ClientSession(loop=loop, trace_configs=[trace_config]) as session: async with session.head(url): pass subsegment = xray_recorder.current_segment().subsegments[0] assert subsegment.name == get_hostname(url) assert subsegment.error assert subsegment.throttle http_meta = subsegment.http assert http_meta['request']['url'] == strip_url(url) assert http_meta['request']['method'] == 'HEAD' assert http_meta['response']['status'] == status_code
def create_session(): if is_xray_on(): trace_config = aws_xray_trace_config() return aiohttp.ClientSession(trace_configs=[trace_config]) return aiohttp.ClientSession()