Skip to content

Commit 07f6ea8

Browse files
committed
add error tests for lambda requestIDs
1 parent 4b2cbd4 commit 07f6ea8

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

internal/extension/extension.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,13 @@ func (em *ExtensionManager) checkAgentRunning() {
122122
func (em *ExtensionManager) SendStartInvocationRequest(ctx context.Context, eventPayload json.RawMessage) context.Context {
123123
body := bytes.NewBuffer(eventPayload)
124124
req, _ := http.NewRequest(http.MethodPost, em.startInvocationUrl, body)
125-
lc, _ := lambdacontext.FromContext(ctx)
126-
req.Header.Set(lambdaRuntimeAwsRequestIdHeader, lc.AwsRequestID)
125+
126+
if lc, ok := lambdacontext.FromContext(ctx); ok {
127+
req.Header.Set(lambdaRuntimeAwsRequestIdHeader, lc.AwsRequestID)
128+
} else {
129+
logger.Error(fmt.Errorf("missing lambda Context. Unable to set lambda-runtime-aws-request-id header"))
130+
}
131+
127132
response, err := em.httpClient.Do(req)
128133
if response != nil && response.Body != nil {
129134
defer func() {
@@ -162,8 +167,11 @@ func (em *ExtensionManager) SendEndInvocationRequest(ctx context.Context, functi
162167
}
163168
body := bytes.NewBuffer(content)
164169
req, _ := http.NewRequest(http.MethodPost, em.endInvocationUrl, body)
165-
lc, _ := lambdacontext.FromContext(ctx)
166-
req.Header.Set(lambdaRuntimeAwsRequestIdHeader, lc.AwsRequestID)
170+
if lc, ok := lambdacontext.FromContext(ctx); ok {
171+
req.Header.Set(lambdaRuntimeAwsRequestIdHeader, lc.AwsRequestID)
172+
} else {
173+
logger.Error(fmt.Errorf("missing lambda Context. Unable to set lambda-runtime-aws-request-id header"))
174+
}
167175

168176
// Mark the invocation as an error if any
169177
if cfg.Error != nil {

internal/extension/extension_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ func TestExtensionStartInvokeLambdaRequestId(t *testing.T) {
175175
assert.Equal(t, "test-request-id-12345", headers.Get("lambda-runtime-aws-request-id"))
176176
}
177177

178+
func TestExtensionStartInvokeLambdaRequestIdError(t *testing.T) {
179+
headers := http.Header{}
180+
capturingClient := capturingClient{hdr: headers}
181+
182+
em := &ExtensionManager{
183+
startInvocationUrl: startInvocationUrl,
184+
httpClient: capturingClient,
185+
}
186+
187+
logOutput := captureLog(func() { em.SendStartInvocationRequest(context.TODO(), []byte{}) })
188+
err := em.Flush()
189+
assert.Nil(t, err)
190+
assert.Contains(t, logOutput, "missing lambda Context. Unable to set lambda-runtime-aws-request-id header")
191+
lines := strings.Split(strings.TrimSpace(logOutput), "\n")
192+
assert.Equal(t, 1, len(lines))
193+
}
194+
178195
func TestExtensionStartInvokeWithTraceContext(t *testing.T) {
179196
headers := http.Header{}
180197
headers.Set(string(DdTraceId), mockTraceId)
@@ -227,8 +244,9 @@ func TestExtensionEndInvocation(t *testing.T) {
227244
endInvocationUrl: endInvocationUrl,
228245
httpClient: &ClientSuccessEndInvoke{},
229246
}
247+
ctx := lambdacontext.NewContext(context.TODO(), &lambdacontext.LambdaContext{})
230248
span := tracer.StartSpan("aws.lambda")
231-
logOutput := captureLog(func() { em.SendEndInvocationRequest(context.TODO(), span, ddtrace.FinishConfig{}) })
249+
logOutput := captureLog(func() { em.SendEndInvocationRequest(ctx, span, ddtrace.FinishConfig{}) })
232250
span.Finish()
233251
// Expected because the noopSpanContext doesn't have the SamplingPriority() and we cannot use the mock for the agent
234252
assert.Contains(t, logOutput, "could not get sampling priority from getSamplingPriority()")
@@ -260,6 +278,27 @@ func TestExtensionEndInvokeLambdaRequestId(t *testing.T) {
260278
assert.Equal(t, "test-request-id-12345", headers.Get("lambda-runtime-aws-request-id"))
261279
}
262280

281+
func TestExtensionEndInvokeLambdaRequestIdError(t *testing.T) {
282+
headers := http.Header{}
283+
capturingClient := capturingClient{hdr: headers}
284+
ctx := context.WithValue(context.TODO(), DdSamplingPriority, mockSamplingPriority)
285+
ctx = context.WithValue(ctx, DdTraceId, mockTraceId)
286+
em := &ExtensionManager{
287+
startInvocationUrl: startInvocationUrl,
288+
httpClient: capturingClient,
289+
}
290+
291+
span := tracer.StartSpan("aws.lambda")
292+
logOutput := captureLog(func() { em.SendEndInvocationRequest(ctx, span, ddtrace.FinishConfig{}) })
293+
span.Finish()
294+
295+
err := em.Flush()
296+
assert.Nil(t, err)
297+
assert.Contains(t, logOutput, "missing lambda Context. Unable to set lambda-runtime-aws-request-id header")
298+
lines := strings.Split(strings.TrimSpace(logOutput), "\n")
299+
assert.Equal(t, 1, len(lines))
300+
}
301+
263302
func TestExtensionEndInvocationError(t *testing.T) {
264303
em := &ExtensionManager{
265304
endInvocationUrl: endInvocationUrl,

0 commit comments

Comments
 (0)