Skip to content
Open
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
66 changes: 57 additions & 9 deletions lambda-petclinic/sample-apps/function2/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@
table = dynamodb.Table(table_name)

def lambda_handler(event, context):
query_params = event.get('queryStringParameters', {})
# Add null check for event to prevent errors
if event is None:
return {
'statusCode': 400,
'body': json.dumps({'error': 'Invalid request: event is null'}),
'headers': {
'Content-Type': 'application/json'
}
}

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,26 +28,64 @@ def lambda_handler(event, context):
pet_id = query_params.get('petid')

try:
response = table.scan()
# Optimize DynamoDB access - use Query instead of Scan for better performance
# For demo purposes, we'll use a more efficient approach
# If we had a GSI on owners/pet_id, we could query specifically
# For now, we'll limit the scan and add pagination

limit = int(query_params.get('limit', 50)) # Default limit of 50 items
last_evaluated_key = query_params.get('lastKey')

scan_kwargs = {
'Limit': limit,
'Select': 'ALL_ATTRIBUTES'
}

# Add pagination support
if last_evaluated_key:
try:
scan_kwargs['ExclusiveStartKey'] = json.loads(last_evaluated_key)
except (json.JSONDecodeError, TypeError):
# Invalid lastKey, ignore and continue without pagination
pass

# Use scan with limit instead of full table scan for better performance
response = table.scan(**scan_kwargs)
items = response.get('Items', [])

print("Record IDs in DynamoDB Table:")
print(f"Retrieved {len(items)} records from DynamoDB Table")
for item in items:
print(item['recordId'])
if 'recordId' in item:
print(item['recordId'])

record_ids = [record['recordId'] for record in items if 'recordId' in record]

# Prepare response with pagination info
response_body = {
'recordIds': record_ids,
'count': len(record_ids)
}

# Add pagination info if there are more items
if 'LastEvaluatedKey' in response:
response_body['lastKey'] = json.dumps(response['LastEvaluatedKey'])
response_body['hasMore'] = True
else:
response_body['hasMore'] = False

return {
'statusCode': 200,
'body': json.dumps({
'recordIds': record_ids
}),
'body': json.dumps(response_body),
'headers': {
'Content-Type': 'application/json'
}
}
except Exception as e:
print(f"Error listing records: {str(e)}")
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}
'body': json.dumps({'error': 'Internal server error'}),
'headers': {
'Content-Type': 'application/json'
}
}
23 changes: 21 additions & 2 deletions lambda-petclinic/sample-apps/function3/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,38 @@
table = dynamodb.Table(table_name)

def lambda_handler(event, context):
# Add null check for event to prevent NullPointerException
if event is None:
return {
'statusCode': 400,
'body': json.dumps({'message': 'Invalid request: event is null'}),
'headers': {
'Content-Type': 'application/json'
}
}

query_params = event.get('queryStringParameters', {})
# Add null check for queryStringParameters to prevent line 27 error
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
current_span.set_attribute("owner.id", owner_id)

# Add null checks for required parameters
record_id = query_params.get('recordId')
owners = query_params.get('owners')
pet_id = query_params.get('petid')

# Validate required parameters with proper error handling
if owners is None or pet_id is None:
raise Exception('Missing owner or pet_id')
return {
'statusCode': 400,
'body': json.dumps({'message': 'Missing required parameters: owners and petid are required'}),
'headers': {
'Content-Type': 'application/json'
}
}

if record_id is None:
return {
Expand Down