import asyncio import json import sys from app import generate_snapshot def lambda_handler(event, context): if context.get_remaining_time_in_millis() < 60000: raise ValueError("the lambda function's timeout property should be configured at least 60s") try: result = asyncio.run( asyncio.wait_for( generate_snapshot(event), context.get_remaining_time_in_millis() / 1000 - 10, ) ) response = json.dumps(result, default=float) if sys.getsizeof(response) >= 4 * 1024 * 1024: raise OverflowError("The response must be smaller than 4MB!") return json.loads(response) except asyncio.TimeoutError as e: print("task timeout, suspending...") print("suspend method not implemented!") raise (e) except Exception as e: raise (e) if __name__ == "__main__": import time import pandas as pd time0 = time.time() # oss-insight # cluster = "1379661944623214066" # org_id = "1372813089187041280" # CC_A1 cluster = "1311285575352848384" org_id = "1311265642040528896" # end = int(time.time()) end = 1660533579 # 2022-08-15 11:19:39 start = end - 3600 org_type = "cloud" # cluster = "1379661944623214066" # start = 1650591648 # end = 1650592648 # org_type = "cloud" # org_id = "1372813089196911678" event = { "task_id": "914d3aa2-7b21-4ee3-bdb8-6b0f5203a84c", "tags": {"source": "83613d40-d446-461f-9920-d2a12f6b7268"}, # predecessor task id "kwargs": { "cluster": cluster, "start_ts": start, "stop_ts": end, "org_id": org_id, "org_type": org_type, "extra_promqls": { "from": "payload", "object": [ { "name": "TiDB_cpu_usage_norm:by_instance", "unit": "%", "expr": 'label_replace(rate(process_cpu_seconds_total{{label_cluster_id},{label_component}="tidb"}[2m])/tidb_server_maxprocs{{label_cluster_id},{label_component}="tidb"}*100,"name","$1","instance","(.*)")', "rawdata": True, "operators": ["check_continuous_high(0.8,5)"], }, { "name": "TiDB_cpu_usage_norm:total", "unit": "", "expr": "", "component": "", "rawdata": False, "operators": ["check_continuous_high(0.8,10)"], }, ], }, }, } try: result = asyncio.run(asyncio.wait_for(generate_snapshot(event), 100000)) # print({ # "code": 200, # "status": "task finished", # "message": result, # }) response = json.dumps(result, default=float) print(response) except asyncio.TimeoutError: print("task timeout and cannot be suspend") print( { "code": 500, "status": "task timeout", "message": "task timeout", } ) print(f"cost time {time.time()-time0} s")