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.2"

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
# 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
end

.configurationObject



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

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

.current_timestampObject



121
122
123
# File 'lib/influxdb-rails.rb', line 121

def current_timestamp
  Time.now.utc.to_i
end

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



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

def handle_action_controller_metrics(name, start, finish, id, payload)
  timestamp = finish.utc.to_i
  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,
      },
    }

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

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

.ignorable_exception?(e) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
# File 'lib/influxdb-rails.rb', line 125

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



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

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



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

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

.rescue(&block) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/influxdb-rails.rb', line 131

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



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

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

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



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

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