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()
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 = Bottle() @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' plugin = XRayMiddleware(new_recorder) plugin._in_lambda_ctx = True new_app.install(plugin) app_client = WebApp(new_app) path = '/subsegment' app_client.get(path) new_app.get(path) segment = recorder.emitter.pop() assert not segment # Segment should be none because it's created and ended by the plugin path2 = '/trace_header' app_client.get(path2, headers={http.XRAY_HEADER: 'k1=v1'})
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 test_lambda_serverless(self): 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) subsegment = new_recorder.begin_subsegment("subsegment") assert type(subsegment.parent_segment) == facade_segment.FacadeSegment new_recorder.end_subsegment() url = reverse('200ok') self.client.get(url) segment = new_recorder.emitter.pop() assert not segment
def test_lambda_serverless(self): 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) subsegment = new_recorder.begin_subsegment("subsegment") assert type(subsegment.parent_segment) == facade_segment.FacadeSegment new_recorder.end_subsegment() url = reverse('200ok') self.client.get(url) segment = new_recorder.emitter.pop() assert not segment # Test Fault in Lambda url = reverse('500fault') try: self.client.get(url) except Exception: pass segment = xray_recorder.emitter.pop() assert segment.fault request = segment.http['request'] response = segment.http['response'] assert request['method'] == 'GET' assert request['client_ip'] == '127.0.0.1' assert response['status'] == 500 exception = segment.cause['exceptions'][0] assert exception.type == 'KeyError'
def error(): return 'Not Found', 404 @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 """