Skip to content

Commit

Permalink
add support for passing request id to the HTTP handler
Browse files Browse the repository at this point in the history
  • Loading branch information
iamatypeofwalrus committed Apr 8, 2024
1 parent 901f046 commit d3ea3e9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api_gateway_v2_http_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,16 @@ func NewHttpRequestFromAPIGatewayV2HTTPRequest(ctx context.Context, event events
return nil, fmt.Errorf("shim could not create http request from event: %w", err)
}

// Xray tracing is passed in via x-amzn-trace-id header that is on the Lambda Event
for h, v := range event.Headers {
req.Header.Set(h, v)
}

requestID := event.RequestContext.RequestID
if requestID != "" {
req.Header.Set("x-request-id", requestID)
}

req.URL.Host = req.Header.Get("Host")
req.Host = req.Header.Get("Host")

Expand Down
36 changes: 36 additions & 0 deletions api_gateway_v2_http_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,39 @@ func TestNewHttpRequestFromAPIGatewayV2HTTPRequest_Base64(t *testing.T) {
t.Errorf("expected body '%s', got '%s'", body, string(decodedBody))
}
}

func TestNewHttpRequestFromAPIGatewayV2HTTPRequest_XRayAndLambdaRequestIDs(t *testing.T) {
requestID := "request-id"
traceID := "trace-id"

// Create a sample APIGatewayV2HTTPRequest event
event := events.APIGatewayV2HTTPRequest{
Version: "2.0",
RouteKey: "GET /hello",
RawPath: "/hello",
RawQueryString: "name=John&age=30",
Headers: map[string]string{
"Content-Type": "application/json",
"x-amzn-trace-id": traceID,
},
QueryStringParameters: map[string]string{"name": "John", "age": "30"},
RequestContext: events.APIGatewayV2HTTPRequestContext{
RequestID: requestID,
},
}

// Call the function under test
req, err := NewHttpRequestFromAPIGatewayV2HTTPRequest(context.Background(), event)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

// Check if the X-Ray ID and the Lambda request ID are added as headers to the request
if req.Header.Get("x-amzn-trace-id") != traceID {
t.Errorf("expected X-Ray trace ID '%s', got '%s'", traceID, req.Header.Get("X-Amzn-Trace-Id"))
}

if req.Header.Get("x-request-id") != requestID {
t.Errorf("expected Lambda request ID '%s', got '%s'", requestID, req.Header.Get("Lambda-Runtime-Aws-Request-Id"))
}
}

0 comments on commit d3ea3e9

Please sign in to comment.