Class: Mongo::Server::AppMetadata::Environment Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/server/app_metadata/environment.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Implements the logic from the handshake spec, for deducing and reporting the current FaaS environment in which the program is executing.

Since:

  • 2.0.0

Defined Under Namespace

Classes: MissingVariable, TooManyEnvironments, TypeMismatch, ValueTooLong

Constant Summary collapse

MAXIMUM_VALUE_LENGTH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

This value is not explicitly specified in the spec, only implied to be less than 512.

Since:

  • 2.0.0

500
DISCRIMINATORS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The mapping that determines which FaaS environment is active, based on which environment variable(s) are present.

Since:

  • 2.0.0

{
  'AWS_EXECUTION_ENV' => { pattern: /^AWS_Lambda_/, name: 'aws.lambda' },
  'AWS_LAMBDA_RUNTIME_API' => { name: 'aws.lambda' },
  'FUNCTIONS_WORKER_RUNTIME' => { name: 'azure.func' },
  'K_SERVICE' => { name: 'gcp.func' },
  'FUNCTION_NAME' => { name: 'gcp.func' },
  'VERCEL' => { name: 'vercel' },
}.freeze
COERCIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Describes how to coerce values of the specified type.

Since:

  • 2.0.0

{
  string: ->(v) { String(v) },
  integer: ->(v) { Integer(v) }
}.freeze
FIELDS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Describes which fields are required for each FaaS environment, along with their expected types, and how they should be named in the handshake document.

Since:

  • 2.0.0

{
  'aws.lambda' => {
    'AWS_REGION' => { field: :region, type: :string },
    'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' => { field: :memory_mb, type: :integer },
  },

  'azure.func' => {},

  'gcp.func' => {
    'FUNCTION_MEMORY_MB' => { field: :memory_mb, type: :integer },
    'FUNCTION_TIMEOUT_SEC' => { field: :timeout_sec, type: :integer },
    'FUNCTION_REGION' => { field: :region, type: :string },
  },

  'vercel' => {
    'VERCEL_REGION' => { field: :region, type: :string },
  },
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new AppMetadata::Environment object, initializing it from the current ENV variables. If no FaaS environment is detected, or if the environment contains invalid or contradictory state, it will be initialized with {name} set to {nil}.

Since:

  • 2.0.0



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mongo/server/app_metadata/environment.rb', line 104

def initialize
  @error = nil
  @name = detect_environment
  populate_fields
rescue TooManyEnvironments => e
  self.error = "too many environments detected: #{e.message}"
rescue MissingVariable => e
  self.error = "missing environment variable: #{e.message}"
rescue TypeMismatch => e
  self.error = e.message
rescue ValueTooLong => e
  self.error = "value for #{e.message} is too long"
end

Instance Attribute Details

#errorString | nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

These error messagess are not to be propogated to the user; they are intended only for troubleshooting and debugging.)

Returns the error message explaining why a valid FaaS environment was not detected, or nil if no error occurred.

Returns:

  • (String | nil)

    the error message explaining why a valid FaaS environment was not detected, or nil if no error occurred.

Since:

  • 2.0.0



98
99
100
# File 'lib/mongo/server/app_metadata/environment.rb', line 98

def error
  @error
end

#fieldsHash | nil (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the fields describing the detected FaaS environment.

Returns:

  • (Hash | nil)

    the fields describing the detected FaaS environment.

Since:

  • 2.0.0



91
92
93
# File 'lib/mongo/server/app_metadata/environment.rb', line 91

def fields
  @fields
end

#nameString | nil (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the name of the FaaS environment that was detected, or nil if no valid FaaS environment was detected.

Returns:

  • (String | nil)

    the name of the FaaS environment that was detected, or nil if no valid FaaS environment was detected.

Since:

  • 2.0.0



87
88
89
# File 'lib/mongo/server/app_metadata/environment.rb', line 87

def name
  @name
end

Instance Method Details

#aws?true | false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Queries whether the current environment is a valid AWS Lambda environment.

Returns:

  • (true | false)

    whether the environment is a AWS Lambda environment or not.

Since:

  • 2.0.0



131
132
133
# File 'lib/mongo/server/app_metadata/environment.rb', line 131

def aws?
  @name == 'aws.lambda'
end

#azure?true | false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Queries whether the current environment is a valid Azure environment.

Returns:

  • (true | false)

    whether the environment is a Azure environment or not.

Since:

  • 2.0.0



140
141
142
# File 'lib/mongo/server/app_metadata/environment.rb', line 140

def azure?
  @name == 'azure.func'
end

#faas?true | false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Queries whether the current environment is a valid FaaS environment.

Returns:

  • (true | false)

    whether the environment is a FaaS environment or not.

Since:

  • 2.0.0



122
123
124
# File 'lib/mongo/server/app_metadata/environment.rb', line 122

def faas?
  @name != nil
end

#gcp?true | false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Queries whether the current environment is a valid GCP environment.

Returns:

  • (true | false)

    whether the environment is a GCP environment or not.

Since:

  • 2.0.0



149
150
151
# File 'lib/mongo/server/app_metadata/environment.rb', line 149

def gcp?
  @name == 'gcp.func'
end

#to_hHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compiles the detected environment information into a Hash. It will always include a {name} key, but may include other keys as well, depending on the detected FaaS environment. (See the handshake spec for details.)

Returns:

  • (Hash)

    the detected environment information.

Since:

  • 2.0.0



168
169
170
# File 'lib/mongo/server/app_metadata/environment.rb', line 168

def to_h
  fields.merge(name: name)
end

#vercel?true | false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Queries whether the current environment is a valid Vercel environment.

Returns:

  • (true | false)

    whether the environment is a Vercel environment or not.

Since:

  • 2.0.0



158
159
160
# File 'lib/mongo/server/app_metadata/environment.rb', line 158

def vercel?
  @name == 'vercel'
end