probot / adapter-aws-lambda-serverless

An extension for running Probot on Lambda
ISC License
93 stars 36 forks source link

Log AWS and Custom Fields to AWS CloudWatch Log #115

Open matthewdfuller opened 1 year ago

matthewdfuller commented 1 year ago

I'm creating a Probot app using the sample code, like so:

const {
    createLambdaFunction,
    createProbot,
} = require("@probot/adapter-aws-lambda-serverless");
const appFn = require("./");

exports.handler = createLambdaFunction(appFn, {
    probot: createProbot(),
});

My NODE_ENV is set to production, which ~sets the pino logging output to JSON, which is logged to AWS CloudWatch.~

Update: The environment variable is set, but the logging is not JSON; it's still pretty print.

However, the logs are missing some key pieces of information that I'd like to inject. Specifically: the AWS Request ID, CloudFront (or API Gateway) headers/trace IDs, and GitHub event IDs.

I found this issue that proposes something like:

const logger = pino.child({
      awsRequestId: context.awsRequestId,
})

But I can't figure out where to overwrite (or modify) the pino logger that Probot has already created.

Any ideas on where we could inject this additional info to make Lambda logs more useful?

matthewdfuller commented 1 year ago

In case it's helpful for other folks searching for similar issues, I was able to get the following mostly working:

const pino = require('pino')({
    prettyPrint: false,
    level: process.env['LOG_LEVEL']
});

const {
    createLambdaFunction,
    createProbot,
} = require("@probot/adapter-aws-lambda-serverless");
const appFn = require("./");

exports.handler = createLambdaFunction(appFn, {
    probot: createProbot({
        defaults: { 
            log: pino
        }
    }),
});

For some reason, the logger was not respecting the NODE_ENV environment variable and I had to set prettyPrint: false explicitly.

I'm still unsure how to inject the Lambda RequestID, since the above is creating the handler globally (i.e. not per invocation), so the typical Lambda context and event objects are not accessible.