Skip to content

Commit bab9a9b

Browse files
committed
feat: ✨ allow some environment variables to be logged as attributes
1 parent 0fa4bb1 commit bab9a9b

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ This will produce the following log statement in CloudWatch Logs:
7272
}
7373
```
7474

75+
### Adding environment variables as attributes to the log handler
76+
77+
You might want to add some environment variables as attributes to the log handler.
78+
Typically you would want to add some environment variables that will help you identify the specific deployment of your lambda function.
79+
You can achieve this by passing the environment variable names as arguments to the `NewAWSLambdaHandler` function.
80+
81+
```golang
82+
slog.SetDefault(slog.New(slogawslambda.NewAWSLambdaHandler(ctx, nil, "ENVIRONMENT", "REGION")))
83+
```
84+
85+
This will produce the following log statement in CloudWatch Logs:
86+
```json
87+
{
88+
"time": "2024-06-18T21:36:21.128059573Z",
89+
"level": "INFO",
90+
"msg": "Successfully downloaded configuration file from S3",
91+
"environment": "production",
92+
"region": "eu-west-1",
93+
"function_arn": "arn:aws:lambda:eu-west-1:01234567890:function:myawesomefunction-chN9TDColFt1",
94+
"request_id": "bdb96c48-c3bc-462d-abbd-6b56e6cb3050"
95+
}
96+
```
7597

7698
### Changing the key for request ID or function ARN
7799

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/jbleduigou/slog-aws-lambda
33
go 1.21
44

55
require (
6-
github.com/aws/aws-lambda-go v1.46.0
6+
github.com/aws/aws-lambda-go v1.47.0
77
github.com/stretchr/testify v1.9.0
88
)
99

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/aws/aws-lambda-go v1.46.0 h1:UWVnvh2h2gecOlFhHQfIPQcD8pL/f7pVCutmFl+oXU8=
2-
github.com/aws/aws-lambda-go v1.46.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
1+
github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI=
2+
github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

handler.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"log/slog"
66
"os"
7+
"strings"
78

89
"github.com/aws/aws-lambda-go/lambdacontext"
910
)
@@ -12,7 +13,11 @@ type LambdaHandler struct {
1213
slog.JSONHandler
1314
}
1415

15-
func NewAWSLambdaHandler(ctx context.Context, opts *slog.HandlerOptions) slog.Handler {
16+
// NewAWSLambdaHandler creates a new AWS Lambda handler
17+
// ctx is the context of the lambda function
18+
// opts are the options for the handler
19+
// envVars is an optional list of environment variables to add to the handler
20+
func NewAWSLambdaHandler(ctx context.Context, opts *slog.HandlerOptions, envVars ...string) slog.Handler {
1621
if opts == nil {
1722
opts = &slog.HandlerOptions{}
1823
}
@@ -33,9 +38,23 @@ func NewAWSLambdaHandler(ctx context.Context, opts *slog.HandlerOptions) slog.Ha
3338
arn := lc.InvokedFunctionArn
3439

3540
// Create the Handler using the attributes from lambda context
36-
return slog.NewJSONHandler(os.Stdout, opts).
41+
h := slog.NewJSONHandler(os.Stdout, opts).
3742
WithAttrs([]slog.Attr{slog.String("function_arn", arn)}).
3843
WithAttrs([]slog.Attr{slog.String("request_id", requestID)})
44+
45+
if len(envVars) == 0 {
46+
return h
47+
}
48+
49+
// Add the environment variables to the handler as attributes
50+
for _, name := range envVars {
51+
val, found := os.LookupEnv(name)
52+
if found && len(name) > 0 {
53+
h = h.WithAttrs([]slog.Attr{slog.String(strings.ToLower(name), val)})
54+
}
55+
}
56+
57+
return h
3958
}
4059

4160
func getLogLevel() slog.Leveler {

0 commit comments

Comments
 (0)