Skip to content

Commit

Permalink
[Serverless] Reset both inferred spans on each invocation. (#30881)
Browse files Browse the repository at this point in the history
  • Loading branch information
purple4reina authored Nov 8, 2024
1 parent 87e6528 commit 9b6d767
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/serverless/invocationlifecycle/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ func (lp *LifecycleProcessor) newRequest(lambdaPayloadString []byte, startTime t
SpanID: inferredspan.GenerateSpanId(),
},
}
lp.requestHandler.inferredSpans[1] = nil
lp.requestHandler.triggerTags = make(map[string]string)
lp.requestHandler.triggerMetrics = make(map[string]float64)
}
Expand Down
52 changes: 52 additions & 0 deletions pkg/serverless/invocationlifecycle/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,58 @@ func TestTriggerTypesLifecycleEventForSNSSQSNoDdContext(t *testing.T) {
assert.Equal(t, snsSpan.SpanID, sqsSpan.ParentID)
}

func TestTriggerTypesLifecycleEventForSNSSQSThenApiGateway(t *testing.T) {
// SNS-SQS creates two inferred spans. Ensure that then invoking the
// function with an event that should have just one inferred span (API
// Gateway) creates just one inferred span.
var tracePayload *api.Payload
testProcessor := &LifecycleProcessor{
DetectLambdaLibrary: func() bool { return false },
ProcessTrace: func(payload *api.Payload) { tracePayload = payload },
InferredSpansEnabled: true,
}

// SNS-SQS invocation
startInvocationTime := time.Now()
endInvocationTime := startInvocationTime.Add(time.Second)

startDetails := &InvocationStartDetails{
InvokeEventRawPayload: getEventFromFile("snssqs.json"),
InvokedFunctionARN: "arn:aws:lambda:us-east-1:123456789012:function:my-function",
StartTime: startInvocationTime,
}
endDetails := &InvocationEndDetails{
RequestID: "test-request-id",
EndTime: endInvocationTime,
}

testProcessor.OnInvokeStart(startDetails)
testProcessor.OnInvokeEnd(endDetails)

spans := tracePayload.TracerPayload.Chunks[0].Spans
assert.Equal(t, 3, len(spans))

// API Gateway invocation
startInvocationTime = endInvocationTime
endInvocationTime = startInvocationTime.Add(time.Second)

startDetails = &InvocationStartDetails{
InvokeEventRawPayload: getEventFromFile("api-gateway.json"),
InvokedFunctionARN: "arn:aws:lambda:us-east-1:123456789012:function:my-function",
StartTime: startInvocationTime,
}
endDetails = &InvocationEndDetails{
RequestID: "test-request-id",
EndTime: endInvocationTime,
}

testProcessor.OnInvokeStart(startDetails)
testProcessor.OnInvokeEnd(endDetails)

spans = tracePayload.TracerPayload.Chunks[0].Spans
assert.Equal(t, 2, len(spans))
}

func TestTriggerTypesLifecycleEventForSQSNoDdContext(t *testing.T) {

startInvocationTime := time.Now()
Expand Down

0 comments on commit 9b6d767

Please sign in to comment.