@@ -3,10 +3,9 @@ import { setTimeout as delay } from 'node:timers/promises';
33import { describe , expect , it } from 'vitest' ;
44import { DASH0_ENDPOINT , DASH0_TOKEN , MAX_ATTEMPTS , RETRY_DELAY_MS } from "./config" ;
55import {
6- checkHttpSpan ,
76 checkLogs ,
87 checkResourceAttributes ,
9- checkSpanAttributesFromReport ,
8+ checkSupplementarySpans ,
109 getAttributesMap , LogToCheck ,
1110 getRequestPayload ,
1211 invokeFunction , runAllTests
@@ -18,6 +17,7 @@ const verifySuccessInvocation = async (functionName: string, invocationEnd: bool
1817
1918 let traceId : string | undefined = undefined ;
2019 let parentSpanId : string | undefined = undefined ;
20+ let rootSpanId : string | undefined = undefined ;
2121 let span = undefined
2222 for ( let attempt = 1 ; attempt <= MAX_ATTEMPTS ; attempt ++ ) {
2323 await delay ( RETRY_DELAY_MS ) ;
@@ -34,21 +34,30 @@ const verifySuccessInvocation = async (functionName: string, invocationEnd: bool
3434 } ) ;
3535
3636 const spanPayload = await spanResponse . json ( ) as any ;
37- expect ( spanPayload ?. resourceSpans . length ) . toEqual ( 1 ) ;
38- expect ( spanPayload ?. resourceSpans [ 0 ] . scopeSpans . length ) . toEqual ( 1 ) ;
37+ // Find the Lambda instrumentation scope (supplementary spans may also be present)
3938 const expectedScopeName = traced ? "io.opentelemetry.aws-lambda-events-2.2" : "opentelemetry.instrumentation.aws_lambda" ;
40- expect ( spanPayload ?. resourceSpans [ 0 ] . scopeSpans [ 0 ] . scope . name ) . toEqual ( expectedScopeName ) ;
41- expect ( spanPayload ?. resourceSpans [ 0 ] . scopeSpans [ 0 ] . spans . length ) . toEqual ( 1 ) ;
42- const resourceAttributes = getAttributesMap ( spanPayload ?. resourceSpans [ 0 ] . resource . attributes ) ;
39+ let lambdaScopeSpan = null ;
40+ let lambdaResource = null ;
41+ for ( const rs of ( spanPayload ?. resourceSpans ?? [ ] ) ) {
42+ for ( const ss of ( rs . scopeSpans ?? [ ] ) ) {
43+ if ( ss . scope ?. name === expectedScopeName ) {
44+ lambdaScopeSpan = ss ;
45+ lambdaResource = rs . resource ;
46+ }
47+ }
48+ }
49+ expect ( lambdaScopeSpan ) . toBeDefined ( ) ;
50+ expect ( lambdaScopeSpan ! . spans . length ) . toEqual ( 1 ) ;
51+ const resourceAttributes = getAttributesMap ( lambdaResource ! . attributes ) ;
4352 expect ( resourceAttributes [ 'service.name' ] . stringValue ) . toEqual ( functionName ) ;
44- checkResourceAttributes ( spanPayload . resourceSpans [ 0 ] . resource . attributes , functionName ) ;
53+ checkResourceAttributes ( lambdaResource ! . attributes , functionName ) ;
4554 // check span attributes
46- span = spanPayload . resourceSpans [ 0 ] . scopeSpans [ 0 ] . spans [ 0 ] ;
55+ span = lambdaScopeSpan ! . spans [ 0 ] ;
4756 const spanAttributes = getAttributesMap ( span . attributes ) ;
4857 expect ( spanAttributes [ 'faas.invocation_id' ] . stringValue ) . toEqual ( invocationId ) ;
49- expect ( spanAttributes [ 'faas.init_duration' ] . doubleValue ) . toBeGreaterThan ( 0 ) ;
5058 traceId = span . traceId ;
5159 parentSpanId = span . spanId ;
60+ rootSpanId = span . parentSpanId ;
5261 break ;
5362 } catch ( error ) {
5463 console . error ( `Error fetching spans on attempt ${ attempt } :` , error ) ;
@@ -66,17 +75,25 @@ const verifySuccessInvocation = async (functionName: string, invocationEnd: bool
6675 if ( ! invocationEnd ) {
6776 logsToBeChecked . push ( { message : 'REPORT RequestId: ' } ) ;
6877 }
69- const reportLog = await checkLogs ( {
78+ await checkLogs ( {
7079 invocationId : invocationId ! ,
7180 functionName,
7281 traceId : traceId ! ,
7382 parentSpanId : parentSpanId ! ,
7483 success : true ,
7584 logsToBeChecked
7685 } ) ;
77- if ( ! invocationEnd ) {
78- checkSpanAttributesFromReport ( reportLog , span ) ;
86+
87+ // Supplementary spans are sent on the next invocation; trigger one if needed
88+ if ( invocationEnd ) {
89+ await invokeFunction ( functionName , true , false ) ;
7990 }
91+ await checkSupplementarySpans ( {
92+ invocationId : invocationId ! ,
93+ functionName,
94+ traceId : traceId ! ,
95+ rootSpanId : rootSpanId ! ,
96+ } ) ;
8097}
8198
8299describe . concurrent ( 'Lambdainvocation' , ( ) => {
0 commit comments