github.com/Foodji/aws-lambda-go@v1.20.2/lambdacontext/context.go (about) 1 // Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 // 3 // Helpers for accessing context information from an Invoke request. Context information 4 // is stored in a https://golang.org/pkg/context/#Context. The functions FromContext and NewContext 5 // are used to retrieving and inserting an instance of LambdaContext. 6 7 package lambdacontext 8 9 import ( 10 "context" 11 "os" 12 "strconv" 13 ) 14 15 // LogGroupName is the name of the log group that contains the log streams of the current Lambda Function 16 var LogGroupName string 17 18 // LogStreamName name of the log stream that the current Lambda Function's logs will be sent to 19 var LogStreamName string 20 21 // FunctionName the name of the current Lambda Function 22 var FunctionName string 23 24 // MemoryLimitInMB is the configured memory limit for the current instance of the Lambda Function 25 var MemoryLimitInMB int 26 27 // FunctionVersion is the published version of the current instance of the Lambda Function 28 var FunctionVersion string 29 30 func init() { 31 LogGroupName = os.Getenv("AWS_LAMBDA_LOG_GROUP_NAME") 32 LogStreamName = os.Getenv("AWS_LAMBDA_LOG_STREAM_NAME") 33 FunctionName = os.Getenv("AWS_LAMBDA_FUNCTION_NAME") 34 if limit, err := strconv.Atoi(os.Getenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")); err != nil { 35 MemoryLimitInMB = 0 36 } else { 37 MemoryLimitInMB = limit 38 } 39 FunctionVersion = os.Getenv("AWS_LAMBDA_FUNCTION_VERSION") 40 } 41 42 // ClientApplication is metadata about the calling application. 43 type ClientApplication struct { 44 InstallationID string `json:"installation_id"` 45 AppTitle string `json:"app_title"` 46 AppVersionCode string `json:"app_version_code"` 47 AppPackageName string `json:"app_package_name"` 48 } 49 50 // ClientContext is information about the client application passed by the calling application. 51 type ClientContext struct { 52 Client ClientApplication 53 Env map[string]string `json:"env"` 54 Custom map[string]string `json:"custom"` 55 } 56 57 // CognitoIdentity is the cognito identity used by the calling application. 58 type CognitoIdentity struct { 59 CognitoIdentityID string 60 CognitoIdentityPoolID string 61 } 62 63 // LambdaContext is the set of metadata that is passed for every Invoke. 64 type LambdaContext struct { 65 AwsRequestID string 66 InvokedFunctionArn string 67 Identity CognitoIdentity 68 ClientContext ClientContext 69 } 70 71 // An unexported type to be used as the key for types in this package. 72 // This prevents collisions with keys defined in other packages. 73 type key struct{} 74 75 // The key for a LambdaContext in Contexts. 76 // Users of this package must use lambdacontext.NewContext and lambdacontext.FromContext 77 // instead of using this key directly. 78 var contextKey = &key{} 79 80 // NewContext returns a new Context that carries value lc. 81 func NewContext(parent context.Context, lc *LambdaContext) context.Context { 82 return context.WithValue(parent, contextKey, lc) 83 } 84 85 // FromContext returns the LambdaContext value stored in ctx, if any. 86 func FromContext(ctx context.Context) (*LambdaContext, bool) { 87 lc, ok := ctx.Value(contextKey).(*LambdaContext) 88 return lc, ok 89 }