influxdb-rails 0.4.999 → 1.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,15 @@
1
1
  module InfluxDB
2
2
  module Rails
3
- module Logger
4
- PREFIX = "[InfluxDB::Rails] "
3
+ module Logger # rubocop:disable Style/Documentation
4
+ PREFIX = "[InfluxDB::Rails] ".freeze
5
5
 
6
6
  private
7
+
7
8
  def log(level, message)
8
- return if level != :error && ! InfluxDB::Rails.configuration.debug?
9
- InfluxDB::Rails.configuration.logger.send(level, PREFIX + message) if InfluxDB::Rails.configuration.logger
9
+ c = InfluxDB::Rails.configuration
10
+ return if level != :error && !c.debug?
11
+
12
+ c.logger&.send(level, PREFIX + message)
10
13
  end
11
14
  end
12
15
  end
@@ -1,31 +1,16 @@
1
1
  module InfluxDB
2
2
  module Rails
3
3
  module Middleware
4
- module HijackRenderException
5
- def render_exception(env, e)
6
- controller = env["action_controller.instance"]
4
+ module HijackRenderException # rubocop:disable Style/Documentation
5
+ def render_exception(env, ex)
6
+ controller = env["action_controller.instance"] || env.controller_instance
7
7
  request_data = controller.try(:influxdb_request_data) || {}
8
8
  unless InfluxDB::Rails.configuration.ignore_user_agent?(request_data[:user_agent])
9
- InfluxDB::Rails.report_exception_unless_ignorable(e, request_data)
9
+ InfluxDB::Rails.report_exception_unless_ignorable(ex, request_data)
10
10
  end
11
11
  super
12
12
  end
13
13
  end
14
-
15
- module OldHijackRenderException
16
- def self.included(base)
17
- base.send(:alias_method_chain, :render_exception, :influxdb)
18
- end
19
-
20
- def render_exception_with_influxdb(env, e)
21
- controller = env["action_controller.instance"]
22
- request_data = controller.try(:influxdb_request_data) || {}
23
- unless InfluxDB::Rails.configuration.ignore_user_agent?(request_data[:user_agent])
24
- InfluxDB::Rails.report_exception_unless_ignorable(e, request_data)
25
- end
26
- render_exception_without_influxdb(env, e)
27
- end
28
- end
29
14
  end
30
15
  end
31
16
  end
@@ -1,32 +1,31 @@
1
1
  module InfluxDB
2
2
  module Rails
3
3
  module Middleware
4
- module HijackRescueActionEverywhere
4
+ module HijackRescueActionEverywhere # rubocop:disable Style/Documentation
5
5
  def self.included(base)
6
6
  base.send(:alias_method_chain, :rescue_action_in_public, :influxdb)
7
7
  base.send(:alias_method_chain, :rescue_action_locally, :influxdb)
8
8
  end
9
9
 
10
10
  private
11
- def rescue_action_in_public_with_influxdb(e)
12
- handle_exception(e)
13
- rescue_action_in_public_without_influxdb(e)
11
+
12
+ def rescue_action_in_public_with_influxdb(ex)
13
+ handle_exception(ex)
14
+ rescue_action_in_public_without_influxdb(ex)
14
15
  end
15
16
 
16
- def rescue_action_locally_with_influxdb(e)
17
- handle_exception(e)
18
- rescue_action_locally_without_influxdb(e)
17
+ def rescue_action_locally_with_influxdb(ex)
18
+ handle_exception(ex)
19
+ rescue_action_locally_without_influxdb(ex)
19
20
  end
20
21
 
21
- def handle_exception(e)
22
+ def handle_exception(ex)
22
23
  request_data = influxdb_request_data || {}
24
+ return if InfluxDB::Rails.configuration.ignore_user_agent?(request_data[:user_agent])
23
25
 
24
- unless InfluxDB::Rails.configuration.ignore_user_agent?(request_data[:user_agent])
25
- InfluxDB::Rails.report_exception_unless_ignorable(e, request_data)
26
- end
26
+ InfluxDB::Rails.report_exception_unless_ignorable(ex, request_data)
27
27
  end
28
28
  end
29
29
  end
30
30
  end
31
31
  end
32
-
@@ -1,6 +1,6 @@
1
1
  module InfluxDB
2
2
  module Rails
3
- class Rack
3
+ class Rack # rubocop:disable Style/Documentation
4
4
  def initialize(app)
5
5
  @app = app
6
6
  end
@@ -12,7 +12,7 @@ module InfluxDB
12
12
  def _call(env)
13
13
  begin
14
14
  response = @app.call(env)
15
- rescue => e
15
+ rescue StandardError => e
16
16
  InfluxDB::Rails.transmit_unless_ignorable(e, env)
17
17
  raise(e)
18
18
  end
@@ -1,54 +1,40 @@
1
- require 'influxdb'
2
- require 'rails'
1
+ require "influxdb"
2
+ require "rails"
3
3
 
4
4
  module InfluxDB
5
5
  module Rails
6
- class Railtie < ::Rails::Railtie
6
+ class Railtie < ::Rails::Railtie # :nodoc:
7
7
  initializer "influxdb.insert_rack_middleware" do |app|
8
8
  app.config.middleware.insert 0, InfluxDB::Rails::Rack
9
9
  end
10
10
 
11
11
  config.after_initialize do
12
- InfluxDB::Rails.configure(true) do |config|
13
- config.logger ||= ::Rails.logger
14
- config.environment ||= ::Rails.env
15
- config.application_root ||= ::Rails.root
16
- config.application_name ||= ::Rails.application.class.parent_name
17
- config.framework = "Rails"
18
- config.framework_version = ::Rails.version
19
- end
12
+ InfluxDB::Rails.configure(true, &:load_rails_defaults)
20
13
 
21
14
  ActiveSupport.on_load(:action_controller) do
22
- require 'influxdb/rails/air_traffic_controller'
15
+ require "influxdb/rails/air_traffic_controller"
23
16
  include InfluxDB::Rails::AirTrafficController
24
- require 'influxdb/rails/instrumentation'
17
+ require "influxdb/rails/instrumentation"
25
18
  include InfluxDB::Rails::Instrumentation
26
19
  end
27
20
 
28
- if defined?(::ActionDispatch::DebugExceptions)
29
- require 'influxdb/rails/middleware/hijack_render_exception'
30
- exceptions_class = ::ActionDispatch::DebugExceptions
31
- elsif defined?(::ActionDispatch::ShowExceptions)
32
- require 'influxdb/rails/middleware/hijack_render_exception'
33
- exceptions_class = ::ActionDispatch::ShowExceptions
34
- end
35
-
36
- InfluxDB::Rails.safely_prepend(
37
- "HijackRenderException",
38
- :from => InfluxDB::Rails::Middleware,
39
- :to => exceptions_class
40
- )
21
+ require "influxdb/rails/middleware/hijack_render_exception"
22
+ ::ActionDispatch::DebugExceptions.prepend InfluxDB::Rails::Middleware::HijackRenderException
41
23
 
42
24
  if defined?(ActiveSupport::Notifications)
43
- ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
44
- if InfluxDB::Rails.configuration.instrumentation_enabled? && ! InfluxDB::Rails.configuration.ignore_current_environment?
25
+ listen = lambda do |name, start, finish, id, payload|
26
+ c = InfluxDB::Rails.configuration
27
+
28
+ if c.instrumentation_enabled? && !c.ignore_current_environment?
45
29
  begin
46
30
  InfluxDB::Rails.handle_action_controller_metrics(name, start, finish, id, payload)
47
- rescue => e
48
- InfluxDB::Rails.configuration.logger.error "[InfluxDB::Rails] Failed writing points to InfluxDB: #{e.message}"
31
+ rescue StandardError => e
32
+ c.logger.error "[InfluxDB::Rails] Failed writing points to InfluxDB: #{e.message}"
49
33
  end
50
34
  end
51
35
  end
36
+
37
+ ActiveSupport::Notifications.subscribe "process_action.action_controller", &listen
52
38
  end
53
39
  end
54
40
  end
@@ -1,5 +1,5 @@
1
1
  module InfluxDB
2
2
  module Rails
3
- VERSION = "0.4.999"
3
+ VERSION = "1.0.0.beta1".freeze
4
4
  end
5
5
  end
@@ -1,14 +1,15 @@
1
- require 'rails/generators'
1
+ require "rails/generators"
2
2
 
3
- class InfluxdbGenerator < Rails::Generators::Base
3
+ class InfluxdbGenerator < Rails::Generators::Base # rubocop:disable Style/Documentation
4
4
  desc "Description:\n This creates a Rails initializer for InfluxDB::Rails."
5
5
 
6
- source_root File.expand_path('../templates', __FILE__)
6
+ source_root File.expand_path("templates", __dir__)
7
7
 
8
8
  def copy_initializer_file
9
- template "initializer.rb", "config/initializers/influxdb-rails.rb"
9
+ template "initializer.rb", "config/initializers/influxdb_rails.rb"
10
10
  end
11
11
 
12
12
  def install
13
+ # nothing to do here
13
14
  end
14
15
  end
@@ -1,6 +1,6 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
- RSpec.describe WidgetsController, :type => :controller do
3
+ RSpec.describe WidgetsController, type: :controller do
4
4
  describe "#new" do
5
5
  it "should raise an exception" do
6
6
  expect { get :new }.to raise_error(ZeroDivisionError)
@@ -1,9 +1,9 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + "/integration_helper")
2
2
 
3
- RSpec.describe "exception handling", :type => :request do
3
+ RSpec.describe "exception handling", type: :request do
4
4
  before do
5
5
  InfluxDB::Rails.configure do |config|
6
- config.ignored_environments = %w{development}
6
+ config.ignored_environments = %w[development]
7
7
  config.instrumentation_enabled = false
8
8
  end
9
9
  end
@@ -26,16 +26,12 @@ RSpec.describe "exception handling", :type => :request do
26
26
  it "should not make an HTTP call to the API" do
27
27
  expect(InfluxDB::Rails.client).not_to receive(:write_point)
28
28
 
29
- InfluxDB::Rails.configure do |config|
30
- config.ignored_user_agents = %w{Googlebot}
31
- end
32
-
29
+ # note: GoogleBot is ignored by default
33
30
  if Rails::VERSION::MAJOR >= 5
34
- get "/widgets/new", headers: { "HTTP_USER_AGENT" => "Googlebot/2.1" }
31
+ get "/widgets/new", headers: { "HTTP_USER_AGENT" => "GoogleBot/2.1" }
35
32
  else
36
- get "/widgets/new", {}, { "HTTP_USER_AGENT" => "Googlebot/2.1" }
33
+ get "/widgets/new", {}, "HTTP_USER_AGENT" => "GoogleBot/2.1"
37
34
  end
38
35
  end
39
36
  end
40
37
  end
41
-
@@ -1,10 +1,9 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + "/integration_helper")
2
2
 
3
- RSpec.describe "collecting metrics through ActiveSupport::Notifications", :type => :request do
3
+ RSpec.describe "collecting metrics through ActiveSupport::Notifications", type: :request do
4
4
  before do
5
5
  InfluxDB::Rails.configure do |config|
6
- config.ignored_environments = %w{development}
7
- config.instrumentation_enabled = true
6
+ config.ignored_environments = %w[development]
8
7
  end
9
8
  end
10
9
 
@@ -20,4 +19,3 @@ RSpec.describe "collecting metrics through ActiveSupport::Notifications", :type
20
19
  end
21
20
  end
22
21
  end
23
-
data/spec/spec_helper.rb CHANGED
@@ -1,14 +1,14 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  ENV["RAILS_ENV"] ||= "test"
4
4
 
5
5
  require "rails"
6
6
 
7
- if Rails::VERSION::MAJOR < 4
8
- raise "Sorry, influxdb-rails only supports Rails 4.x and higher."
7
+ if Rails::VERSION::MAJOR < 4 || Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR < 2
8
+ raise "Sorry, influxdb-rails only supports Rails 4.2 and higher."
9
9
  end
10
10
 
11
- require 'bundler/setup'
11
+ require "bundler/setup"
12
12
  Bundler.require
13
13
 
14
14
  require "fakeweb"
@@ -16,13 +16,16 @@ FakeWeb.allow_net_connect = false
16
16
 
17
17
  puts "Loading Rails v#{Rails.version}..."
18
18
 
19
- if Rails.version.to_f < 5.0
20
- require "support/rails4/app"
21
- require "rspec/rails"
22
- else
23
- require "support/rails5/app"
24
- require "rspec/rails"
25
- end
19
+ require "support/rails#{Rails::VERSION::MAJOR}/app"
20
+ require "rspec/rails"
21
+
22
+ RSpec.configure do |config|
23
+ # use expect syntax
24
+ config.disable_monkey_patching!
26
25
 
27
- # use expect syntax
28
- RSpec.configure { |c| c.disable_monkey_patching! }
26
+ # reset configuration for each spec
27
+ config.before :each do
28
+ InfluxDB::Rails.instance_variable_set :@configuration, nil
29
+ InfluxDB::Rails.configure(&:load_rails_defaults)
30
+ end
31
+ end
@@ -1,8 +1,8 @@
1
- require 'action_controller/railtie'
1
+ require "action_controller/railtie"
2
2
 
3
3
  app = Class.new(Rails::Application)
4
- app.config.secret_token = '1234567890abcdef1234567890abcdef'
5
- app.config.session_store :cookie_store, :key => '_myapp_session'
4
+ app.config.secret_token = "1234567890abcdef1234567890abcdef"
5
+ app.config.session_store :cookie_store, key: "_myapp_session"
6
6
  app.config.active_support.deprecation = :log
7
7
  app.config.eager_load = false
8
8
  app.config.root = File.dirname(__FILE__)
@@ -18,8 +18,13 @@ end
18
18
 
19
19
  class ApplicationController < ActionController::Base; end
20
20
  class WidgetsController < ApplicationController
21
- def index; render :nothing => true; end
22
- def new; return 1/0; end
21
+ def index
22
+ render nothing: true
23
+ end
24
+
25
+ def new
26
+ 1 / 0
27
+ end
23
28
  end
24
29
 
25
30
  Object.const_set(:ApplicationHelper, Module.new)
@@ -1,8 +1,9 @@
1
- require 'action_controller/railtie'
1
+ require "action_controller/railtie"
2
2
 
3
3
  app = Class.new(Rails::Application)
4
- app.config.secret_token = '1234567890abcdef1234567890abcdef'
5
- app.config.session_store :cookie_store, :key => '_myapp_session'
4
+ app.config.secret_key_base = "1234567890abcdef1234567890abcdef"
5
+ app.config.secret_token = "1234567890abcdef1234567890abcdef"
6
+ app.config.session_store :cookie_store, key: "_myapp_session"
6
7
  app.config.active_support.deprecation = :log
7
8
  app.config.eager_load = false
8
9
  app.config.root = File.dirname(__FILE__)
@@ -18,8 +19,13 @@ end
18
19
 
19
20
  class ApplicationController < ActionController::Base; end
20
21
  class WidgetsController < ApplicationController
21
- def index; head 200; end
22
- def new; return 1/0; end
22
+ def index
23
+ head 200
24
+ end
25
+
26
+ def new
27
+ 1 / 0
28
+ end
23
29
  end
24
30
 
25
31
  Object.const_set(:ApplicationHelper, Module.new)
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  RSpec.describe InfluxDB::Rails::Backtrace do
4
4
  before do
@@ -33,7 +33,6 @@ RSpec.describe InfluxDB::Rails::Backtrace do
33
33
  context "nil backtrace" do
34
34
  before do
35
35
  @raw_backtrace = nil
36
-
37
36
  @backtrace = InfluxDB::Rails::Backtrace.new(@raw_backtrace)
38
37
  end
39
38
 
@@ -47,7 +46,6 @@ RSpec.describe InfluxDB::Rails::Backtrace do
47
46
  expect(@backtrace.to_a.is_a?(Array)).to be_truthy
48
47
  end
49
48
  end
50
-
51
49
  end
52
50
 
53
51
  describe "backtrace filters" do
@@ -75,7 +73,7 @@ RSpec.describe InfluxDB::Rails::Backtrace do
75
73
 
76
74
  it "should allow the addition of custom backtrace filters" do
77
75
  InfluxDB::Rails.configure do |config|
78
- config.backtrace_filters << lambda { |line| line.gsub(/foo/, "F00") }
76
+ config.backtrace_filters << ->(line) { line.gsub(/foo/, "F00") }
79
77
  end
80
78
 
81
79
  filtered_backtrace = InfluxDB::Rails::Backtrace.new(@raw_backtrace)
@@ -85,4 +83,3 @@ RSpec.describe InfluxDB::Rails::Backtrace do
85
83
  end
86
84
  end
87
85
  end
88
-
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  RSpec.describe InfluxDB::Rails::Configuration do
4
4
  before do
@@ -7,12 +7,12 @@ RSpec.describe InfluxDB::Rails::Configuration do
7
7
 
8
8
  describe "#ignore_user_agent?" do
9
9
  it "should be true for user agents that have been set as ignorable" do
10
- @configuration.ignored_user_agents = %w{Googlebot}
10
+ @configuration.ignored_user_agents = %w[Googlebot]
11
11
  expect(@configuration.ignore_user_agent?("Googlebot/2.1")).to be_truthy
12
12
  end
13
13
 
14
14
  it "should be false for user agents that have not been set as ignorable" do
15
- @configuration.ignored_user_agents = %w{Googlebot}
15
+ @configuration.ignored_user_agents = %w[Googlebot]
16
16
  expect(@configuration.ignore_user_agent?("Mozilla/5.0")).to be_falsey
17
17
  end
18
18
 
@@ -29,8 +29,6 @@ RSpec.describe InfluxDB::Rails::Configuration do
29
29
 
30
30
  describe "#retry" do
31
31
  it "defaults to nil" do
32
- InfluxDB::Rails.configure do |config|
33
- end
34
32
  expect(InfluxDB::Rails.configuration.retry).to be_nil
35
33
  end
36
34
 
@@ -44,8 +42,6 @@ RSpec.describe InfluxDB::Rails::Configuration do
44
42
 
45
43
  describe "#open_timeout" do
46
44
  it "defaults to 5" do
47
- InfluxDB::Rails.configure do |config|
48
- end
49
45
  expect(InfluxDB::Rails.configuration.open_timeout).to eql(5)
50
46
  end
51
47
 
@@ -59,8 +55,6 @@ RSpec.describe InfluxDB::Rails::Configuration do
59
55
 
60
56
  describe "#read_timeout" do
61
57
  it "defaults to 300" do
62
- InfluxDB::Rails.configure do |config|
63
- end
64
58
  expect(InfluxDB::Rails.configuration.read_timeout).to eql(300)
65
59
  end
66
60
 
@@ -74,8 +68,6 @@ RSpec.describe InfluxDB::Rails::Configuration do
74
68
 
75
69
  describe "#max_delay" do
76
70
  it "defaults to 30" do
77
- InfluxDB::Rails.configure do |config|
78
- end
79
71
  expect(InfluxDB::Rails.configuration.max_delay).to eql(30)
80
72
  end
81
73
 
@@ -89,16 +81,28 @@ RSpec.describe InfluxDB::Rails::Configuration do
89
81
 
90
82
  describe "#time_precision" do
91
83
  it "defaults to seconds" do
84
+ expect(InfluxDB::Rails.configuration.time_precision).to eql("s")
85
+ end
86
+
87
+ it "can be updated" do
92
88
  InfluxDB::Rails.configure do |config|
89
+ config.time_precision = "ms"
93
90
  end
94
- expect(InfluxDB::Rails.configuration.time_precision).to eql('s')
91
+ expect(InfluxDB::Rails.configuration.time_precision).to eql("ms")
95
92
  end
93
+ end
96
94
 
97
- it "can be updated" do
95
+ describe "#rails_app_name" do
96
+ it "defaults to nil" do
97
+ expect(InfluxDB::Rails.configuration.rails_app_name).to be(nil)
98
+ end
99
+
100
+ it "can be set to own name" do
98
101
  InfluxDB::Rails.configure do |config|
99
- config.time_precision = 'ms'
102
+ config.rails_app_name = "my-app"
100
103
  end
101
- expect(InfluxDB::Rails.configuration.time_precision).to eql('ms')
104
+
105
+ expect(InfluxDB::Rails.configuration.rails_app_name).to eq("my-app")
102
106
  end
103
107
  end
104
108
  end