1
+ import { assertIsError } from '#helpers/utils' ;
1
2
import { logger } from '#powertools/logger' ;
3
+ import { tracer } from '#powertools/tracer' ;
2
4
import {
3
5
BatchProcessor ,
4
6
EventType ,
5
7
processPartialResponse ,
6
8
} from '@aws-lambda-powertools/batch' ;
9
+ import type { AttributeValue } from '@aws-sdk/client-dynamodb' ;
10
+ import { unmarshall } from '@aws-sdk/util-dynamodb' ;
7
11
import type {
8
12
Context ,
9
13
DynamoDBBatchResponse ,
@@ -23,37 +27,46 @@ const processor = new BatchProcessor(EventType.DynamoDBStreams);
23
27
* @param record The DynamoDB record to process
24
28
*/
25
29
const recordHandler = async ( record : DynamoDBRecord ) : Promise < void > => {
30
+ logger . debug ( 'processing record' , { record } ) ;
31
+ const subsegment = tracer
32
+ . getSegment ( )
33
+ ?. addNewSubsegment ( '#### recordHandler' ) ;
26
34
try {
27
35
if ( record . dynamodb && record . dynamodb . NewImage ) {
28
- const message = record . dynamodb . NewImage . Message ?. S ;
29
- if ( message ) {
30
- const payload = JSON . parse ( message ) as { id : string ; name : string } ;
31
- // Add itemId to the logger so that it's included in every log message
32
- logger . appendKeys ( { itemId : payload . id } ) ;
36
+ const payload = unmarshall (
37
+ record . dynamodb . NewImage as { [ key : string ] : AttributeValue }
38
+ ) ;
33
39
34
- const query = new URLSearchParams ( ) ;
35
- query . set ( 'name' , payload . name ) ;
40
+ // Add itemId to the logger so that it's included in every log message
41
+ logger . appendKeys ( { itemId : payload . id } ) ;
42
+ // Also add it to the subsegment, so you can search for traces by itemId
43
+ subsegment ?. addAnnotation ( 'itemId' , payload . id ) ;
36
44
37
- const remoteUrl = `https://httpbin.org/anything? ${ query . toString ( ) } ` ;
38
- logger . debug ( 'sending request ', { remoteUrl } ) ;
45
+ const query = new URLSearchParams ( ) ;
46
+ query . set ( 'name ', payload . name ) ;
39
47
40
- // This request doesn't show up in the trace yet, see #1619 for updates
41
- const response = await fetch ( remoteUrl ) ;
42
- // If the request was successful, the response.ok property will be true
43
- equal ( response . ok , true ) ;
48
+ const remoteUrl = `https://httpbin.org/anything?${ query . toString ( ) } ` ;
49
+ logger . debug ( 'sending request' , { remoteUrl } ) ;
44
50
45
- logger . debug ( 'request completed' , {
46
- response : await response . json ( ) ,
47
- status : response . status ,
48
- } ) ;
49
- }
51
+ // This request doesn't show up in the trace yet, see #1619 for updates
52
+ const response = await fetch ( remoteUrl ) ;
53
+ // If the request was successful, the response.ok property will be true
54
+ equal ( response . ok , true ) ;
55
+
56
+ logger . debug ( 'request completed' , {
57
+ response : await response . json ( ) ,
58
+ status : response . status ,
59
+ } ) ;
50
60
}
51
61
} catch ( error ) {
52
- logger . error ( 'error processing record' , { error } ) ;
62
+ assertIsError ( error ) ;
63
+ logger . error ( 'error processing record' , error ) ;
64
+ subsegment ?. addError ( error , true ) ;
53
65
54
66
throw error ;
55
67
} finally {
56
68
logger . removeKeys ( [ 'itemId' ] ) ;
69
+ subsegment ?. close ( ) ;
57
70
}
58
71
} ;
59
72
@@ -62,10 +75,24 @@ const recordHandler = async (record: DynamoDBRecord): Promise<void> => {
62
75
*
63
76
* The functions uses the `processPartialResponse` function from the `@aws-lambda-powertools/batch` package
64
77
* to process the records in parallel using the `recordHandler` function.
78
+ *
79
+ * The `processPartialResponse` is wrapped in a `tracer.provider.captureAsyncFunc` to ensure that the whole
80
+ * process is captured in a single trace.
65
81
*/
66
82
export const handler = async (
67
83
event : DynamoDBStreamEvent ,
68
84
context : Context
69
85
) : Promise < DynamoDBBatchResponse > => {
70
- return processPartialResponse ( event , recordHandler , processor , { context } ) ;
86
+ return tracer . provider . captureAsyncFunc ( '### handler' , async ( segment ) => {
87
+ const result = await processPartialResponse (
88
+ event ,
89
+ recordHandler ,
90
+ processor ,
91
+ { context }
92
+ ) ;
93
+
94
+ segment ?. close ( ) ;
95
+
96
+ return result ;
97
+ } ) as DynamoDBBatchResponse ;
71
98
} ;
0 commit comments