Lambda powertool logging for Java

Venkateswaran.N
3 min readOct 17, 2021

Lambda Logging

Thinking of standard logging and adopting best practices for capturing metrics and tracing at a latter stage costs heavy price which should be averted exclusively when modernizing applications in cloud environment.

Observability, for AWS Lambda serverless functions poses a huge challenge for eliminating nonstandard logging across and it should not be an afterthought.

Since we don’t have the control over Lambda as all the underlying complete resources like CPU, Memory etc., are automatically managed when the Lambda gets triggered, it is an absolute necessary to trace, monitor, log and collect the metrics to monitor and trouble shoot .(One potential unnoticed area is, the unnoticed lambda function throttling due to lot of parallel events).

Yet another challenge in Normal logging with Lambda function is to capture the coldstart information and xray tracing. Developers need lot of work to capture and log this information.

Though a lot of third party commercial tools are available, AWS has provided ‘Lambda power tools’ for logging ,metrics and tracing. All these logs and metrics are captured and pumped to cloud watch and can be later streamed to any other Analytics system. Logging provides opinionated logger with JSON structured output.

Lambda powertools are very simple to use and available for various programming Languages (Java, node.js and python at this time of writing).

Key features of Lambda power tool

· Capture key fields from Lambda context, cold start and structures logging output as JSON. Add additional custom fields

· Log Lambda event via annotation paramAppend additional keys to structured log at any point in time(disabled by default, can be enabled explicitly)

. Auto patch supported modules by AWS X-Ray

· Aggregate up to 100 metrics using a single CloudWatch EMF object Validate against common metric definitions mistakes (metric unit, values, max dimensions, max metrics, etc).

· Metrics are created asynchronously by CloudWatch without custom stacks.

· Context manager to create a one off metric with a different dimension.

Install

For Java, Powertools dependencies are available in Maven Central and also in Gradle. You can use your favourite dependency management tool to install it.

All you need in maven is to add the dependencies and plugins in pom.xml file as shown below.

dependencies to be added in pom.xml

and also add the plugins as shown below

Plugins to be added in pom.xml

Thats all to do. Life is so simple.

Sample Java code

simple example

Sample CloudWatch Log output

sample — one of the logstream entry

Sample service map

Service lensmap showing the complete request flow

The service lens map provides the complete traceability of the request which spans since the event triggers which might include all the lambda functions spanned . The service map also shows the errors happening in the manbda function with red color enabling us to dril down to debug and also when placing the mouse on the line or the circle, provides more info pertaining to leg shows the average latency, requests served per minute and the faults per minute if any.

Lambda power tools are available for Python and also for node.js Lambda functions.

--

--

Venkateswaran.N

Lead Architect (Application modernization/Containerization - AWS cloud practice , IBM India )