context.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 isntance of LambdaContext.
  6. package lambdacontext
  7. import (
  8. "context"
  9. "os"
  10. "strconv"
  11. )
  12. // LogGroupName is the name of the log group that contains the log streams of the current Lambda Function
  13. var LogGroupName string
  14. // LogStreamName name of the log stream that the current Lambda Function's logs will be sent to
  15. var LogStreamName string
  16. // FunctionName the name of the current Lambda Function
  17. var FunctionName string
  18. // MemoryLimitInMB is the configured memory limit for the current instance of the Lambda Function
  19. var MemoryLimitInMB int
  20. // FunctionVersion is the published version of the current instance of the Lambda Function
  21. var FunctionVersion string
  22. func init() {
  23. LogGroupName = os.Getenv("AWS_LAMBDA_LOG_GROUP_NAME")
  24. LogStreamName = os.Getenv("AWS_LAMBDA_LOG_STREAM_NAME")
  25. FunctionName = os.Getenv("AWS_LAMBDA_FUNCTION_NAME")
  26. if limit, err := strconv.Atoi(os.Getenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")); err != nil {
  27. MemoryLimitInMB = 0
  28. } else {
  29. MemoryLimitInMB = limit
  30. }
  31. FunctionVersion = os.Getenv("AWS_LAMBDA_FUNCTION_VERSION")
  32. }
  33. // ClientApplication is metadata about the calling application.
  34. type ClientApplication struct {
  35. InstallationID string `json:"installation_id"`
  36. AppTitle string `json:"app_title"`
  37. AppVersionCode string `json:"app_version_code"`
  38. AppPackageName string `json:"app_package_name"`
  39. }
  40. // ClientContext is information about the client application passed by the calling application.
  41. type ClientContext struct {
  42. Client ClientApplication
  43. Env map[string]string `json:"env"`
  44. Custom map[string]string `json:"custom"`
  45. }
  46. // CognitoIdentity is the cognito identity used by the calling application.
  47. type CognitoIdentity struct {
  48. CognitoIdentityID string
  49. CognitoIdentityPoolID string
  50. }
  51. // LambdaContext is the set of metadata that is passed for every Invoke.
  52. type LambdaContext struct {
  53. AwsRequestID string
  54. InvokedFunctionArn string
  55. Identity CognitoIdentity
  56. ClientContext ClientContext
  57. }
  58. // An unexported type to be used as the key for types in this package.
  59. // This prevents collisions with keys defined in other packages.
  60. type key struct{}
  61. // The key for a LambdaContext in Contexts.
  62. // Users of this package must use lambdacontext.NewContext and lambdacontext.FromContext
  63. // instead of using this key directly.
  64. var contextKey = &key{}
  65. // NewContext returns a new Context that carries value lc.
  66. func NewContext(parent context.Context, lc *LambdaContext) context.Context {
  67. return context.WithValue(parent, contextKey, lc)
  68. }
  69. // FromContext returns the LambdaContext value stored in ctx, if any.
  70. func FromContext(ctx context.Context) (*LambdaContext, bool) {
  71. lc, ok := ctx.Value(contextKey).(*LambdaContext)
  72. return lc, ok
  73. }