class Datadog::Statsd::ConnectionCfg

Constants

DEFAULT_HOST
DEFAULT_PORT
ERROR_MESSAGE
UDP_PREFIX
UDS_PREFIX

Attributes

host[R]
port[R]
socket_path[R]
transport_type[R]

Public Class Methods

new(host: nil, port: nil, socket_path: nil) click to toggle source
# File lib/datadog/statsd/connection_cfg.rb, line 9
def initialize(host: nil, port: nil, socket_path: nil)
  initialize_with_constructor_args(host: host, port: port, socket_path: socket_path) ||
    initialize_with_env_vars ||
    initialize_with_defaults
end

Public Instance Methods

make_connection(**params) click to toggle source
# File lib/datadog/statsd/connection_cfg.rb, line 15
def make_connection(**params)
  case @transport_type
  when :udp
    UDPConnection.new(@host, @port, **params)
  when :uds
    UDSConnection.new(@socket_path, **params)
  end
end

Private Instance Methods

initialize_with_constructor_args(host: nil, port: nil, socket_path: nil) click to toggle source
# File lib/datadog/statsd/connection_cfg.rb, line 42
def initialize_with_constructor_args(host: nil, port: nil, socket_path: nil)
  try_initialize_with(host: host, port: port, socket_path: socket_path,
    error_message: 
      "Both UDP: (host/port #{host}:#{port}) and UDS (socket_path #{socket_path}) " +
      "constructor arguments were given. Use only one or the other.",
    )
end
initialize_with_defaults() click to toggle source
# File lib/datadog/statsd/connection_cfg.rb, line 60
def initialize_with_defaults()
  try_initialize_with(host: DEFAULT_HOST, port: DEFAULT_PORT)
end
initialize_with_env_vars() click to toggle source
# File lib/datadog/statsd/connection_cfg.rb, line 50
def initialize_with_env_vars()
  try_initialize_with(
    dogstatsd_url: ENV['DD_DOGSTATSD_URL'],
    host: ENV['DD_AGENT_HOST'],
    port: ENV['DD_DOGSTATSD_PORT'] && ENV['DD_DOGSTATSD_PORT'].to_i,
    socket_path: ENV['DD_DOGSTATSD_SOCKET'],
    error_message: ERROR_MESSAGE,
  )
end
parse_dogstatsd_url(str:) click to toggle source
# File lib/datadog/statsd/connection_cfg.rb, line 90
def parse_dogstatsd_url(str:)
  # udp socket connection

  if str.start_with?(UDP_PREFIX)
    dogstatsd_url = str[UDP_PREFIX.size..str.size]
    host = nil
    port = nil

    if dogstatsd_url.include?(":")
      parts = dogstatsd_url.split(":")
      if parts.size > 2
        raise ArgumentError, "Error: DD_DOGSTATSD_URL wrong format for an UDP connection. E.g. 'udp://localhost:8125'"
      end

      host = parts[0]
      port = parts[1].to_i
    else
      host = dogstatsd_url
    end

    return host, port, nil
  end

  # unix socket connection

  if str.start_with?(UDS_PREFIX)
    return nil, nil, str[UDS_PREFIX.size..str.size]
  end

  # malformed value

  raise ArgumentError, "Error: DD_DOGSTATSD_URL has been provided but is not starting with 'udp://' nor 'unix://'"
end
try_initialize_with(dogstatsd_url: nil, host: nil, port: nil, socket_path: nil, error_message: ERROR_MESSAGE) click to toggle source
# File lib/datadog/statsd/connection_cfg.rb, line 64
def try_initialize_with(dogstatsd_url: nil, host: nil, port: nil, socket_path: nil, error_message: ERROR_MESSAGE)
  if (host || port) && socket_path
    raise ArgumentError, error_message
  end

  if dogstatsd_url
    host, port, socket_path = parse_dogstatsd_url(str: dogstatsd_url.to_s)
  end

  if host || port 
    @host = host || DEFAULT_HOST
    @port = port || DEFAULT_PORT
    @socket_path = nil
    @transport_type = :udp
    return true
  elsif socket_path
    @host = nil
    @port = nil
    @socket_path = socket_path
    @transport_type = :uds
    return true
  end

  return false
end