slogawslambda

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 4 Imported by: 2

README

slog: AWS Lambda handler

Go Version GoDoc Build Status Go report Contributors License

An AWS Lambda Function Handler for slog Go library.
The idea is to provide a log handler that will use the attributes present in the lambda context.
Also the handler is using Json format so that it can leverage the features offered by AWS CloudWatch.

🚀 Install

go get github.com/jbleduigou/slog-aws-lambda

Compatibility: go >= 1.21

💡 Usage

GoDoc: https://pkg.go.dev/github.com/jbleduigou/slog-aws-lambda

The handler will log the following attributes by default:

  • request-id: the unique request ID for the lambda function invocation.
  • function-arn: the Amazon Resource Name (ARN) that's used to invoke the function, indicates if the invoker specified a version number or alias.
Using the handler as default handler
package main

import (
	"context"
	"log/slog"

	"github.com/aws/aws-lambda-go/events"
	slogawslambda "github.com/jbleduigou/slog-aws-lambda"
)

func handleS3Event(ctx context.Context, s3Event events.S3Event) {
	slog.SetDefault(slog.New(slogawslambda.NewAWSLambdaHandler(ctx, nil)))
	//rest of the business logic
}
Logging a message with a few attributes

Once the handler is defined as default, there is no additional configuration needed.
We can start using the logger by directly using the slog package.

import "log/slog"

slog.Info("Successfully downloaded configuration file from S3", "bucket", bucket, "object-key", objectKey)
Changing the key for request ID or function ARN

The default case for the attributes is dash-case, i.e. we are using request-id and function-arn.
If you want to switch to another case you can use the ReplaceAttr mechanism provided by the slog package.
This way allows not only to switch to camel case but also renaming altogether the attribute.

slog.SetDefault(slog.New(slogawslambda.NewAWSLambdaHandler(ctx, &slog.HandlerOptions{
    ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
		// change request ID to camel case
        if a.Key == "request-id" {
            a.Key = "requestID"
            return a
        }
        // change the name of the attribute for the invoked function ARN
        if a.Key == "function-arn" {
            a.Key = "invokedFunctionARN"
            return a
        }
        return a
    },
})))
Stop logging the function ARN

You might also want to stop logging the function ARN.
For instance if you are not using it and want to save cost on your CloudWatch logs.

slog.SetDefault(slog.New(slogawslambda.NewAWSLambdaHandler(ctx, &slog.HandlerOptions{
    ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
        if a.Key == "request-id" {
            // don't log function ARN
			return slog.Attr{}
        }
        return a
    },
})))

📓 Why should we want to log request ID and function ARN

Embracing serverless comes with scalability and resilience benefits, but logging across a distributed system can be challenging.
AWS generates a unique request ID for each Lambda execution, serving as a critical identifier for tracing requests and facilitating troubleshooting.

Implementing this logging strategy involves retrieving the request ID from lambdacontext, which is exactly what this handler does.

Once this is configured it becomes easier to retrieve all the logs for a specific execution, whether you use the AWS Console or a third party tool like DataDog or SumoLogic.

🤝 Contributing

Feel free to contribute to this project, either my opening issues or submitting pull requests.
Don't hesitate to contact me, by sending me a PM on LinkedIn.

👤 Contributors

The only contributor so far is me, Jean-Baptiste Le Duigou. Feel free to check my blog to about my other projects: http://www.jbleduigou.com

💫 Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

📝 License

This project is MIT licensed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAWSLambdaHandler

func NewAWSLambdaHandler(ctx context.Context, opts *slog.HandlerOptions) slog.Handler

Types

type LambdaHandler

type LambdaHandler struct {
	slog.JSONHandler
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL