Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in Parsing Lambda Result Logs #253

Open
RomanOttGmx opened this issue Jun 27, 2024 · 7 comments
Open

Error in Parsing Lambda Result Logs #253

RomanOttGmx opened this issue Jun 27, 2024 · 7 comments

Comments

@RomanOttGmx
Copy link

I get an error during executing state machine in the executor lambda.

Logs from Execute Lambda:
{
"errorType": "SyntaxError",
"errorMessage": "Unexpected end of JSON input",
"stack": [
"SyntaxError: Unexpected end of JSON input",
" at JSON.parse ()",
" at /var/task/utils.js:552:54",
" at Array.map ()",
" at module.exports.extractDurationFromJSON (/var/task/utils.js:552:35)",
" at module.exports.extractDuration (/var/task/utils.js:529:22)",
" at /var/task/utils.js:473:22",
" at Array.map ()",
" at module.exports.parseLogAndExtractDurations (/var/task/utils.js:470:17)",
" at computeStatistics (/var/task/executor.js:180:29)",
" at module.exports.handler (/var/task/executor.js:71:12)"
]
}

Because the result inherits empty lines.

In utils.js there is a module function: module.exports.extractDurationFromJSON = (log) => {

const lines = log.split('\n').filter((line) => line.startsWith('{')).map((line) => {

Replacing it with:
const lines = log.split('\n').filter(line => line.trim() !== '').map((line) => JSON.parse(line));

it works. Best Regards

@patmeiler
Copy link
Collaborator

patmeiler commented Jul 15, 2024

Hi @RomanOttGmx , thanks for raising this. I'll look into it. Could you clarify a bit further how this leads to an empty lines in the JSON? I'd like to check if there are any other points where this could happen.

@Marcel-Brouwer
Copy link

Having the same issue. We are seeing a trailing newline in the logs when logging the incoming event, using the injectLambdaContext middleware from Powertools Logger.

https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger/#log-incoming-event

This leads to an empty line.

@alexcasalboni
Copy link
Owner

hey @Marcel-Brouwer @RomanOttGmx thanks for sharing!

The current implementation (here):

const lines = log.split('\n').filter((line) => line.startsWith('{')).map((line) => {...});

should already manage empty lines quite well (as they don't start with {).

Could you please look into the logs that generate the Unexpected end of JSON input error and share here? It's true that JSON.parse("") throws such an error, but we aren't parsing empty lines. Even if it was a JSON line with some weird syntax (like a missing } at the end), the current implementation handles that and should print out:

console.error(`Detected invalid JSON line: ${line}`);

I'd like to add a regression test to catch this case, if possible :)

@alexcasalboni
Copy link
Owner

maybe the Powertools Logger is logging out prettyfied logs? (with newlines in between fields for readability)

something like:

{
  "cold_start": true,
  "function_arn": "arn:aws:lambda:eu-west-1:123456789012:function:LogEventFn",
  "function_memory_size": "128",
  "function_name": "LogEventFn",
  "function_request_id": "0a9df60d-e2de-447d-ba3e-45f149eae6c9",
  "level": "INFO",
  "message": "Lambda invocation event",
  "sampling_rate": 0,
  "service": "service_undefined",
  "timestamp": "2024-08-14T10:08:06.199Z",
  "xray_trace_id": "1-66bc8205-21f8b5190da519d22b2b0533",
  "event": {
    "foo": "bar"
  }
}

instead of

{ "cold_start": true, "function_arn": "arn:aws:lambda:eu-west-1:123456789012:function:LogEventFn", "function_memory_size": "128", "function_name": "LogEventFn", "function_request_id": "0a9df60d-e2de-447d-ba3e-45f149eae6c9", "level": "INFO", "message": "Lambda invocation event", "sampling_rate": 0, "service": "service_undefined", "timestamp": "2024-08-14T10:08:06.199Z", "xray_trace_id": "1-66bc8205-21f8b5190da519d22b2b0533", "event": {   "foo": "bar" }}

In that case, our implementation would break since we expect every log to be one line.

@tavosansal
Copy link

I ran into this when my lambda had Log Format set to JSON.
Once I changed it back to Text format the power tuner ran fine.

@shawnxue
Copy link

shawnxue commented Oct 1, 2024

Same as @tavosansal , this issue was gone after I changed Lambda log format to text. The funny thing for me is that: We have the same lambda functions deployed in AWS AMAP regions, EMEA regions and China. Only China region has this issue.

@alexcasalboni
Copy link
Owner

@shawnxue that's interesting! I imagine you're using the SAR app to deploy Lambda Power Tuning in China as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants