Gathering detailed insights and metrics for pino-lambda
Gathering detailed insights and metrics for pino-lambda
Gathering detailed insights and metrics for pino-lambda
Gathering detailed insights and metrics for pino-lambda
npm install pino-lambda
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
126 Stars
92 Commits
13 Forks
39 Watching
4 Branches
71 Contributors
Updated on 06 Nov 2024
TypeScript (72.47%)
JavaScript (27.53%)
Cumulative downloads
Total Downloads
Last day
-9.1%
13,207
Compared to previous day
Last week
-7.3%
69,843
Compared to previous week
Last month
-5.4%
314,214
Compared to previous month
Last year
41.1%
2,933,610
Compared to previous year
1
24
A custom destination for pino that takes advantage of the unique environment in AWS Lambda functions. ref
By default, this destination reformats the log output so it matches the existing Cloudwatch format. The default pino log format loses some of the built in support for request ID tracing that lambda has built into to support Cloudwatch insights and Xray tracing.
It also automatically tracks the request id, correlation ids, and xray tracing from upstream services.
Basic usage for most applications
1import pino from 'pino'; 2import { lambdaRequestTracker, pinoLambdaDestination } from 'pino-lambda'; 3 4// custom destination formatter 5const destination = pinoLambdaDestination(); 6const logger = pino( 7 { 8 // typical pino options 9 }, 10 destination, 11); 12const withRequest = lambdaRequestTracker(); 13 14async function handler(event, context) { 15 // automatic request tracing across all instances of pino 16 // called once at the beginning of your Lambda handler 17 withRequest(event, context); 18 19 // typical logging methods 20 logger.info({ data: 'Some data' }, 'A log message'); 21}
Cloudwatch output will now match the native console.log
output, correctly preserving
@requestid
, @timestamp
, and @message
properties for use in Cloudwatch Insights and
other Cloudwatch aware tools such as Datadog and Splunk.
2018-12-20T17:05:25.330Z 6fccb00e-0479-11e9-af91-d7ab5c8fe19e INFO A log message
{
"awsRequestId": "6fccb00e-0479-11e9-af91-d7ab5c8fe19e",
"x-correlation-id": "238da608-0542-11e9-8eb2-f2801f1b9fd1",
"x-correlation-trace-id": "Root=1-5c1bcbd2-9cce3b07143efd5bea1224f2;Parent=07adc05e4e92bf13;Sampled=1",
"level": 30,
"message": "A log message",
"data": "Some data"
}
The request logging context can be updated downstream by calling the updateContext
function. Any duplicate values will overwrite previous values.
Note: If you provide a custom storage context, you will need to update that storage context directly (this is not typical)
import { GlobalContextStorageProvider } from 'pino-lambda';
GlobalContextStorageProvider.updateContext({ userId: '12' });
With context tracing enabled, all instances of pino
that use one of the built in formatters will automatically log the request id and other details so you don't need to pass an instance of a logger to all of your functions.
Property | Value | Info |
---|---|---|
awsRequestId | context.awsRequestId | The unique request id for this request |
apiRequestId | context.requestContext.requestId | The API Gateway RequestId |
x-correlation-id | event.headers['x-correlation-id'] | The upstream request id for tracing |
x-correlation-trace-id | process.env._X_AMZN_TRACE_ID | The AWS Xray tracking id |
x-correlation-* | event.headers.startsWith('x-correlation-') | Any header that starts with x-correlation- will be automatically added |
Every AWS Lambda request contains a unique request ID, context.awsRequestId
. If the request originated outside of the AWS platform,
the request ID will match the event.header.x-correlation-id
value. However, if the request originated from within the AWS platform,
the event.header.x-correlation-id
will be set to the request ID of the calling service. This allows you to trace a request
across the entire platform.
Amazon XRAY also has a unique tracing ID that is propagated across the requests and can be tracked as well.
You can customize the data that is tracked for each request by adding a per-request mixin.
The request mixin takes the Lambda event
and context
and returns an object.
This differs from the built in pino mixin as it only executes once per request where the built in pino mixin runs once per log entry.
1import pino from 'pino'; 2import { lambdaRequestTracker, pinoLambdaDestination } from 'pino-lambda'; 3 4const destination = pinoLambdaDestination(); 5const logger = pino(destination); 6const withRequest = lambdaRequestTracker({ 7 requestMixin: (event, context) => { 8 return { 9 // add request header host name 10 host: event.headers?.host, 11 12 // you can also set any request property to undefined 13 // which will remove it from the output 14 'x-correlation-id': undefined, 15 16 // add any type of static data 17 brand: 'famicom', 18 }; 19 }, 20}); 21 22async function handler(event, context) { 23 withRequest(event, context); 24 logger.info({ data: 'Some data' }, 'A log message'); 25}
Output
2018-12-20T17:05:25.330Z 6fccb00e-0479-11e9-af91-d7ab5c8fe19e INFO A log message
{
"awsRequestId": "6fccb00e-0479-11e9-af91-d7ab5c8fe19e",
"x-correlation-trace-id": "Root=1-5c1bcbd2-9cce3b07143efd5bea1224f2;Parent=07adc05e4e92bf13;Sampled=1",
"level": 30,
"host": "www.host.com",
"brand": "famicom",
"message": "A log message",
"data": "Some data"
}
By default, the pinoLambdaDestination
uses the CloudwatchLogFormatter
.
If you would like to use the new AWS Lambda Advanced Logging Controls format for your logs, ensure your Lambda function is properly configured and enable StructuredLogFormatter
in pino-lambda
.
1import pino from 'pino'; 2import { lambdaRequestTracker, pinoLambdaDestination, StructuredLogFormatter } from 'pino-lambda'; 3 4const destination = pinoLambdaDestination({ 5 formatter: new StructuredLogFormatter() 6}); 7const logger = pino(destination); 8const withRequest = lambdaRequestTracker(); 9 10async function handler(event, context) { 11 withRequest(event, context); 12 logger.info({ data: 'Some data' }, 'A log message'); 13}
Output
1{ 2 "timestamp": "2016-12-01T06:00:00.000Z", 3 "requestId": "6fccb00e-0479-11e9-af91-d7ab5c8fe19e", 4 "level": "INFO", 5 "message": { 6 "msg": "A log message", 7 "data": "Some data", 8 "x-correlation-trace-id": "Root=1-5c1bcbd2-9cce3b07143efd5bea1224f2;Parent=07adc05e4e92bf13;Sampled=1" 9 } 10}
If you want the request tracing features of pino-lambda
, but don't need the Cloudwatch format, you can use the PinoLogFormatter
which matches the default object output format of pino
.
1import pino from 'pino'; 2import { lambdaRequestTracker, pinoLambdaDestination, PinoLogFormatter } from 'pino-lambda'; 3 4const destination = pinoLambdaDestination({ 5 formatter: new PinoLogFormatter(), 6}); 7const logger = pino(destination); 8const withRequest = lambdaRequestTracker(); 9 10async function handler(event, context) { 11 withRequest(event, context); 12 logger.info({ data: 'Some data' }, 'A log message'); 13}
Output
1{ 2 "awsRequestId": "6fccb00e-0479-11e9-af91-d7ab5c8fe19e", 3 "x-correlation-trace-id": "Root=1-5c1bcbd2-9cce3b07143efd5bea1224f2;Parent=07adc05e4e92bf13;Sampled=1", 4 "level": 30, 5 "time": 1480572000000, 6 "msg": "A log message", 7 "data": "Some data" 8}
The formatter function can also be replaced with any custom implementation you need by using the supplied interface.
1import { LogData, ILogFormatter } from 'pino-lambda'; 2 3class BananaLogFormatter implements ILogFormatter { 4 format(data: LogData) { 5 return `[BANANA] ${JSON.stringify(data)}`; 6 } 7} 8 9const destination = pinoLambdaDestination({ 10 formatter: new BananaLogFormatter(), 11}); 12const logger = pino(destination);
Output
[BANANA]
{
"awsRequestId": "6fccb00e-0479-11e9-af91-d7ab5c8fe19e",
"x-correlation-trace-id": "Root=1-5c1bcbd2-9cce3b07143efd5bea1224f2;Parent=07adc05e4e92bf13;Sampled=1",
"level": 30,
"msg": "A log message",
"data": "Some data"
}
Unless your application is small, it can be useful to split the logger into its own module for easier reuse across your application code. This ensures that all your logging calls receive the correct formatting and context across the request.
1// logger.ts 2import pino from 'pino'; 3import { pinoLambdaDestination } from 'pino-lambda'; 4 5const destination = pinoLambdaDestination(); 6export const logger = pino(destination);
1// handler.ts 2import { lambdaRequestTracker } from 'pino-lambda'; 3import { logger } from './logger'; 4 5const withRequest = lambdaRequestTracker(); 6 7async function handler(event, context) { 8 // automatic request tracing across all instances of pino 9 // called once at the beginning of your Lambda handler 10 withRequest(event, context); 11 12 // typical logging methods 13 logger.info({ data: 'Some data' }, 'A log message'); 14}
You can use the this wrapper outside of the AWS lambda function in any place you want. This is especially useful in private npm modules that will be used by your AWS Lambda function. The default logger context is a shared instance, so it inherits all properties the default is configured for, and will emit request information for all logs. This effectively allows you to track a request across its entire set of log entries.
Please see our contributing guide.
Active: Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.
No vulnerabilities found.
No security vulnerabilities found.