""" Hawkeye - ElasticSearch Data Integrity Monitor @author(s): Abdullah S. Adam """ # lambda_handler.py import os from hawkeye_monitor.integrations.datadog import send_alert_to_datadog from hawkeye_monitor.monitor import DataIntegrityMonitor from hawkeye_monitor.checklist import get_checklist from hawkeye_monitor.utils import get_secret_value from hawkeye_monitor.config import ( SILENCE_PASSED_CHECKS, DATADOG_API_KEY, ELASTICSEARCH_CREDS, ELASTICSEARCH_INDEX, ) from datadog import initialize, api from absl import logging logging.set_verbosity(logging.INFO) # intialisse data dog initialize(**{"api_key": DATADOG_API_KEY}) def lambda_handler_fn(event, context): """Runs checks using Elasticsearch Data Integrity monitor""" # fetch enviornment env = event.get("env", None) logging.info(f"ENV: {env}") if env is None: raise Exception("Expected environment variable 'env' as either 'QA' or 'PROD'") # get the index and creds index = ELASTICSEARCH_INDEX creds = ELASTICSEARCH_CREDS logging.info(f"Running Checks against index: {index}") # initialise data ingestor es_monitor = DataIntegrityMonitor(**creds) # get checklist logging.info("Fetching checklist") checklist = get_checklist() # Running the monitor logging.info("Running checks") passed, failed = es_monitor.run_checks(index, checklist) # alert these checks datadog_lst = failed # add passed checks to alerts if SILENCE_PASSED_CHECKS and len(passed) > 0: datadog_lst += passed logging.info(f"Passed checks: {len(passed)}") logging.info(f"Failed checks: {len(failed)}") # add failed checks for check in datadog_lst: send_alert_to_datadog( api=api, check_name=check.check_name, passed=check.check_passed, kwargs=check.kwargs, ) return { "statusCode": 200, "body": {"passed_checks": len(passed), "failed_checks": len(failed)}, }