-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
336 lines (289 loc) · 16.5 KB
/
lambda_function.py
File metadata and controls
336 lines (289 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import json
import os
import random
import time
import traceback
import boto3
import cv2
import base64
import uuid
from urllib.parse import unquote_plus
from openai import OpenAI
# ── X-Ray setup ────────────────────────────────────────────────────────────
# patch(['boto3']) must be called BEFORE any boto3 client is created so that
# every subsequent client call is automatically traced as a subsegment.
from aws_xray_sdk.core import xray_recorder, patch
patch(['boto3'])
xray_recorder.configure(
service='video-analyzer-worker',
# LOG_ERROR instead of raising so the function keeps running even when
# the X-Ray daemon is unreachable (e.g. local testing).
context_missing='LOG_ERROR',
)
# ── Chaos Engineering hooks ────────────────────────────────────────────────
# Controlled exclusively via Lambda env vars set by chaos_test.py.
# Both vars are absent in normal operation — zero runtime cost.
_CHAOS_FAILURE_RATE = float(os.environ.get('CHAOS_FAILURE_RATE', '0')) # 0.0–1.0
_CHAOS_OPENAI_SLEEP = int(os.environ.get('CHAOS_OPENAI_TIMEOUT_SECS', '0')) # seconds
# ── AWS / OpenAI clients (created after patching) ─────────────────────────
s3_client = boto3.client('s3')
openai_api_key = os.environ.get('OPENAI_API_KEY')
openai_client = OpenAI(api_key=openai_api_key, timeout=800.0) if openai_api_key else None
def process_video_and_analyze(video_path, context_text=""):
"""
Extracts frames from the video and sends them to OpenAI for analysis.
Returns the analysis JSON.
X-Ray subsegments:
• Frame Extraction — CPU-bound OpenCV work
• OpenAI Analysis — external HTTP call (not auto-patched)
"""
if not openai_client:
raise ValueError("OpenAI API Key is missing.")
# ── Subsegment: Frame Extraction ───────────────────────────────────────
base64Frames = []
subsegment = xray_recorder.begin_subsegment('Frame Extraction')
try:
print(f"Extracting frames from {video_path}...", flush=True)
video = cv2.VideoCapture(video_path)
fps = video.get(cv2.CAP_PROP_FPS)
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
duration = total_frames / fps if fps > 0 else 0
print(f"Video Info: FPS={fps}, Total Frames={total_frames}, Duration={duration}s", flush=True)
frame_interval = int(fps) if fps > 0 else 30
frame_count = 0
while video.isOpened():
success, frame = video.read()
if not success:
break
if frame_count % frame_interval == 0:
_, buffer = cv2.imencode(".jpg", frame)
base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
frame_count += 1
video.release()
if len(base64Frames) > 20:
print(f"Resampling frames from {len(base64Frames)} to 20...", flush=True)
step = len(base64Frames) / 20
base64Frames = [base64Frames[int(i * step)] for i in range(20)]
print(f"Extracted {len(base64Frames)} frames.", flush=True)
subsegment.put_annotation('frame_count', len(base64Frames))
subsegment.put_metadata('fps', fps, namespace='video')
subsegment.put_metadata('total_frames', total_frames, namespace='video')
subsegment.put_metadata('duration_secs', duration, namespace='video')
except Exception as e:
subsegment.add_exception(e, traceback.extract_stack())
raise
finally:
xray_recorder.end_subsegment()
if not base64Frames:
raise ValueError("No frames extracted from video.")
# Build prompt
context_instruction = ""
if context_text:
try:
ctx_json = json.loads(context_text)
history = ctx_json.get('history', '')
if history:
context_instruction = (
f"USER CONTEXT / CHAT HISTORY:\n{history}\n\n"
"INSTRUCTION: Incorporate the user's focus areas from the context above into your analysis."
)
except Exception:
context_instruction = f"USER CONTEXT: {context_text}"
PROMPT_MESSAGES = [
{
"role": "user",
"content": [
{
"type": "text",
"text": (
f"TASK: Technical Video Analysis.\n{context_instruction}\n\n"
"OBJECTIVE: Evaluate the technical quality of this video feed across four metrics:\n"
"1. Lighting Uniformity: Identify shadow areas or overexposed glare.\n"
"2. Focus Sharpness: Evaluate if edges are crisp or if there is motion blur.\n"
"3. Frame Composition: Assess the alignment and positioning of the primary subject.\n"
"4. Color Accuracy: Detect white balance shifts or unnatural tinting.\n\n"
"CONSTRAINT: Only analyze the physical environment and camera properties. Ignore biological subjects.\n\n"
"OUTPUT: Provide a detailed JSON object with a numeric score (0-100), a concise transcript of "
"findings, specific feedback, key strengths, and areas for improvement."
),
},
*[
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{frame}"}}
for frame in base64Frames
],
],
},
]
# ── Subsegment: OpenAI Analysis ────────────────────────────────────────
# The OpenAI SDK is not auto-patched by X-Ray, so we wrap it manually.
subsegment = xray_recorder.begin_subsegment('OpenAI Analysis')
try:
subsegment.put_annotation('model', 'gpt-4o-mini')
subsegment.put_annotation('frame_count', len(base64Frames))
subsegment.put_metadata('context_injected', bool(context_instruction), namespace='openai')
# ── Chaos: simulate OpenAI timeout ─────────────────────────────────
if _CHAOS_OPENAI_SLEEP > 0:
print(f"[CHAOS] Sleeping {_CHAOS_OPENAI_SLEEP}s to simulate OpenAI timeout", flush=True)
subsegment.put_annotation('chaos_openai_timeout', True)
time.sleep(_CHAOS_OPENAI_SLEEP)
print("Sending request to OpenAI...", flush=True)
response = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=PROMPT_MESSAGES,
max_tokens=1000,
response_format={"type": "json_object"},
)
content = response.choices[0].message.content
print(f"OpenAI Response: {content}", flush=True)
# Record token usage so it's visible in the X-Ray trace
if response.usage:
subsegment.put_metadata('prompt_tokens', response.usage.prompt_tokens, namespace='openai')
subsegment.put_metadata('completion_tokens', response.usage.completion_tokens, namespace='openai')
subsegment.put_metadata('total_tokens', response.usage.total_tokens, namespace='openai')
content = content.replace("```json", "").replace("```", "").strip()
return json.loads(content)
except Exception as e:
print(f"OpenAI API Error/Refusal: {str(e)}", flush=True)
subsegment.add_exception(e, traceback.extract_stack())
raise
finally:
xray_recorder.end_subsegment()
def handler(event, context):
print(">>> [LAMBDA] Worker Lambda Triggered", flush=True)
print("Received event:", json.dumps(event), flush=True)
# ── SQS Trigger (Processor) ────────────────────────────────────────────
if 'Records' in event:
print(f"Processing {len(event['Records'])} SQS records...", flush=True)
for record in event['Records']:
try:
# ── Chaos: random failure injection ────────────────────────
if _CHAOS_FAILURE_RATE > 0 and random.random() < _CHAOS_FAILURE_RATE:
raise RuntimeError(
f"[CHAOS] Simulated Lambda failure "
f"(rate={_CHAOS_FAILURE_RATE}, record={record.get('messageId', '?')})"
)
sqs_body = json.loads(record['body'])
print(f"SQS Body: {json.dumps(sqs_body)}", flush=True)
if 'Records' not in sqs_body:
continue
for s3_record in sqs_body['Records']:
s3_bucket = s3_record['s3']['bucket']['name']
s3_key = unquote_plus(s3_record['s3']['object']['key'])
print(f">>> [S3/SQS] Triggered by object: s3://{s3_bucket}/{s3_key}", flush=True)
# Derive IDs early so we can annotate all subsegments
parts = s3_key.split('/')
user_id = parts[0] if len(parts) > 1 else "unknown"
video_id = parts[1] if len(parts) > 1 else s3_key
# Annotate the root Lambda segment (searchable in X-Ray console)
try:
segment = xray_recorder.current_segment()
segment.put_annotation('user_id', user_id)
segment.put_annotation('video_id', video_id)
segment.put_annotation('s3_bucket', s3_bucket)
except Exception:
pass # tracing not available (local/test)
# ── Subsegment: S3 Metadata Fetch ──────────────────────
# boto3 is patched so the head_object call itself is traced
# automatically; this wrapper adds searchable metadata.
context_str = ""
subsegment = xray_recorder.begin_subsegment('S3 Metadata Fetch')
try:
subsegment.put_metadata('s3_key', s3_key, namespace='s3')
head_obj = s3_client.head_object(Bucket=s3_bucket, Key=s3_key)
metadata = head_obj.get('Metadata', {})
raw_context = metadata.get('context', '')
if raw_context:
try:
context_str = base64.b64decode(raw_context).decode('utf-8')
except Exception:
context_str = raw_context
subsegment.put_annotation('has_context', bool(context_str))
print(f"Context found: {context_str}", flush=True)
except Exception as meta_err:
print(f"Failed to fetch metadata: {meta_err}", flush=True)
subsegment.add_exception(meta_err, traceback.extract_stack())
finally:
xray_recorder.end_subsegment()
# ── Subsegment: S3 Download ────────────────────────────
download_path = f"/tmp/{uuid.uuid4()}.mp4"
subsegment = xray_recorder.begin_subsegment('S3 Download')
try:
subsegment.put_metadata('s3_key', s3_key, namespace='s3')
subsegment.put_metadata('download_path', download_path, namespace='s3')
print(f"Downloading to {download_path}...", flush=True)
s3_client.download_file(s3_bucket, s3_key, download_path)
file_size = os.path.getsize(download_path)
subsegment.put_annotation('file_size_bytes', file_size)
except Exception as dl_err:
subsegment.add_exception(dl_err, traceback.extract_stack())
raise
finally:
xray_recorder.end_subsegment()
# ── Analysis (contains its own Frame Extraction + OpenAI subsegments)
analysis_failed = False
try:
analysis_result = process_video_and_analyze(download_path, context_str)
except Exception as analysis_err:
print(f"Analysis failed: {analysis_err}", flush=True)
analysis_failed = True
analysis_result = {
'score': 0,
'transcript': 'Analysis failed',
'feedback': str(analysis_err),
}
# Cleanup temp file
if os.path.exists(download_path):
os.remove(download_path)
result_status = 'failed' if analysis_failed else 'completed'
# Annotate root segment with outcome (visible in X-Ray search)
try:
segment = xray_recorder.current_segment()
segment.put_annotation('result_status', result_status)
if not analysis_failed:
segment.put_annotation('analysis_score', analysis_result.get('score', 0))
except Exception:
pass
# ── Subsegment: S3 Result Write ────────────────────────
results_bucket = os.environ.get('RESULTS_BUCKET_NAME')
if results_bucket:
result_key = f"{user_id}/{video_id}.json"
analysis_data = {
'userId': user_id,
'videoId': video_id,
'status': result_status,
's3_uri': f"s3://{s3_bucket}/{s3_key}",
'analysis': analysis_result,
'timestamp': s3_record['eventTime'],
}
subsegment = xray_recorder.begin_subsegment('S3 Result Write')
try:
subsegment.put_annotation('user_id', user_id)
subsegment.put_annotation('result_status', result_status)
subsegment.put_metadata('result_key', result_key, namespace='s3')
subsegment.put_metadata('results_bucket', results_bucket, namespace='s3')
s3_client.put_object(
Bucket=results_bucket,
Key=result_key,
Body=json.dumps(analysis_data),
ContentType='application/json',
)
print(f">>> [S3] Successfully wrote analysis to s3://{results_bucket}/{result_key}", flush=True)
except Exception as write_err:
subsegment.add_exception(write_err, traceback.extract_stack())
raise
finally:
xray_recorder.end_subsegment()
else:
print("Error: RESULTS_BUCKET_NAME not set.", flush=True)
except Exception as e:
print(f"Error processing record: {e}", flush=True)
raise
return {'statusCode': 200, 'body': json.dumps('Processing complete')}
# ── API Gateway Proxy Trigger ──────────────────────────────────────────
if 'httpMethod' in event:
print(f">>> [API GATEWAY] Triggered with method: {event['httpMethod']}", flush=True)
return {
'statusCode': 200,
'body': json.dumps({'message': 'API Gateway trigger received', 'event_received': True}),
}
return {'statusCode': 400, 'body': json.dumps('Unknown event source')}