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 environment in which the program is executing.

This includes FaaS environment checks, as well as checks for the presence of a container (Docker) and/or orchestrator (Kubernetes).

Since:

  • 2.0.0

Defined Under Namespace

Classes: MissingVariable, TooManyEnvironments, TypeMismatch, ValueTooLong

Constant Summary collapse

DOCKERENV_PATH =

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 name and location of the .dockerenv file that will signal the presence of Docker.

Since:

  • 2.0.0

'/.dockerenv'
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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mongo/server/app_metadata/environment.rb', line 111

def initialize
  @fields = {}
  @error = nil
  @name = detect_environment
  populate_faas_fields
  detect_container
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



105
106
107
# File 'lib/mongo/server/app_metadata/environment.rb', line 105

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



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

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



94
95
96
# File 'lib/mongo/server/app_metadata/environment.rb', line 94

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



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

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



166
167
168
# File 'lib/mongo/server/app_metadata/environment.rb', line 166

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

#containerHash | 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.

Queries the detected container information.

Returns:

  • (Hash | nil)

    the detected container information, or nil if no container was detected.

Since:

  • 2.0.0



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

def container
  fields[:container]
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



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

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



175
176
177
# File 'lib/mongo/server/app_metadata/environment.rb', line 175

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

#present?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 any environment information was able to be detected.

Returns:

  • (true | false)

    if any environment information was detected.

Since:

  • 2.0.0



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

def present?
  @name || fields.any?
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.

Returns:

  • (Hash)

    the detected environment information.

Since:

  • 2.0.0



191
192
193
# File 'lib/mongo/server/app_metadata/environment.rb', line 191

def to_h
  name ? fields.merge(name: name) : fields
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



184
185
186
# File 'lib/mongo/server/app_metadata/environment.rb', line 184

def vercel?
  @name == 'vercel'
end