Skip to content

Request ID logging #3

@mnapoli

Description

@mnapoli

Here are some notes about logging with the AWS request ID.

The goal

Be able to see all logs of a single invocation.

How? By filtering using the "request ID".

How? By storing that request ID in each log record.

Is it not there already? It's there when logging from a JS lambda for example.

When logging from PHP (by writing to stdout/stderr), only the START, END and REPORT log lines contain it (the lines written by Lambda itself).

NodeJS runtime

exports.handler = async () => {
    console.log('Hello from Lambda!');
};

This is what we see in CloudWatch:

Capture d’écran 2020-03-13 à 22 38 05

This is the same thing in Insights:

Capture d’écran 2020-03-13 à 22 39 49

Analysis: the request ID is added in a non-structured text line. Values are separated by tabs (\t). For example:

2020-03-13T21:21:56.341Z 0879de55-0817-4d5c-9f37-7ed2755a72e1 INFO Hello from Lambda! 

Current Bref PHP runtime

echo 'Hello world';

In CloudWatch:

Capture d’écran 2020-03-13 à 22 45 48

In Insights:

Capture d’écran 2020-03-13 à 22 47 35

Analysis: the @requestId field (or the request ID value itself) does not appear in that record.

Experiment 1

echo json_encode([
        'message' => 'Hello world!',
        'requestId' => $context->getAwsRequestId(),
        '@requestId' => $context->getAwsRequestId(),
]) . PHP_EOL;

In CloudWatch:

Capture d’écran 2020-03-13 à 22 40 59

In Insights:

Capture d’écran 2020-03-13 à 22 42 14

Analysis: it looked promising at first, but it turns out the @requestId field is escaped to @@requestId by CloudWatch (or Lambda?).

That means that we still have no @requestId field. For example, this query does not return the lines we logged in PHP:

fields @message
| filter @requestId = '6dc9b447-3898-4793-83cf-60730058c833'

Experiment 2

Based on the tabulated format from the JS runtime, could it be that CloudWatch interprets data that way?

echo "2020-03-13T21:21:56.341Z\t{$context->getAwsRequestId()}\tINFO\tHello world" . PHP_EOL;

CloudWatch:

Capture d’écran 2020-03-13 à 22 50 17

Insights:

Capture d’écran 2020-03-13 à 22 52 37

Analysis: OMG it works!!!

The field is correctly recognized now:

Capture d’écran 2020-03-13 à 22 54 54

Even serverless logs parsed the fields:

Capture d’écran 2020-03-13 à 22 56 35

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions