def run_integration(func_container, ctx, *args, **kwargs): try: int_name = func_container.QONTRACT_INTEGRATION.replace('_', '-') running_state = RunningState() running_state.integration = int_name except AttributeError: sys.stderr.write("Integration missing QONTRACT_INTEGRATION.\n") sys.exit(ExitCodes.ERROR) try: gql.init_from_config(sha_url=ctx['gql_sha_url'], integration=int_name, validate_schemas=ctx['validate_schemas'], print_url=ctx['gql_url_print']) except GqlApiIntegrationNotFound as e: sys.stderr.write(str(e) + "\n") sys.exit(ExitCodes.INTEGRATION_NOT_FOUND) unleash_feature_state = get_feature_toggle_state(int_name) if not unleash_feature_state: logging.info('Integration toggle is disabled, skipping integration.') sys.exit(ExitCodes.SUCCESS) dry_run = ctx.get('dry_run', False) try: func_container.run(dry_run, *args, **kwargs) except RunnerException as e: sys.stderr.write(str(e) + "\n") sys.exit(ExitCodes.ERROR) except GqlApiErrorForbiddenSchema as e: sys.stderr.write(str(e) + "\n") sys.exit(ExitCodes.FORBIDDEN_SCHEMA) except GqlApiError as e: if '409' in str(e): logging.info(f'Data changed during execution. This is fine.') # exit code to relect conflict # TODO: document this better sys.exit(ExitCodes.DATA_CHANGED) else: raise e finally: if ctx.get('dump_schemas_file'): gqlapi = gql.get_api() with open(ctx.get('dump_schemas_file'), 'w') as f: f.write(json.dumps(gqlapi.get_queried_schemas()))
def init_from_config(sha_url=True, integration=None, validate_schemas=False, print_url=True): config = get_config() server_url = urlparse(config['graphql']['server']) server = server_url.geturl() token = config['graphql'].get('token') if sha_url: sha = get_sha(server_url, token) server = server_url._replace(path=f'/graphqlsha/{sha}').geturl() runing_state = RunningState() git_commit_info = get_git_commit_info(sha, server_url, token) runing_state.timestamp = git_commit_info.get('timestamp') runing_state.commit = git_commit_info.get('commit') if print_url: logging.info(f'using gql endpoint {server}') return init(server, token, integration, validate_schemas)
def init_from_config( sha_url=True, integration=None, validate_schemas=False, print_url=True ): config = get_config() server_url = urlparse(config["graphql"]["server"]) server = server_url.geturl() token = config["graphql"].get("token") if sha_url: sha = get_sha(server_url, token) server = server_url._replace(path=f"/graphqlsha/{sha}").geturl() runing_state = RunningState() git_commit_info = get_git_commit_info(sha, server_url, token) runing_state.timestamp = git_commit_info.get("timestamp") runing_state.commit = git_commit_info.get("commit") if print_url: logging.info(f"using gql endpoint {server}") return init(server, token, integration, validate_schemas)
def wrapper(*args, **kwargs): runing_state = RunningState() result = function(*args, **kwargs) commit_time = float(runing_state.timestamp) time_spent = time.time() - commit_time name = f'{function.__module__}.{function.__qualname__}' reconcile_time.labels( name=name, integration=runing_state.integration).observe(amount=time_spent) return result
def wrapper(*args, **kwargs): result = function(*args, **kwargs) msg = result[:-1] if isinstance(result, (list, tuple)) else result if not isinstance(msg, OCProcessReconcileTimeDecoratorMsg): return result running_state = RunningState() commit_time = float(running_state.timestamp) time_spent = time.time() - commit_time try: resource_kind = msg.resource['kind'] resource_name = msg.resource['metadata']['name'] annotations = \ msg.resource['metadata'].get('annotations', {}) except KeyError as e: logging.warning(f'Error processing metric: {e}') return result function_name = f'{function.__module__}.{function.__qualname__}' ignore_reconcile_time = \ annotations.get('qontract.ignore_reconcile_time') == 'true' if not ignore_reconcile_time: reconcile_time.labels( name=function_name, integration=running_state.integration).observe( amount=time_spent) if not msg.is_log_slow_oc_reconcile: return result if time_spent > msg.slow_oc_reconcile_threshold: log_msg = f'Action {function_name} for {resource_kind} ' \ f'{resource_name} in namespace ' \ f'{msg.namespace} from ' \ f'{msg.server} took {time_spent} to ' \ f'reconcile. Commit sha {running_state.commit} ' \ f'and commit ts {running_state.timestamp}.' if ignore_reconcile_time: log_msg += ' Ignored in the metric published.' logging.info(log_msg) return result