|
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 | | -} |
0 commit comments