aws / aws-lambda-dotnet

Libraries, samples and tools to help .NET Core developers develop AWS Lambda functions.
Apache License 2.0
1.57k stars 477 forks source link

LoggingFormat=JSON does not affect exception logging #1831

Open Dreamescaper opened 2 weeks ago

Dreamescaper commented 2 weeks ago

Describe the bug

When I set lambda LoggingFormat to JSON, exception logs remains in TEXT format.

Regression Issue

Expected Behavior

I would expect all logs from lambda published to CloudWatch in JSON format, including fail' exception logs.

Current Behavior

Exception logs are not in JSON format. image

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

Amazon.Lambda.Core 2.3.0

Targeted .NET Platform

.NET 8

Operating System and version

AmazonLinux

ashishdhingra commented 2 weeks ago

@Dreamescaper Good morning. Thanks for opening the issue. Please let us know the following:

Thanks, Ashish

Dreamescaper commented 2 weeks ago

I have a suspicion that this issue is already fixed, but is waiting for a managed runtime update on lambdas...

I'll provide the info you're asking a bit later.

Dreamescaper commented 2 weeks ago

@ashishdhingra

  1. No, we don't use AWS_LAMBDA_LOG_FORMAT log format, we configure it in Lambda Logging configuration: image

  2. I have updated Amazon.Lambda.Core to 2.3.0, nothing has changed.

ashishdhingra commented 2 weeks ago

@ashishdhingra

  1. No, we don't use AWS_LAMBDA_LOG_FORMAT log format, we configure it in Lambda Logging configuration:

image

  1. I have updated Amazon.Lambda.Core to 2.3.0, nothing has changed.

@Dreamescaper The changes for structured logging would work as of now when using Amazon.Lambda.Core version 2.3.0 when using executable assembly. For class library projects, we would need to wait for Lambda runtime to get updated. As of now we don't have exact ETA, but it could take few weeks. CC @normj

normj commented 2 weeks ago

Are you deploying your function as an executable or class library? If you are deploying as an executable pulling in the latest versions of Amazon.Lambda.RuntimeSupport and Amazon.Lambda.Core then it should be working. It is the class library programming model we are waiting for the Lambda deployment to happen.

I'm assuming you are saying an exception that is thrown and not caught within your function handler. I just confirmed I got expected behavior with a function that does this:

var handler = (string input, ILambdaContext context) =>
{
    if (string.Equals(input, "fail", StringComparison.InvariantCultureIgnoreCase))
    {
        throw new ArgumentException("You said fail");
    }
    return input.ToUpper();
};

// Build the Lambda runtime client passing in the handler to call for each
// event and the JSON serializer to use for translating Lambda JSON documents
// to .NET types.
await LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
        .Build()
        .RunAsync();

And get the following log message:

{
  "timestamp": "2024-10-09T22:09:42.804Z",
  "level": "Error",
  "requestId": "2882bc48-17de-4d53-817e-b91aa1616a65",
  "traceId": "Root=1-6706ff26-6370f67a168e1f2b3e1927ae;Parent=4fab4474991f77fa;Sampled=0;Lineage=1:ced1418f:0",
  "message": null,
  "errorType": "System.ArgumentException",
  "errorMessage": "You said fail",
  "stackTrace": [
    "System.ArgumentException: You said fail",
    "at Program.<>c.<<Main>$>b__0_0(String input, ILambdaContext context) in C:\\Users\\normj\\source\\repos\\StructuredLoggingWithException\\StructuredLoggingWithException\\Function.cs:line 10",
    "at Amazon.Lambda.RuntimeSupport.HandlerWrapper.<>c__DisplayClass44_0`2.<GetHandlerWrapper>b__0(InvocationRequest invocation)",
    "at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InvokeOnceAsync(CancellationToken cancellationToken)"
  ]
}
github-actions[bot] commented 4 days ago

This issue has not received a response in 5 days. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.