@@ -72,6 +72,7 @@ describe("ColdStartTracer", () => {
7272 currentSpanStartTime : 500 ,
7373 minDuration : 1 ,
7474 ignoreLibs : "" ,
75+ isColdStart : true ,
7576 } ;
7677 const coldStartTracer = new ColdStartTracer ( coldStartConfig ) ;
7778 coldStartTracer . trace ( requireNodes ) ;
@@ -162,6 +163,7 @@ describe("ColdStartTracer", () => {
162163 currentSpanStartTime : 500 ,
163164 minDuration : 1 ,
164165 ignoreLibs : "myChildModule,myCoreModule" ,
166+ isColdStart : true ,
165167 } ;
166168 const coldStartTracer = new ColdStartTracer ( coldStartConfig ) ;
167169 coldStartTracer . trace ( requireNodes ) ;
@@ -194,4 +196,86 @@ describe("ColdStartTracer", () => {
194196 service : "aws.lambda" ,
195197 } ) ;
196198 } ) ;
199+ it ( "traces lazy imports" , ( ) => {
200+ const requireNodes : RequireNode [ ] = [
201+ {
202+ id : "handler" ,
203+ filename : "/var/task/handler.js" ,
204+ startTime : 1 ,
205+ endTime : 6 ,
206+ children : [
207+ {
208+ id : "myChildModule" ,
209+ filename : "/opt/nodejs/node_modules/my-child-module.js" ,
210+ startTime : 2 ,
211+ endTime : 3 ,
212+ } ,
213+ {
214+ id : "myCoreModule" ,
215+ filename : "http" ,
216+ startTime : 4 ,
217+ endTime : 5 ,
218+ } ,
219+ {
220+ id : "aws-sdk" ,
221+ filename : "/var/runtime/aws-sdk" ,
222+ startTime : 4 ,
223+ endTime : 5 ,
224+ } ,
225+ ] ,
226+ } as any as RequireNode ,
227+ ] ;
228+ const coldStartConfig : ColdStartTracerConfig = {
229+ tracerWrapper : new TracerWrapper ( ) ,
230+ parentSpan : {
231+ span : { } ,
232+ name : "my-parent-span" ,
233+ } as any as SpanWrapper ,
234+ lambdaFunctionName : "my-function-name" ,
235+ currentSpanStartTime : 500 ,
236+ minDuration : 1 ,
237+ ignoreLibs : "" ,
238+ isColdStart : false ,
239+ } ;
240+ const coldStartTracer = new ColdStartTracer ( coldStartConfig ) ;
241+ coldStartTracer . trace ( requireNodes ) ;
242+ expect ( mockStartSpan ) . toHaveBeenCalledTimes ( 4 ) ;
243+ expect ( mockFinishSpan ) . toHaveBeenCalledTimes ( 4 ) ;
244+ const span1 = mockStartSpan . mock . calls [ 0 ] ;
245+ expect ( span1 [ 0 ] ) . toEqual ( "aws.lambda.require" ) ;
246+ expect ( span1 [ 1 ] . tags ) . toEqual ( {
247+ operation_name : "aws.lambda.require" ,
248+ "resource.name" : "handler" ,
249+ resource_names : "handler" ,
250+ service : "aws.lambda" ,
251+ filename : "/var/task/handler.js" ,
252+ } ) ;
253+ const span2 = mockStartSpan . mock . calls [ 1 ] ;
254+ expect ( span2 [ 0 ] ) . toEqual ( "aws.lambda.require_layer" ) ;
255+ expect ( span2 [ 1 ] . tags ) . toEqual ( {
256+ filename : "/opt/nodejs/node_modules/my-child-module.js" ,
257+ operation_name : "aws.lambda.require_layer" ,
258+ "resource.name" : "myChildModule" ,
259+ resource_names : "myChildModule" ,
260+ service : "aws.lambda" ,
261+ } ) ;
262+ const span3 = mockStartSpan . mock . calls [ 2 ] ;
263+ expect ( span3 [ 0 ] ) . toEqual ( "aws.lambda.require_core_module" ) ;
264+ expect ( span3 [ 1 ] . tags ) . toEqual ( {
265+ filename : "http" ,
266+ operation_name : "aws.lambda.require_core_module" ,
267+ "resource.name" : "myCoreModule" ,
268+ resource_names : "myCoreModule" ,
269+ service : "aws.lambda" ,
270+ } ) ;
271+ const span4 = mockStartSpan . mock . calls [ 3 ] ;
272+ expect ( span4 [ 0 ] ) . toEqual ( "aws.lambda.require_runtime" ) ;
273+ expect ( span4 [ 1 ] . tags ) . toEqual ( {
274+ filename : "/var/runtime/aws-sdk" ,
275+ operation_name : "aws.lambda.require_runtime" ,
276+ "resource.name" : "aws-sdk" ,
277+ resource_names : "aws-sdk" ,
278+ service : "aws.lambda" ,
279+ } ) ;
280+ } ) ;
197281} ) ;
0 commit comments