Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions contrib/aws/datadog-lambda-go/internal/extension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/DataDog/dd-trace-go/contrib/aws/datadog-lambda-go/v2/internal/logger"
"github.com/aws/aws-lambda-go/lambdacontext"

ddtracer "github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
)
Expand All @@ -40,6 +41,8 @@ const (

DdSeverlessSpan ddTraceContext = "dd-tracer-serverless-span"
DdLambdaResponse ddTraceContext = "dd-response"

lambdaRuntimeAwsRequestIdHeader = "lambda-runtime-aws-request-id"
)

const (
Expand Down Expand Up @@ -116,6 +119,13 @@ func (em *ExtensionManager) checkAgentRunning() {
func (em *ExtensionManager) SendStartInvocationRequest(ctx context.Context, eventPayload json.RawMessage) context.Context {
body := bytes.NewBuffer(eventPayload)
req, _ := http.NewRequest(http.MethodPost, em.startInvocationUrl, body)

if lc, ok := lambdacontext.FromContext(ctx); ok {
req.Header.Set(lambdaRuntimeAwsRequestIdHeader, lc.AwsRequestID)
} else {
logger.Error(fmt.Errorf("missing AWS Lambda context. Unable to set lambda-runtime-aws-request-id header"))
}

response, err := em.httpClient.Do(req)
if response != nil && response.Body != nil {
defer func() {
Expand Down Expand Up @@ -153,6 +163,11 @@ func (em *ExtensionManager) SendEndInvocationRequest(ctx context.Context, functi
}
body := bytes.NewBuffer(content)
req, _ := http.NewRequest(http.MethodPost, em.endInvocationUrl, body)
if lc, ok := lambdacontext.FromContext(ctx); ok {
req.Header.Set(lambdaRuntimeAwsRequestIdHeader, lc.AwsRequestID)
} else {
logger.Error(fmt.Errorf("missing AWS Lambda context. Unable to set lambda-runtime-aws-request-id header"))
}

// Mark the invocation as an error if any
if cfg.Error != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
"fmt"
"net/http"
"os"
"strings"
"testing"

"github.com/DataDog/dd-trace-go/contrib/aws/datadog-lambda-go/v2/internal/logger"
"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/aws/aws-lambda-go/lambdacontext"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -149,6 +151,41 @@ func TestExtensionStartInvoke(t *testing.T) {
assert.Nil(t, samplingPriority)
}

func TestExtensionStartInvokeLambdaRequestId(t *testing.T) {
headers := http.Header{}
capturingClient := capturingClient{hdr: headers}

em := &ExtensionManager{
startInvocationUrl: startInvocationUrl,
httpClient: capturingClient,
}

lc := &lambdacontext.LambdaContext{
AwsRequestID: "test-request-id-12345",
}
ctx := lambdacontext.NewContext(context.TODO(), lc)
em.SendStartInvocationRequest(ctx, []byte{})

err := em.Flush()

assert.Nil(t, err)
assert.Equal(t, "test-request-id-12345", headers.Get("lambda-runtime-aws-request-id"))
}

func TestExtensionStartInvokeLambdaRequestIdError(t *testing.T) {
em := &ExtensionManager{
startInvocationUrl: startInvocationUrl,
httpClient: &ClientSuccessStartInvoke{},
}

logOutput := captureLog(func() { em.SendStartInvocationRequest(context.TODO(), []byte{}) })
err := em.Flush()
assert.Nil(t, err)
assert.Contains(t, logOutput, "missing AWS Lambda context. Unable to set lambda-runtime-aws-request-id header")
lines := strings.Split(strings.TrimSpace(logOutput), "\n")
assert.Equal(t, 1, len(lines))
}

func TestExtensionStartInvokeWithTraceContext(t *testing.T) {
headers := http.Header{}
headers.Set(string(DdTraceId), mockTraceId)
Expand Down Expand Up @@ -201,13 +238,58 @@ func TestExtensionEndInvocation(t *testing.T) {
endInvocationUrl: endInvocationUrl,
httpClient: &ClientSuccessEndInvoke{},
}
ctx := lambdacontext.NewContext(context.TODO(), &lambdacontext.LambdaContext{})
span := tracer.StartSpan("aws.lambda")
logOutput := captureLog(func() { em.SendEndInvocationRequest(context.TODO(), span, tracer.FinishConfig{}) })
logOutput := captureLog(func() { em.SendEndInvocationRequest(ctx, span, tracer.FinishConfig{}) })
span.Finish()

assert.Equal(t, "", logOutput)
}

func TestExtensionEndInvokeLambdaRequestId(t *testing.T) {
headers := http.Header{}
capturingClient := capturingClient{hdr: headers}

em := &ExtensionManager{
endInvocationUrl: endInvocationUrl,
httpClient: capturingClient,
}

lc := &lambdacontext.LambdaContext{
AwsRequestID: "test-request-id-12345",
}

ctx := lambdacontext.NewContext(context.TODO(), lc)
span := tracer.StartSpan("aws.lambda")
span.Finish()
cfg := tracer.FinishConfig{}
em.SendEndInvocationRequest(ctx, span, cfg)
err := em.Flush()
assert.Nil(t, err)
assert.Equal(t, "test-request-id-12345", headers.Get("lambda-runtime-aws-request-id"))
}

func TestExtensionEndInvokeLambdaRequestIdError(t *testing.T) {
headers := http.Header{}
capturingClient := capturingClient{hdr: headers}
ctx := context.WithValue(context.TODO(), DdSamplingPriority, mockSamplingPriority)
ctx = context.WithValue(ctx, DdTraceId, mockTraceId)
em := &ExtensionManager{
endInvocationUrl: endInvocationUrl,
httpClient: capturingClient,
}

span := tracer.StartSpan("aws.lambda")
logOutput := captureLog(func() { em.SendEndInvocationRequest(ctx, span, tracer.FinishConfig{}) })
span.Finish()

err := em.Flush()
assert.Nil(t, err)
assert.Contains(t, logOutput, "missing AWS Lambda context. Unable to set lambda-runtime-aws-request-id header")
lines := strings.Split(strings.TrimSpace(logOutput), "\n")
assert.Equal(t, 1, len(lines))
}

func TestExtensionEndInvocationError(t *testing.T) {
em := &ExtensionManager{
endInvocationUrl: endInvocationUrl,
Expand Down
Loading