Module: InfluxDB::Rails

Extended by:
Logger
Defined in:
lib/influxdb-rails.rb,
lib/influxdb/rails/rack.rb,
lib/influxdb/rails/logger.rb,
lib/influxdb/rails/railtie.rb,
lib/influxdb/rails/version.rb,
lib/influxdb/rails/backtrace.rb,
lib/influxdb/rails/configuration.rb,
lib/influxdb/rails/instrumentation.rb,
lib/influxdb/rails/exception_presenter.rb,
lib/influxdb/rails/air_traffic_controller.rb,
lib/influxdb/rails/middleware/hijack_render_exception.rb,
lib/influxdb/rails/middleware/hijack_rescue_action_everywhere.rb

Defined Under Namespace

Modules: AirTrafficController, Instrumentation, Logger, Middleware Classes: Backtrace, Configuration, ExceptionPresenter, Rack, Railtie

Constant Summary collapse

VERSION =
"0.4.3"

Constants included from Logger

Logger::PREFIX

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/influxdb-rails.rb', line 33

def client
  @client ||= InfluxDB::Client.new configuration.influxdb_database,
    :username => configuration.influxdb_username,
    :password => configuration.influxdb_password,
    :hosts => configuration.influxdb_hosts,
    :port => configuration.influxdb_port,
    :async => configuration.async,
    :use_ssl => configuration.use_ssl,
    :retry => configuration.retry,
    :open_timeout => configuration.open_timeout,
    :read_timeout => configuration.read_timeout,
    :max_delay => configuration.max_delay,
    :time_precision => configuration.time_precision
end

.configurationObject



48
49
50
# File 'lib/influxdb-rails.rb', line 48

def configuration
  @configuration ||= InfluxDB::Rails::Configuration.new
end

Class Method Details

.configure(silent = false) {|configuration| ... } ⇒ Object

Yields:



24
25
26
27
28
29
30
31
# File 'lib/influxdb-rails.rb', line 24

def configure(silent = false)
  yield(configuration)

  # if we change configuration, reload the client
  self.client = nil

  InfluxDB::Logging.logger = configuration.logger unless configuration.logger.nil?
end

.convert_timestamp(ts) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/influxdb-rails.rb', line 125

def convert_timestamp(ts)
  case configuration.time_precision
  when 'ns', nil
    (ts.to_r * 1e9).to_i
  when 'u'
    (ts.to_r * 1e6).to_i
  when 'ms'
    (ts.to_r * 1e3).to_i
  when 's'
    ts.to_i
  when 'm'
    ts.to_i / 60
  when 'h'
    ts.to_i / 60 / 60
  else
    raise "Invalid time precision: #{configuration.time_precision}"
  end
end

.current_timestampObject



144
145
146
# File 'lib/influxdb-rails.rb', line 144

def current_timestamp
  convert_timestamp(Time.now.utc)
end

.handle_action_controller_metrics(name, start, finish, id, payload) ⇒ Object



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
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/influxdb-rails.rb', line 79

def handle_action_controller_metrics(name, start, finish, id, payload)
  timestamp = convert_timestamp(finish.utc)
  controller_runtime = ((finish - start)*1000).ceil
  view_runtime = (payload[:view_runtime] || 0).ceil
  db_runtime = (payload[:db_runtime] || 0).ceil
  method = "#{payload[:controller]}##{payload[:action]}"
  hostname = Socket.gethostname

  begin
    client.write_point configuration.series_name_for_controller_runtimes, {
      values: {
        value: controller_runtime,
      },
      tags: {
        method: method,
        server: hostname,
      },
      timestamp: timestamp,
    }

    client.write_point configuration.series_name_for_view_runtimes, {
      values: {
        value: view_runtime,
      },
      tags: {
        method: method,
        server: hostname,
      },
      timestamp: timestamp,
    }

    client.write_point configuration.series_name_for_db_runtimes, {
      values: {
        value: db_runtime,
      },
      tags: {
        method: method,
        server: hostname,
      },
      timestamp: timestamp,
    }
  rescue => e
    log :error, "[InfluxDB::Rails] Unable to write points: #{e.message}"
  end
end

.ignorable_exception?(e) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
152
# File 'lib/influxdb-rails.rb', line 148

def ignorable_exception?(e)
  configuration.ignore_current_environment? ||
  !!configuration.ignored_exception_messages.find{ |msg| /.*#{msg}.*/ =~ e.message  } ||
  configuration.ignored_exceptions.include?(e.class.to_s)
end

.report_exception(e, env = {}) ⇒ Object Also known as: transmit



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/influxdb-rails.rb', line 57

def report_exception(e, env = {})
  begin
    env = influxdb_request_data if env.empty? && defined? influxdb_request_data
    exception_presenter = ExceptionPresenter.new(e, env)
    log :info, "Exception: #{exception_presenter.to_json[0..512]}..."

    ex_data = exception_presenter.context.merge(exception_presenter.dimensions)
    timestamp = ex_data.delete(:time)

    client.write_point "rails.exceptions", {
      values: {
        ts: timestamp,
      },
      tags: ex_data,
      timestamp: timestamp,
    }
  rescue => e
    log :info, "[InfluxDB::Rails] Something went terribly wrong. Exception failed to take off! #{e.class}: #{e.message}"
  end
end

.report_exception_unless_ignorable(e, env = {}) ⇒ Object Also known as: transmit_unless_ignorable



52
53
54
# File 'lib/influxdb-rails.rb', line 52

def report_exception_unless_ignorable(e, env = {})
  report_exception(e, env) unless ignorable_exception?(e)
end

.rescue(&block) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/influxdb-rails.rb', line 154

def rescue(&block)
  block.call
rescue StandardError => e
  if configuration.ignore_current_environment?
    raise(e)
  else
    transmit_unless_ignorable(e)
  end
end

.rescue_and_reraise(&block) ⇒ Object



164
165
166
167
168
169
# File 'lib/influxdb-rails.rb', line 164

def rescue_and_reraise(&block)
  block.call
rescue StandardError => e
  transmit_unless_ignorable(e)
  raise(e)
end

.safely_prepend(module_name, opts = {}) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/influxdb-rails.rb', line 171

def safely_prepend(module_name, opts = {})
  return if opts[:to].nil? || opts[:from].nil?
  if opts[:to].respond_to?(:prepend, true)
    opts[:to].send(:prepend, opts[:from].const_get(module_name))
  else
    opts[:to].send(:include, opts[:from].const_get("Old" + module_name))
  end
end