Class: TreasureData::Logger::Agent::Rails::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/td/logger/agent/rails/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ Config

Returns a new instance of Config.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/td/logger/agent/rails/config.rb', line 6

def initialize(conf)
  if agent = conf['agent']
    host, port = agent.split(':',2)
    port = (port || 24224).to_i
    @agent_host = host
    @agent_port = port

    @tag = conf['tag']
    @tag ||= conf['database']
    raise "'tag' nor 'database' options are not set" unless @tag

  else
    @apikey = conf['apikey']
    raise "'apikey' option is not set" unless @apikey

    @database = conf['database']
    raise "'database' option is not set" unless @database

    if conf.has_key?('auto_create_table')
      @auto_create_table = !!conf['auto_create_table']
    else
      @auto_create_table = true
    end

    @debug_mode = !!conf['debug_mode']
  end

  @access_log_table = conf['access_log_table']
end

Instance Attribute Details

#access_log_tableObject (readonly)

Returns the value of attribute access_log_table.



38
39
40
# File 'lib/td/logger/agent/rails/config.rb', line 38

def access_log_table
  @access_log_table
end

#agent_hostObject (readonly)

Returns the value of attribute agent_host.



36
37
38
# File 'lib/td/logger/agent/rails/config.rb', line 36

def agent_host
  @agent_host
end

#agent_portObject (readonly)

Returns the value of attribute agent_port.



36
37
38
# File 'lib/td/logger/agent/rails/config.rb', line 36

def agent_port
  @agent_port
end

#apikeyObject (readonly)

Returns the value of attribute apikey.



37
38
39
# File 'lib/td/logger/agent/rails/config.rb', line 37

def apikey
  @apikey
end

#auto_create_tableObject (readonly)

Returns the value of attribute auto_create_table.



37
38
39
# File 'lib/td/logger/agent/rails/config.rb', line 37

def auto_create_table
  @auto_create_table
end

#databaseObject (readonly)

Returns the value of attribute database.



37
38
39
# File 'lib/td/logger/agent/rails/config.rb', line 37

def database
  @database
end

#debug_modeObject (readonly)

Returns the value of attribute debug_mode.



38
39
40
# File 'lib/td/logger/agent/rails/config.rb', line 38

def debug_mode
  @debug_mode
end

#tagObject (readonly)

Returns the value of attribute tag.



36
37
38
# File 'lib/td/logger/agent/rails/config.rb', line 36

def tag
  @tag
end

Class Method Details

.initObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/td/logger/agent/rails/config.rb', line 48

def self.init
  logger = ::Logger.new(STDERR)

  if CONFIG_PATH[0] == ?/
    config_path = CONFIG_PATH
  else
    config_path = "#{::Rails.root}/#{CONFIG_PATH}"
  end

  if File.exist?(config_path)
    load_file(logger, config_path)
  elsif File.exist?("#{::Rails.root}/#{CONFIG_PATH_EY_DEPLOY}")
    load_file_ey(logger, "#{::Rails.root}/#{CONFIG_PATH_EY_DEPLOY}")
  elsif File.exist?("#{::Rails.root}/#{CONFIG_PATH_EY_LOCAL}")
    load_file_ey(logger, "#{::Rails.root}/#{CONFIG_PATH_EY_LOCAL}")
  else
    load_env(logger)
  end
end

.load_env(logger) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/td/logger/agent/rails/config.rb', line 127

def self.load_env(logger)
  apikey = ENV['TREASURE_DATA_API_KEY'] || ENV['TD_API_KEY']

  unless apikey
    logger.warn "WARNING: #{CONFIG_PATH} does not exist."
    logger.warn "WARNING: Disabling Treasure Data event logger."
    return nil
  end

  return Config.new({
    'apikey' => apikey,
    'database' => ENV['TREASURE_DATA_DB'] || "rails_#{::Rails.env}",
    'access_log_table' => ENV['TREASURE_DATA_ACCESS_LOG_TABLE'],
    'auto_create_table' => true
  })
end

.load_file(logger, path = "#{::Rails.root}/#{CONFIG_PATH}") ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/td/logger/agent/rails/config.rb', line 68

def self.load_file(logger, path="#{::Rails.root}/#{CONFIG_PATH}")
  require 'yaml'
  require 'erb'

  begin
    src = File.read(path)
    yaml = ERB.new(src).result
    env_conf = YAML.load(yaml)
  rescue
    logger.warn "WARNING: Can't load #{path} file: #{$!}"
    logger.warn "WARNING: Disabling Treasure Data event logger."
    return nil
  end

  conf = env_conf[::Rails.env] if env_conf.is_a?(Hash)
  unless conf
    logger.warn "WARNING: #{path} doesn't include setting for current environment (#{::Rails.env})."
    logger.warn "WARNING: Disabling Treasure Data event logger."
    return nil
  end

  begin
    return Config.new(conf)
  rescue
    logger.warn "WARNING: #{path}: #{$!}."
    logger.warn "WARNING: Disabling Treasure Data event logger."
    return nil
  end
end

.load_file_ey(logger, path) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/td/logger/agent/rails/config.rb', line 98

def self.load_file_ey(logger, path)
  require 'yaml'
  require 'erb'

  begin
    src = File.read(path)
    yaml = ERB.new(src).result
    env_conf = YAML.load(yaml)
  rescue
    logger.warn "WARNING: Can't load #{path} file: #{$!}"
    logger.warn "WARNING: Disabling Treasure Data event logger."
    return nil
  end

  apikey = env_conf['td']['TREASURE_DATA_API_KEY'] if env_conf.is_a?(Hash) and env_conf['td'].is_a?(Hash)
  unless apikey
    logger.warn "WARNING: #{path} does not have a configuration of API key."
    logger.warn "WARNING: Disabling Treasure Data event logger."
    return nil
  end

  return Config.new({
    'apikey' => apikey,
    'database' => ENV['TREASURE_DATA_DB'] || "rails_#{::Rails.env}",
    'access_log_table' => ENV['TREASURE_DATA_ACCESS_LOG_TABLE'],
    'auto_create_table' => true
  })
end

Instance Method Details

#access_log_enabled?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/td/logger/agent/rails/config.rb', line 44

def access_log_enabled?
  !@access_log_table.nil? && !@access_log_table.empty?
end

#agent_mode?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/td/logger/agent/rails/config.rb', line 40

def agent_mode?
  @agent_host != nil
end