Module: TreasureData::Logger::Agent::Rails

Defined in:
lib/td/logger/agent/rails.rb,
lib/td/logger/agent/rails/model.rb,
lib/td/logger/agent/rails/controller.rb

Defined Under Namespace

Modules: ControllerExtension, ModelExtension Classes: Config

Constant Summary collapse

CONFIG_PATH =
'config/treasure_data.yml'
CONFIG_SAMPLE =
<<EOF
# logging to Treasure Data directly
development:
  apikey: "YOUR_API_KEY"
  database: myapp
  access_log_table: access
  auto_create_table: true

# logging via td-agent
production:
  agent: "localhost:24224"
  tag: td.myapp
  access_log_table: access

# disable logging
test:
EOF

Class Method Summary collapse

Class Method Details

.init(rails) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/td/logger/agent/rails.rb', line 113

def self.init(rails)
  require 'td/logger/agent/middleware'
  require 'td/logger/agent/access_log'
  require 'td/logger/agent/rails/controller'
  require 'td/logger/agent/rails/model'

  c = read_config(rails)
  return unless c

  if c.agent_mode?
    ::TreasureData.open_agent(c.tag, c.agent_host, c.agent_port)
  else
    ::TreasureData.open(c.apikey, c.database, c.auto_create_table)
  end

  rails.middleware.use Agent::Middleware

  if c.access_log_enabled?
    Agent.enable_access_log(c.access_log_table)
  end
  Agent::Rails.init_controller
  Agent::Rails.init_model
end

.init_controllerObject



6
7
8
# File 'lib/td/logger/agent/rails/controller.rb', line 6

def self.init_controller
  ActionController::Base.send(:include, ControllerExtension)
end

.init_modelObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/td/logger/agent/rails/model.rb', line 6

def self.init_model
  unless defined?(::ActiveRecord)
    # disable model extension if Rails > 3 and
    # ActiveRecord is not loaded (other ORM is used)
    if ::Rails.respond_to?(:version) && ::Rails.version =~ /^3/
      return
    end
    require 'active_record'
  end
  ::ActiveRecord::Base.send(:include, ModelExtension)
end

.read_config(rails) ⇒ Object



64
65
66
67
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/td/logger/agent/rails.rb', line 64

def self.read_config(rails)
  require 'yaml'
  require 'erb'
  logger = ::Rails.logger || ::Logger.new(STDERR)

  unless File.exist?(CONFIG_PATH)
    apikey = ENV['TREASURE_DATA_API_KEY'] || ENV['TD_API_KEY']
    unless apikey
      logger.warn "TREASURE_DATA_API_KEY environment variable is not set"
      logger.warn "#{CONFIG_PATH} does not exist."
      logger.warn "Disabling Treasure Data logger."
      return
    end
    return Config.new({
      'apikey' => apikey,
      'database' => ENV['TREASURE_DATA_DB'] || "rails_#{::Rails.env}",
      'access_log_table' => ENV['TREASURE_DATA_TABLE'] || 'web_access',
      'auto_create_table' => true
    })
  end

  begin
    src = File.read("#{::Rails.root}/#{CONFIG_PATH}")
    yaml = ERB.new(src).result
    env_conf = YAML.load(yaml)
  rescue
    logger.warn "Can't load #{CONFIG_PATH} file: #{$!}"
    logger.warn "Disabling Treasure Data logger."
    logger.warn "Example:"
    logger.warn CONFIG_SAMPLE
    return
  end

  conf = env_conf[::Rails.env]
  unless conf
    logger.warn "#{CONFIG_PATH} doesn't include setting for current environment (#{::Rails.env})."
    logger.warn "Disabling Treasure Data logger."
    return
  end

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