Skip to content

Bug: Correlation ID extraction fails for CloudWatch Events with custom paths #1048

@hjgraca

Description

@hjgraca

Expected Behaviour

When using the [Logging] attribute with a custom CorrelationIdPath for CloudWatch Events, the correlation ID should be extracted from the event's detail object and automatically added to all log entries.

[Logging(ClearState = true, LogEvent = true, CorrelationIdPath = "/detail/correlationId")]
public async Task FunctionHandler(CloudWatchEvent<RideCreatedEvent> eventBridgeEvent, ILambdaContext context)
{
    Logger.LogInformation("Processing ride");
    // Expected: correlation ID from eventBridgeEvent.Detail.CorrelationId is included in logs
}

The correlation ID should be extracted regardless of the JSON property name casing in the serialized event.

Current Behaviour

The correlation ID extraction fails for CloudWatch Events with custom paths. The CorrelationIdPath = "/detail/correlationId" does not extract the correlation ID from the event, while the same approach works correctly for API Gateway requests with paths like "/headers/x-correlation-id".

The issue occurs because the correlation ID extraction logic only attempts to match JSON properties after applying output case transformations, which doesn't account for the actual property names in the serialized CloudWatch Event object.

Code snippet

// This works correctly
[Logging(ClearState = true, CorrelationIdPath = "/headers/x-correlation-id")]
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
{
    Logger.LogInformation("Processing request");
    // Correlation ID IS extracted successfully
}

Possible Solution

No response

Steps to Reproduce

Create a function with the folowing details and observe that no correlationid is not set

[Logging(ClearState = true, LogEvent = true, CorrelationIdPath = "/detail/correlationId")]
public async Task FunctionHandler(CloudWatchEvent<RideCreatedEvent> eventBridgeEvent, ILambdaContext context)
{
    Logger.LogInformation("Processing ride");
    // Expected: correlation ID from eventBridgeEvent.Detail.CorrelationId is included in logs
}

Powertools for AWS Lambda (.NET) version

latest

AWS Lambda function runtime

dotnet6

Debugging logs

Metadata

Metadata

Assignees

Labels

bugUnexpected, reproducible and unintended software behaviour

Type

Projects

Status

👀 In review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions