diff --git a/lambda-petclinic/sample-apps/function2/lambda_function.py b/lambda-petclinic/sample-apps/function2/lambda_function.py index d6f237c..cbfc08c 100644 --- a/lambda-petclinic/sample-apps/function2/lambda_function.py +++ b/lambda-petclinic/sample-apps/function2/lambda_function.py @@ -8,7 +8,7 @@ table = dynamodb.Table(table_name) def lambda_handler(event, context): - query_params = event.get('queryStringParameters', {}) + query_params = event.get('queryStringParameters') or {} current_span = trace.get_current_span() # Add an attribute to the current span owner_id = random.randint(1, 9) # Generate a random value between 1 and 9 @@ -18,10 +18,14 @@ def lambda_handler(event, context): pet_id = query_params.get('petid') try: - response = table.scan() + # Optimize: Use pagination to limit scan results and improve performance + response = table.scan( + Limit=100, # Limit results to prevent large scans + ProjectionExpression='recordId' # Only fetch recordId to reduce data transfer + ) items = response.get('Items', []) - print("Record IDs in DynamoDB Table:") + print(f"Retrieved {len(items)} records from DynamoDB") for item in items: print(item['recordId']) @@ -30,14 +34,19 @@ def lambda_handler(event, context): return { 'statusCode': 200, 'body': json.dumps({ - 'recordIds': record_ids + 'recordIds': record_ids, + 'count': len(record_ids) }), 'headers': { 'Content-Type': 'application/json' } } except Exception as e: + print(f"Error scanning table: {str(e)}") return { 'statusCode': 500, - 'body': json.dumps({'error': str(e)}) - } + 'body': json.dumps({'error': 'Failed to retrieve records'}), + 'headers': { + 'Content-Type': 'application/json' + } + } \ No newline at end of file diff --git a/lambda-petclinic/sample-apps/function3/lambda_function.py b/lambda-petclinic/sample-apps/function3/lambda_function.py index b82a9fc..4e03ddb 100644 --- a/lambda-petclinic/sample-apps/function3/lambda_function.py +++ b/lambda-petclinic/sample-apps/function3/lambda_function.py @@ -10,7 +10,7 @@ def lambda_handler(event, context): - query_params = event.get('queryStringParameters', {}) + query_params = event.get('queryStringParameters') or {} current_span = trace.get_current_span() # Add an attribute to the current span owner_id = random.randint(1, 9) # Generate a random value between 1 and 9 @@ -20,13 +20,20 @@ def lambda_handler(event, context): owners = query_params.get('owners') pet_id = query_params.get('petid') - if owners is None or pet_id is None: - raise Exception('Missing owner or pet_id') + # Fix: Return proper 400 error instead of raising exception + if not owners or not pet_id: + return { + 'statusCode': 400, + 'body': json.dumps({'error': 'Missing required parameters: owners and petid'}), + 'headers': { + 'Content-Type': 'application/json' + } + } - if record_id is None: + if not record_id: return { 'statusCode': 400, - 'body': json.dumps({'message': 'recordId is required'}), + 'body': json.dumps({'error': 'recordId is required'}), 'headers': { 'Content-Type': 'application/json' } @@ -34,7 +41,7 @@ def lambda_handler(event, context): try: # Retrieve the item with the specified recordId - response = table.get_item(Key={'recordId': record_id}) # Assuming recordId is the primary key + response = table.get_item(Key={'recordId': record_id}) # Check if the item exists if 'Item' in response: @@ -48,7 +55,7 @@ def lambda_handler(event, context): else: return { 'statusCode': 404, - 'body': json.dumps({'message': 'Record not found'}), + 'body': json.dumps({'error': 'Record not found'}), 'headers': { 'Content-Type': 'application/json' } @@ -58,7 +65,7 @@ def lambda_handler(event, context): print("Error retrieving record:", str(e)) return { 'statusCode': 500, - 'body': json.dumps({'message': 'Internal server error'}), + 'body': json.dumps({'error': 'Internal server error'}), 'headers': { 'Content-Type': 'application/json' }