lib/fozzie/configuration.rb in fozzie-0.0.27 vs lib/fozzie/configuration.rb in fozzie-1.0.0

- old
+ new

@@ -1,42 +1,60 @@ require 'yaml' -require 'core_ext/hash' +require 'facets/hash/symbolize_keys' require 'sys/uname' require 'timeout' module Fozzie # Fozzie configuration allows assignment of global properties # that will be used within the Fozzie codebase. + class Configuration include Sys + extend Forwardable - attr_accessor :env, :config_path, :host, :port, :appname, :namespaces, :timeout, :monitor_classes, :sniff_envs, :ignore_prefix, :prefix + def_delegators :adapter, :delimeter, :safe_separator + attr_accessor :env, :config_path, :host, :port, :appname, :namespaces, + :timeout, :monitor_classes, :sniff_envs, :ignore_prefix, :prefix + def initialize(args = {}) merge_and_assign_config(args) + self.adapter self.origin_name end + def adapter=(adapter) + @adapter = eval("Fozzie::Adapter::#{adapter}").new + rescue NoMethodError + raise AdapterMissing, "Adapter could not be found for given provider #{@provider}" + end + + def adapter + @adapter || default_configuration[:adapter] + end + def disable_prefix @ignore_prefix = true end # Returns the prefix for any stat requested to be registered def data_prefix return nil if @ignore_prefix return @data_prefix if @data_prefix - data_prefix = @prefix.collect do |me| - (me.kind_of?(Symbol) && self.respond_to?(me.to_sym) ? self.send(me) : me.to_s) - end + escaped_prefix_with_dynamically_resolved_parts = prefix.map do |part| + resolved_part = (part.kind_of?(Symbol) && self.respond_to?(part) ? self.send(part) : part.to_s) + escaped_resolved_part = resolved_part.gsub(delimeter, safe_separator) + escaped_resolved_part == "" ? nil : escaped_resolved_part + end.compact - data_prefix = data_prefix.collect do |s| - s.empty? ? nil : s.gsub(Socket::DELIMETER, '-') - end.compact.join(Socket::DELIMETER).strip - - @data_prefix ||= (data_prefix.empty? ? nil : data_prefix) + @data_prefix = if escaped_prefix_with_dynamically_resolved_parts.any? + escaped_prefix_with_dynamically_resolved_parts.join(delimeter).strip + else + nil + end end # Returns the origin name of the current machine to register the stat against def origin_name @origin_name ||= Uname.nodename @@ -68,11 +86,12 @@ :appname => '', :namespaces => %w{Stats S Statistics Warehouse}, :timeout => 0.5, :monitor_classes => [], :sniff_envs => [:development, :staging, :production], - :ignore_prefix => false + :ignore_prefix => false, + :adapter => :Statsd }.dup end # Loads the configuration from YAML, if possible def config_from_yaml(args) @@ -87,6 +106,6 @@ File.expand_path('config/fozzie.yml', path) end end -end \ No newline at end of file +end