Skip to content

Commit 37549f7

Browse files
committed
stop exposing LogFormat and LogLevel from lambdacontext
1 parent a4e53b2 commit 37549f7

3 files changed

Lines changed: 9 additions & 121 deletions

File tree

lambdacontext/context.go

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +0,0 @@
1-
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2-
3-
// Package lambdacontext provides access to Lambda execution context information.
4-
//
5-
// This package allows Lambda functions to access metadata about the current invocation,
6-
// including request ID, function ARN, Cognito identity, and client context. Context
7-
// information is retrieved from the standard Go context.Context using FromContext().
8-
//
9-
// See https://docs.aws.amazon.com/lambda/latest/dg/golang-context.html
10-
package lambdacontext
11-
12-
import (
13-
"context"
14-
"os"
15-
"strconv"
16-
)
17-
18-
// LogFormat is the log format, either TEXT or JSON (from AWS_LAMBDA_LOG_FORMAT)
19-
var LogFormat string
20-
21-
// LogLevel is the log level for structured logging (from AWS_LAMBDA_LOG_LEVEL). Only available when LogFormat is JSON
22-
var LogLevel string
23-
24-
// LogGroupName is the name of the log group that contains the log streams of the current Lambda Function
25-
var LogGroupName string
26-
27-
// LogStreamName name of the log stream that the current Lambda Function's logs will be sent to
28-
var LogStreamName string
29-
30-
// FunctionName the name of the current Lambda Function
31-
var FunctionName string
32-
33-
// MemoryLimitInMB is the configured memory limit for the current instance of the Lambda Function
34-
var MemoryLimitInMB int
35-
36-
// FunctionVersion is the published version of the current instance of the Lambda Function
37-
var FunctionVersion string
38-
39-
var maxConcurrency int
40-
41-
func init() {
42-
LogFormat = os.Getenv("AWS_LAMBDA_LOG_FORMAT")
43-
LogLevel = os.Getenv("AWS_LAMBDA_LOG_LEVEL")
44-
LogGroupName = os.Getenv("AWS_LAMBDA_LOG_GROUP_NAME")
45-
LogStreamName = os.Getenv("AWS_LAMBDA_LOG_STREAM_NAME")
46-
FunctionName = os.Getenv("AWS_LAMBDA_FUNCTION_NAME")
47-
if limit, err := strconv.Atoi(os.Getenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")); err != nil {
48-
MemoryLimitInMB = 0
49-
} else {
50-
MemoryLimitInMB = limit
51-
}
52-
FunctionVersion = os.Getenv("AWS_LAMBDA_FUNCTION_VERSION")
53-
if v, err := strconv.Atoi(os.Getenv("AWS_LAMBDA_MAX_CONCURRENCY")); err != nil || v < 1 {
54-
maxConcurrency = 1
55-
} else {
56-
maxConcurrency = v
57-
}
58-
}
59-
60-
func MaxConcurrency() int {
61-
return maxConcurrency
62-
}
63-
64-
// ClientApplication is metadata about the calling application.
65-
type ClientApplication struct {
66-
InstallationID string `json:"installation_id"`
67-
AppTitle string `json:"app_title"`
68-
AppVersionCode string `json:"app_version_code"`
69-
AppPackageName string `json:"app_package_name"`
70-
}
71-
72-
// ClientContext is information about the client application passed by the calling application.
73-
type ClientContext struct {
74-
Client ClientApplication
75-
Env map[string]string `json:"env"`
76-
Custom map[string]string `json:"custom"`
77-
}
78-
79-
// CognitoIdentity is the cognito identity used by the calling application.
80-
type CognitoIdentity struct {
81-
CognitoIdentityID string
82-
CognitoIdentityPoolID string
83-
}
84-
85-
// LambdaContext is the set of metadata that is passed for every Invoke.
86-
type LambdaContext struct {
87-
AwsRequestID string //nolint: staticcheck
88-
InvokedFunctionArn string //nolint: staticcheck
89-
Identity CognitoIdentity
90-
ClientContext ClientContext
91-
TenantID string `json:",omitempty"`
92-
}
93-
94-
// An unexported type to be used as the key for types in this package.
95-
// This prevents collisions with keys defined in other packages.
96-
type key struct{}
97-
98-
// The key for a LambdaContext in Contexts.
99-
// Users of this package must use lambdacontext.NewContext and lambdacontext.FromContext
100-
// instead of using this key directly.
101-
var contextKey = &key{}
102-
103-
// NewContext returns a new Context that carries value lc.
104-
func NewContext(parent context.Context, lc *LambdaContext) context.Context {
105-
return context.WithValue(parent, contextKey, lc)
106-
}
107-
108-
// FromContext returns the LambdaContext value stored in ctx, if any.
109-
func FromContext(ctx context.Context) (*LambdaContext, bool) {
110-
lc, ok := ctx.Value(contextKey).(*LambdaContext)
111-
return lc, ok
112-
}

lambdacontext/logger.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
"os"
1212
)
1313

14+
// logFormat is the log format from AWS_LAMBDA_LOG_FORMAT (TEXT or JSON)
15+
var logFormat = os.Getenv("AWS_LAMBDA_LOG_FORMAT")
16+
17+
// logLevel is the log level from AWS_LAMBDA_LOG_LEVEL
18+
var logLevel = os.Getenv("AWS_LAMBDA_LOG_LEVEL")
19+
1420
// field represents a Lambda context field to include in log records.
1521
type field struct {
1622
key string
@@ -58,7 +64,7 @@ func NewLogHandler(opts ...LogOption) slog.Handler {
5864
}
5965

6066
var h slog.Handler
61-
if LogFormat == "JSON" {
67+
if logFormat == "JSON" {
6268
h = slog.NewJSONHandler(os.Stdout, handlerOpts)
6369
} else {
6470
h = slog.NewTextHandler(os.Stdout, handlerOpts)
@@ -130,7 +136,7 @@ func (h *lambdaHandler) WithGroup(name string) slog.Handler {
130136
}
131137

132138
func parseLogLevel() slog.Level {
133-
switch LogLevel {
139+
switch logLevel {
134140
case "DEBUG":
135141
return slog.LevelDebug
136142
case "INFO":

lambdacontext/logger_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestParseLogLevel(t *testing.T) {
8181

8282
for _, tt := range tests {
8383
t.Run(tt.name, func(t *testing.T) {
84-
LogLevel = tt.input
84+
logLevel = tt.input
8585
result := parseLogLevel()
8686
assert.Equal(t, tt.expected, result)
8787
})
@@ -386,17 +386,11 @@ func TestWithTenantID(t *testing.T) {
386386
}
387387

388388
func TestNewLogger(t *testing.T) {
389-
LogFormat = "JSON"
390-
LogLevel = "INFO"
391-
392389
logger := NewLogger()
393390
assert.NotNil(t, logger)
394391
}
395392

396393
func TestNewLogHandler(t *testing.T) {
397-
LogFormat = "JSON"
398-
LogLevel = "INFO"
399-
400394
handler := NewLogHandler()
401395
assert.NotNil(t, handler)
402396

0 commit comments

Comments
 (0)