Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions lambda-petclinic/sample-apps/function2/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'])

Expand All @@ -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'
}
}
23 changes: 15 additions & 8 deletions lambda-petclinic/sample-apps/function3/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,21 +20,28 @@ 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'
}
}

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:
Expand All @@ -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'
}
Expand All @@ -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'
}
Expand Down