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

#initializeConfig

Returns a new instance of Config.



42
43
44
# File 'lib/td/logger/agent/rails/config.rb', line 42

def initialize
  @disabled = false
end

Instance Attribute Details

#access_log_tableObject (readonly)

Returns the value of attribute access_log_table.



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

def access_log_table
  @access_log_table
end

#agent_hostObject (readonly)

Returns the value of attribute agent_host.



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

def agent_host
  @agent_host
end

#agent_portObject (readonly)

Returns the value of attribute agent_port.



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

def agent_port
  @agent_port
end

#apikeyObject (readonly)

Returns the value of attribute apikey.



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

def apikey
  @apikey
end

#auto_create_tableObject (readonly)

Returns the value of attribute auto_create_table.



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

def auto_create_table
  @auto_create_table
end

#databaseObject (readonly)

Returns the value of attribute database.



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

def database
  @database
end

#debug_modeObject (readonly)

Returns the value of attribute debug_mode.



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

def debug_mode
  @debug_mode
end

#disabledObject

Returns the value of attribute disabled.



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

def disabled
  @disabled
end

#tagObject (readonly)

Returns the value of attribute tag.



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

def tag
  @tag
end

#test_modeObject (readonly)

Returns the value of attribute test_mode.



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

def test_mode
  @test_mode
end

Class Method Details

.initObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/td/logger/agent/rails/config.rb', line 58

def self.init
  c = Config.new
  config_path = CONFIG_PATH.start_with?('/') ? CONFIG_PATH : "#{::Rails.root}/#{CONFIG_PATH}"

  if File.exist?(config_path)
    c.load_file(config_path)
  elsif File.exist?(CONFIG_PATH_EY_DEPLOY)
    c.load_file_ey(CONFIG_PATH_EY_DEPLOY)
  elsif File.exist?(CONFIG_PATH_EY_LOCAL)
    c.load_file_ey(CONFIG_PATH_EY_LOCAL)
  else
    c.load_env
  end

  return c
rescue
  warn "Disabling Treasure Data event logger: #{$!}"
  c.disabled = true
  return c
end

Instance Method Details

#access_log_enabled?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/td/logger/agent/rails/config.rb', line 54

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

#agent_mode?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/td/logger/agent/rails/config.rb', line 46

def agent_mode?
  @agent_host != nil
end

#assign_conf(conf) ⇒ Object



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
35
# File 'lib/td/logger/agent/rails/config.rb', line 6

def assign_conf(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

  @test_mode = !!conf['test_mode']
  @access_log_table = conf['access_log_table']
end

#load_envObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/td/logger/agent/rails/config.rb', line 107

def load_env
  apikey = ENV['TREASURE_DATA_API_KEY'] || ENV['TD_API_KEY']

  unless apikey
    @disabled = true
    return
  end

  assign_conf({
    '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(path) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/td/logger/agent/rails/config.rb', line 79

def load_file(path)
  conf = load_yaml(path)[::Rails.env]

  unless conf
    @disabled = true
    return
  end

  assign_conf(conf)
end

#load_file_ey(path) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/td/logger/agent/rails/config.rb', line 90

def load_file_ey(path)
  conf = load_yaml(path)
  apikey = conf['td']['TREASURE_DATA_API_KEY'] if conf.is_a?(Hash) and conf['td'].is_a?(Hash)

  unless apikey
    @disabled = true
    return
  end

  assign_conf({
    '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_yaml(path) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/td/logger/agent/rails/config.rb', line 123

def load_yaml(path)
  require 'yaml'
  require 'erb'

  src = File.read(path)
  yaml = ERB.new(src).result
  YAML.load(yaml)
end

#test_mode?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/td/logger/agent/rails/config.rb', line 50

def test_mode?
  @test_mode
end