"""Lambda osr-fingerprint-filter function module.""" import os from concurrent.futures import as_completed from concurrent.futures import ThreadPoolExecutor from .common import logger import sentry_sdk from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration import config from src.utils.events import filter_events from src.utils.events import generate_outputs from src.utils.records import decode_records from src.utils.sfn_handler import throttle from src.utils.sfn_handler import execute_sfn from src.common.connectors.sfn import sfn_client # initialize sentry sentry_dsn = os.environ.get( 'SENTRY_DSN', config.secrets_manager_client.get_cred('SENTRY_DSN')) if sentry_dsn: logger.info('Initializing with sentry') sentry_sdk.init( sentry_dsn, integrations=[AwsLambdaIntegration()] ) else: logger.info('Initializing without sentry') def handler(event, context): """Lambda entry point.""" try: logger.info(event) all_events = decode_records(event) filtered_events = filter_events(all_events) output_events = generate_outputs(filtered_events) exc_names: dict[str, list[str]] = { 'skipped': [], 'started': [] } throttle(sfn_client, len(output_events)) with ThreadPoolExecutor(max_workers=20) as executor: futures = [executor.submit(execute_sfn, sfn_client, evt) for evt in output_events] for future in as_completed(futures): (exc_name, started) = future.result() if started: exc_names['started'].append(exc_name) else: exc_names['skipped'].append(exc_name) summary = { 'num_input_records': len(all_events), 'sfn_executions': exc_names } logger.info(summary) return summary except Exception as e: logger.exception(str(e)) raise e