def start_test_span(cls, name, test_suite_name, test_case_id, app_info): try: test_wrapper = TestWrapper.get_instance() test_wrapper.change_app_info(app_info) current_context = ExecutionContextManager.get() parent_transaction_id = current_context.invocation_data.get("transactionId") context = test_wrapper.create_test_case_execution_context(name, test_suite_name, test_case_id, app_info, parent_transaction_id) ExecutionContextManager.set(context) test_wrapper.before_test_process(context) except Exception as e: logger.error("Handler_utils start_test_span error: {}".format(e)) pass
def start_after_all_span(cls, app_info, span_tags): """after all executed after test cases. That is why context should be getting from TestRunnerSupport and set into ExecutionContextManager. """ try: context = TestRunnerSupport.test_suite_execution_context ExecutionContextManager.set(context) return cls.create_span(cls.TEST_AFTER_ALL_OPERATION_NAME, app_info, span_tags) except Exception as e: logger.error("Handler_utils start_after_all_span error: {}".format(e)) pass return None
def finish_test_span(): """Setup TestRunnerSupport for test suite. It's executed after all process has been done for test case such as before_each, after each etc. """ try: test_wrapper = TestWrapper.get_instance() context = ExecutionContextManager.get() test_wrapper.after_test_process(context) app_info = TestRunnerSupport.test_suite_application_info context = TestRunnerSupport.test_suite_execution_context test_wrapper.change_app_info(app_info) ExecutionContextManager.set(context) except Exception as e: logger.error("Handler_utils finish_test_span error: {}".format(e))
def start_test_suite_span(cls, test_suite_id, app_info): """For now, TestRunnerSupport not support concurrency. Only one test suite info is kept there. It should be changed for concurrent python test framework. """ try: if not TestRunnerSupport.test_suite_execution_context: test_wrapper = TestWrapper.get_instance() context = test_wrapper.create_test_suite_execution_context(test_suite_id) ExecutionContextManager.set(context) test_wrapper.change_app_info(app_info) TestRunnerSupport.set_test_suite_application_info(app_info) TestRunnerSupport.set_test_suite_execution_context(context) test_wrapper.before_test_process(context) except Exception as e: logger.error("Handler_utils start_test_suite_span error: {}".format(e)) pass
def test_get_resources(mocked_get_spans, mocked_span): span_args = [ { 'id': '0', 'cn': 'Class0', 'on': 'operation0', 'ot': 'type0', 'd': 37, 'tv': True, 'e': False, 'et': '', 'v': None, 'b': None }, { 'id': '1', 'cn': 'Class1', 'on': 'operation1', 'ot': 'type1', 'd': 37, 'tv': True, 'e': True, 'et': 'AnErrorType', 'v': True, 'b': True }, { 'id': '2', 'cn': 'Class1', 'on': 'operation1', 'ot': 'type1', 'd': 73, 'tv': True, 'e': True, 'et': 'AnotherErrorType', 'v': True, 'b': None }, { 'id': '3', 'cn': 'Class2', 'on': 'operation2', 'ot': 'type2', 'd': 38, 'tv': False, 'e': False, 'et': '', 'v': True, 'b': True }, { 'id': '4', 'cn': 'Class2', 'on': 'operation2', 'ot': 'type2', 'd': 83, 'tv': True, 'e': True, 'et': 'WeirdError', 'v': True, 'b': None }, ] spans = [ mocked_span(span_id=args['id'], class_name=args['cn'], operation_name=args['on'], topology_vertex=args['tv'], operation_type=args['ot'], errorneous=args['e'], error_type=args['et'], duration=args['d'], violated=args['v'], blocked=args['b']) for args in span_args ] mocked_get_spans.return_value = spans spans[0].context.span_id = spans[0].span_id ExecutionContextManager.set(ExecutionContext(root_span=spans[0])) resources = invocation_trace_support.get_resources()['resources'] if resources[0]['resourceCount'] == 2: r1, r2 = resources[0], resources[1] else: r1, r2 = resources[1], resources[0] assert len(resources) == 2 assert r1['resourceType'] == 'Class1' assert r1['resourceName'] == 'operation1' assert r1['resourceOperation'] == 'type1' assert r1['resourceCount'] == 2 assert r1['resourceErrorCount'] == 2 assert r1['resourceDuration'] == 110 assert r1['resourceMaxDuration'] == 73 assert r1['resourceAvgDuration'] == 55 assert r1['resourceViolatedCount'] == 2 assert r1['resourceBlockedCount'] == 1 assert len(r1['resourceErrors']) == 2 assert 'AnErrorType' in r1['resourceErrors'] assert 'AnotherErrorType' in r1['resourceErrors'] assert r2['resourceType'] == 'Class2' assert r2['resourceName'] == 'operation2' assert r2['resourceOperation'] == 'type2' assert r2['resourceCount'] == 1 assert r2['resourceErrorCount'] == 1 assert r2['resourceDuration'] == 83 assert r2['resourceMaxDuration'] == 83 assert r2['resourceAvgDuration'] == 83 assert r2['resourceViolatedCount'] == 1 assert r2['resourceBlockedCount'] == 0 assert len(r2['resourceErrors']) == 1 assert 'WeirdError' in r2['resourceErrors']
def teardown(): yield ConfigProvider.clear() ExecutionContextManager.set(ExecutionContext())
def wrapper(event, context): application_name = self.plugin_context.application_info.get( 'applicationName') self.application_info_provider.update({ 'applicationId': LambdaApplicationInfoProvider.get_application_id( context, application_name=application_name) }) # Execution context initialization execution_context = wrapper_utils.create_execution_context() try: execution_context.platform_data[ 'originalEvent'] = copy.deepcopy(event) except: execution_context.platform_data['originalEvent'] = event execution_context.platform_data['originalContext'] = context ExecutionContextManager.set(execution_context) # Before running user's handler try: if ConfigProvider.get( config_names.THUNDRA_LAMBDA_WARMUP_WARMUPAWARE, False) and self.check_and_handle_warmup_request(event): return None self.plugin_context.request_count += 1 self.execute_hook('before:invocation', execution_context) timeout_duration = self.get_timeout_duration(context) except Exception as e: logger.error( "Error during the before part of Thundra: {}".format(e)) return original_func(event, context) # Invoke user handler try: response = None with Timeout(timeout_duration, self.timeout_handler, execution_context): if ConfigProvider.get( config_names.THUNDRA_LAMBDA_DEBUGGER_ENABLE, ConfigProvider.get( config_names.THUNDRA_LAMBDA_DEBUGGER_AUTH_TOKEN )) and self.ptvsd_imported: self.start_debugger_tracing(context) response = original_func(event, context) execution_context.response = response except Exception as e: try: execution_context.error = { 'type': type(e).__name__, 'message': str(e), 'traceback': traceback.format_exc() } self.prepare_and_send_reports(execution_context) except Exception as e_in: logger.error( "Error during the after part of Thundra: {}".format( e_in)) pass raise e finally: if ConfigProvider.get( config_names.THUNDRA_LAMBDA_DEBUGGER_ENABLE, ConfigProvider.get( config_names.THUNDRA_LAMBDA_DEBUGGER_AUTH_TOKEN) ) and self.ptvsd_imported: self.stop_debugger_tracing() # After having run the user's handler try: self.prepare_and_send_reports(execution_context) except Exception as e: logger.error( "Error during the after part of Thundra: {}".format(e)) ExecutionContextManager.clear() return response