Class: Mongo::Server::AppMetadata::Environment Private
- Inherits:
-
Object
- Object
- Mongo::Server::AppMetadata::Environment
- 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).
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.
'/.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.
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.
{ '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.
{ 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.
{ '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
-
#error ⇒ String | nil
readonly
private
The error message explaining why a valid FaaS environment was not detected, or nil if no error occurred.
-
#fields ⇒ Hash | nil
readonly
private
The fields describing the detected FaaS environment.
-
#name ⇒ String | nil
readonly
private
The name of the FaaS environment that was detected, or nil if no valid FaaS environment was detected.
Instance Method Summary collapse
-
#aws? ⇒ true | false
private
Queries whether the current environment is a valid AWS Lambda environment.
-
#azure? ⇒ true | false
private
Queries whether the current environment is a valid Azure environment.
-
#container ⇒ Hash | nil
private
Queries the detected container information.
-
#faas? ⇒ true | false
private
Queries whether the current environment is a valid FaaS environment.
-
#gcp? ⇒ true | false
private
Queries whether the current environment is a valid GCP environment.
-
#initialize ⇒ Environment
constructor
private
Create a new AppMetadata::Environment object, initializing it from the current ENV variables.
-
#present? ⇒ true | false
private
Queries whether any environment information was able to be detected.
-
#to_h ⇒ Hash
private
Compiles the detected environment information into a Hash.
-
#vercel? ⇒ true | false
private
Queries whether the current environment is a valid Vercel environment.
Constructor Details
#initialize ⇒ Environment
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}.
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.}" rescue MissingVariable => e self.error = "missing environment variable: #{e.}" rescue TypeMismatch => e self.error = e. rescue ValueTooLong => e self.error = "value for #{e.} is too long" end |
Instance Attribute Details
#error ⇒ String | 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.
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.
105 106 107 |
# File 'lib/mongo/server/app_metadata/environment.rb', line 105 def error @error end |
#fields ⇒ Hash | 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.
98 99 100 |
# File 'lib/mongo/server/app_metadata/environment.rb', line 98 def fields @fields end |
#name ⇒ String | 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.
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.
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.
166 167 168 |
# File 'lib/mongo/server/app_metadata/environment.rb', line 166 def azure? @name == 'azure.func' end |
#container ⇒ Hash | 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.
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.
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.
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.
140 141 142 |
# File 'lib/mongo/server/app_metadata/environment.rb', line 140 def present? @name || fields.any? end |
#to_h ⇒ Hash
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.
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.
184 185 186 |
# File 'lib/mongo/server/app_metadata/environment.rb', line 184 def vercel? @name == 'vercel' end |