From bf862675dea35659c1c447e88ff543ea7685d945 Mon Sep 17 00:00:00 2001 From: robandpdx <95243761+robandpdx@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:19:43 +0000 Subject: [PATCH] docs: add Lambda invocation logging example Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/README.md b/README.md index a85748d..6b7d550 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,65 @@ functions: Make sure to configure your GitHub App registration's webhook URL to `/api/github/webhooks`. +## Logging invocation IDs + +To include Lambda and request trace IDs in JSON logs, supply a Pino logger and +use `AsyncLocalStorage` to associate fields with each invocation. Install Pino +directly in your application: + +```shell +npm install pino +``` + +```javascript +// handler.js +import { AsyncLocalStorage } from "node:async_hooks"; +import pino from "pino"; +import { + createLambdaFunction, + createProbot, +} from "@probot/adapter-aws-lambda-serverless"; +import appFn from "./index.js"; + +const invocation = new AsyncLocalStorage(); +const log = pino({ + level: process.env.LOG_LEVEL || "warn", + mixin() { + return { ...invocation.getStore() }; + }, +}); + +const handle = createLambdaFunction(appFn, { + probot: createProbot({ defaults: { log } }), +}); + +export const lambdaFn = (event, context) => { + const headers = Object.fromEntries( + Object.entries(event.headers ?? {}).map(([name, value]) => [ + name.toLowerCase(), + value, + ]), + ); + + return invocation.run( + { + awsRequestId: context.awsRequestId, + apiGatewayRequestId: event.requestContext?.requestId, + traceId: headers["x-amzn-trace-id"], + cloudFrontRequestId: headers["x-amz-cf-id"], + }, + () => handle(event, context), + ); +}; +``` + +The supplied Pino logger outputs JSON independently of `NODE_ENV` and +`LOG_FORMAT`. Probot adds the GitHub delivery ID as `id` to event-handler +logs. Invocation fields are available to logs created within the invocation's +async context, not to startup logs emitted before it. Some request fields may +be absent depending on the AWS integration. Log only selected headers rather +than all headers, which may contain secrets. + ## Test deployment For testing your Probot deployment without end to end (GitHub) integration, you can use this shell snippet: