@@ -339,33 +339,8 @@ When using Tracer to capture responses for each batch record processing, you mig
339
339
340
340
If that's the case, you can configure [ Tracer to disable response auto-capturing] ( ../core/tracer.md#disabling-response-auto-capture ) {target="_ blank"}.
341
341
342
- ``` python hl_lines="14" title="Disabling Tracer response auto-capturing"
343
- import json
344
-
345
- from aws_lambda_powertools import Logger, Tracer
346
- from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, batch_processor
347
- from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
348
- from aws_lambda_powertools.utilities.typing import LambdaContext
349
-
350
-
351
- processor = BatchProcessor(event_type = EventType.SQS )
352
- tracer = Tracer()
353
- logger = Logger()
354
-
355
-
356
- @tracer.capture_method (capture_response = False )
357
- def record_handler (record : SQSRecord):
358
- payload: str = record.body
359
- if payload:
360
- item: dict = json.loads(payload)
361
- ...
362
-
363
- @logger.inject_lambda_context
364
- @tracer.capture_lambda_handler
365
- @batch_processor (record_handler = record_handler, processor = processor)
366
- def lambda_handler (event , context : LambdaContext):
367
- return processor.response()
368
-
342
+ ``` python hl_lines="17" title="Disabling Tracer response auto-capturing"
343
+ -- 8 < -- " examples/batch_processing/src/disable_tracing.py"
369
344
```
370
345
371
346
## Testing your code
@@ -379,127 +354,19 @@ Given a SQS batch where the first batch record succeeds and the second fails pro
379
354
=== "test_app.py"
380
355
381
356
```python
382
- import json
383
-
384
- from pathlib import Path
385
- from dataclasses import dataclass
386
-
387
- import pytest
388
- from src.app import lambda_handler, processor
389
-
390
-
391
- def load_event(path: Path):
392
- with path.open() as f:
393
- return json.load(f)
394
-
395
-
396
- @pytest.fixture
397
- def lambda_context():
398
- @dataclass
399
- class LambdaContext:
400
- function_name: str = "test"
401
- memory_limit_in_mb: int = 128
402
- invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
403
- aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
404
-
405
- return LambdaContext()
406
-
407
- @pytest.fixture()
408
- def sqs_event():
409
- """Generates API GW Event"""
410
- return load_event(path=Path("events/sqs_event.json"))
411
-
412
-
413
- def test_app_batch_partial_response(sqs_event, lambda_context):
414
- # GIVEN
415
- processor = app.processor # access processor for additional assertions
416
- successful_record = sqs_event["Records"][0]
417
- failed_record = sqs_event["Records"][1]
418
- expected_response = {
419
- "batchItemFailures: [
420
- {
421
- "itemIdentifier": failed_record["messageId"]
422
- }
423
- ]
424
- }
425
-
426
- # WHEN
427
- ret = app.lambda_handler(sqs_event, lambda_context)
428
-
429
- # THEN
430
- assert ret == expected_response
431
- assert len(processor.fail_messages) == 1
432
- assert processor.success_messages[0] == successful_record
357
+ --8<-- "examples/batch_processing/testing/test_app.py"
433
358
```
434
359
435
360
=== "src/app.py"
436
361
437
362
```python
438
- import json
439
-
440
- from aws_lambda_powertools import Logger, Tracer
441
- from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response
442
- from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
443
- from aws_lambda_powertools.utilities.typing import LambdaContext
444
-
445
-
446
- processor = BatchProcessor(event_type=EventType.SQS)
447
- tracer = Tracer()
448
- logger = Logger()
449
-
450
-
451
- @tracer.capture_method
452
- def record_handler(record: SQSRecord):
453
- payload: str = record.body
454
- if payload:
455
- item: dict = json.loads(payload)
456
- ...
457
-
458
- @logger.inject_lambda_context
459
- @tracer.capture_lambda_handler
460
- def lambda_handler(event, context: LambdaContext):
461
- return process_partial_response(event=event, record_handler=record_handler, processor=processor, context=context)
363
+ --8<-- "examples/batch_processing/testing/src/app.py"
462
364
```
463
365
464
366
=== "Sample SQS event"
465
367
466
- ```json title="events/sqs_sample.json"
467
- {
468
- "Records": [
469
- {
470
- "messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
471
- "receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a",
472
- "body": "{\"Message\": \"success\"}",
473
- "attributes": {
474
- "ApproximateReceiveCount": "1",
475
- "SentTimestamp": "1545082649183",
476
- "SenderId": "AIDAIENQZJOLO23YVJ4VO",
477
- "ApproximateFirstReceiveTimestamp": "1545082649185"
478
- },
479
- "messageAttributes": {},
480
- "md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
481
- "eventSource": "aws:sqs",
482
- "eventSourceARN": "arn:aws:sqs:us-east-2: 123456789012:my-queue",
483
- "awsRegion": "us-east-1"
484
- },
485
- {
486
- "messageId": "244fc6b4-87a3-44ab-83d2-361172410c3a",
487
- "receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a",
488
- "body": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==",
489
- "attributes": {
490
- "ApproximateReceiveCount": "1",
491
- "SentTimestamp": "1545082649183",
492
- "SenderId": "AIDAIENQZJOLO23YVJ4VO",
493
- "ApproximateFirstReceiveTimestamp": "1545082649185"
494
- },
495
- "messageAttributes": {},
496
- "md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
497
- "eventSource": "aws:sqs",
498
- "eventSourceARN": "arn:aws:sqs:us-east-2: 123456789012:my-queue",
499
- "awsRegion": "us-east-1"
500
- }
501
- ]
502
- }
368
+ ```json title="events/sqs_event.json"
369
+ --8<-- "examples/batch_processing/testing/events/sqs_event.json"
503
370
```
504
371
505
372
## FAQ
0 commit comments