123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Server::AppMetadata Private

Relationships & Source Files
Namespace Children
Classes:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Inherits: Object
Defined in: lib/mongo/server/app_metadata.rb,
lib/mongo/server/app_metadata/environment.rb,
lib/mongo/server/app_metadata/platform.rb,
lib/mongo/server/app_metadata/truncator.rb

Overview

Application metadata that is sent to the server during a handshake, when a new connection is established.

Since:

  • 2.0.0

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(options = {}) ⇒ AppMetadata

Instantiate the new AppMetadata object.

Examples:

Instantiate the app metadata.

Mongo::Server::AppMetadata.new(options)

Parameters:

  • options (Hash) (defaults to: {})

    Metadata options.

Options Hash (options):

  • :app_name (String, Symbol)

    Application name that is printed to the mongod logs upon establishing a connection

  • :auth_mech (Symbol)

    The authentication mechanism to use. One of :mongodb_cr, :mongodb_x509, :plain, :scram, :scram256

  • :auth_source (String)

    The source to authenticate from.

  • :compressors (Array<String>)

    A list of potential compressors to use, in order of preference. The driver chooses the first compressor that is also supported by the server. Currently the driver only supports 'zstd', 'snappy' and 'zlib'.

  • :platform (String)

    AppMetadata::Platform information to include in the metadata printed to the mongod logs upon establishing a connection

  • :purpose (Symbol)

    The purpose of this connection.

  • :server_api (Hash)

    The requested server API version. This hash can have the following items:

    • :version -- string
    • :strict -- boolean
    • :deprecation_errors -- boolean
  • :user (String)

    The user name.

  • :wrapping_libraries (Array<Hash>)

    Information about libraries such as ODMs that are wrapping the driver. Specify the lower level libraries first. Allowed hash keys: :name, :version, :platform.

Since:

  • 2.4.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 72

def initialize(options = {})
  @app_name = options[:app_name].to_s if options[:app_name]
  @platform = options[:platform]

  @purpose = check_purpose!(options[:purpose])

  @compressors = options[:compressors] || []
  @wrapping_libraries = options[:wrapping_libraries]
  @server_api = options[:server_api]

  return unless options[:user] && !options[:auth_mech]

  auth_db = options[:auth_source] || 'admin'
  @request_auth_mech = "#{auth_db}.#{options[:user]}"
end

Instance Attribute Details

#platformString (readonly)

Returns:

  • (String)

    The platform information given when the object was instantiated.

Since:

  • 2.0.0

[ GitHub ]

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

attr_reader :platform

#purposeSymbol (readonly)

Returns:

  • (Symbol)

    The purpose of the connection for which this app metadata is created.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 90

attr_reader :purpose

#server_apiHash | nil (readonly)

Returns:

  • (Hash | nil)

    The requested server API version.

    This hash can have the following items:

    • :version -- string
    • :strict -- boolean
    • :deprecation_errors -- boolean

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 102

attr_reader :server_api

#wrapping_librariesArray<Hash> | nil (readonly)

Returns:

  • (Array<Hash> | nil)

    Information about libraries wrapping the driver.

Since:

  • 2.0.0

[ GitHub ]

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

attr_reader :wrapping_libraries

Instance Method Details

#architecture (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 211

def architecture
  RbConfig::CONFIG['target_cpu']
end

#check_purpose!(purpose) ⇒ String | nil (private)

Verifies that the given purpose is either nil, or is one of the allowed purposes.

Parameters:

  • purpose (String | nil)

    The purpose to validate

Returns:

  • (String | nil)

    the {purpose} argument

Raises:

  • (ArgumentError)

    if the purpose is invalid

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 227

def check_purpose!(purpose)
  return purpose unless purpose && !PURPOSES.include?(purpose)

  raise ArgumentError, "Invalid purpose: #{purpose}"
end

#client_documentBSON::Document

Get BSON::Document to be used as value for client key in handshake document.

Returns:

  • (BSON::Document)

    Document describing client for handshake.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 126

def client_document
  @client_document ||=
    BSON::Document.new.tap do |doc|
      doc[:application] = { name: @app_name } if @app_name
      doc[:driver] = driver_doc
      doc[:os] = os_doc
      doc[:platform] = platform_string
      doc[:backpressure] = true
      env_doc.tap { |env| doc[:env] = env if env }
    end
end

#documentBSON::Document (private)

Get the metadata as BSON::Document to be sent to as part of the handshake. The document should be appended to a suitable handshake command.

Returns:

  • (BSON::Document)

    Document for connection's handshake.

Since:

  • 2.0.0

[ GitHub ]

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

def document
  @document ||= begin
    client = Truncator.new(client_document).document
    BSON::Document.new(compression: @compressors, client: client).tap do |doc|
      doc[:saslSupportedMechs] = @request_auth_mech if @request_auth_mech
      doc.update(Utils.transform_server_api(@server_api)) if @server_api
    end
  end
end

#driver_doc (private)

Since:

  • 2.0.0

[ GitHub ]

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

def driver_doc
  names = [ DRIVER_NAME ]
  versions = [ Mongo::VERSION ]
  wrapping_libraries&.each do |library|
    names << (library[:name] || '')
    versions << (library[:version] || '')
  end

  {
    name: names.join('|'),
    version: versions.join('|'),
  }
end

#env_docHash | nil (private)

Returns the environment doc describing the current execution environment.

Returns:

  • (Hash | nil)

    the environment doc (or nil if no relevant environment info was detected)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 194

def env_doc
  env = Environment.new
  env.present? ? env.to_h : nil
end

#name (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 207

def name
  RbConfig::CONFIG['host_os']
end

#os_doc (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 181

def os_doc
  {
    type: type,
    name: name,
    architecture: architecture,
  }
end

#platform_string (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 215

def platform_string
  Platform.new(self).to_s
end

#type (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 199

def type
  if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
    RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase
  else
    'unknown'
  end
end

#validate! (private)

Check whether it is possible to build a valid app metadata document with params provided on initialization.

Raises:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 144

def validate!
  if @app_name && @app_name.bytesize > MAX_APP_NAME_SIZE
    raise Error::InvalidApplicationName.new(@app_name, MAX_APP_NAME_SIZE)
  end

  true
end

#validated_documentBSON::Document

Get the metadata as BSON::Document to be sent to as part of the handshake. The document should be appended to a suitable handshake command.

This method ensures that the metadata are valid.

Returns:

  • (BSON::Document)

    Valid document for connection's handshake.

Raises:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata.rb', line 117

def validated_document
  validate!
  document
end