asklytics-influxdb-rails 1.0.0.beta3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +78 -0
  5. data/.travis.yml +37 -0
  6. data/CHANGELOG.md +127 -0
  7. data/Gemfile +9 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +291 -0
  10. data/Rakefile +34 -0
  11. data/config.ru +7 -0
  12. data/gemfiles/Gemfile.rails-4.2.x +7 -0
  13. data/gemfiles/Gemfile.rails-5.0.x +7 -0
  14. data/gemfiles/Gemfile.rails-5.1.x +7 -0
  15. data/gemfiles/Gemfile.rails-5.2.x +7 -0
  16. data/influxdb-rails.gemspec +52 -0
  17. data/lib/httplog.rb +8 -0
  18. data/lib/influxdb-rails.rb +134 -0
  19. data/lib/influxdb/rails/air_traffic_controller.rb +41 -0
  20. data/lib/influxdb/rails/backtrace.rb +44 -0
  21. data/lib/influxdb/rails/configuration.rb +263 -0
  22. data/lib/influxdb/rails/context.rb +42 -0
  23. data/lib/influxdb/rails/exception_presenter.rb +94 -0
  24. data/lib/influxdb/rails/httplog/adapters/ethon.rb +62 -0
  25. data/lib/influxdb/rails/httplog/adapters/excon.rb +67 -0
  26. data/lib/influxdb/rails/httplog/adapters/http.rb +64 -0
  27. data/lib/influxdb/rails/httplog/adapters/httpclient.rb +76 -0
  28. data/lib/influxdb/rails/httplog/adapters/net_http.rb +53 -0
  29. data/lib/influxdb/rails/httplog/adapters/patron.rb +44 -0
  30. data/lib/influxdb/rails/httplog/helpers/al_helper.rb +12 -0
  31. data/lib/influxdb/rails/httplog/http_configuration.rb +55 -0
  32. data/lib/influxdb/rails/httplog/http_log.rb +332 -0
  33. data/lib/influxdb/rails/instrumentation.rb +34 -0
  34. data/lib/influxdb/rails/logger.rb +16 -0
  35. data/lib/influxdb/rails/middleware/hijack_render_exception.rb +16 -0
  36. data/lib/influxdb/rails/middleware/hijack_rescue_action_everywhere.rb +31 -0
  37. data/lib/influxdb/rails/middleware/render_subscriber.rb +28 -0
  38. data/lib/influxdb/rails/middleware/request_subscriber.rb +69 -0
  39. data/lib/influxdb/rails/middleware/simple_subscriber.rb +71 -0
  40. data/lib/influxdb/rails/middleware/sql_subscriber.rb +35 -0
  41. data/lib/influxdb/rails/middleware/subscriber.rb +44 -0
  42. data/lib/influxdb/rails/rack.rb +39 -0
  43. data/lib/influxdb/rails/railtie.rb +52 -0
  44. data/lib/influxdb/rails/sql/normalizer.rb +27 -0
  45. data/lib/influxdb/rails/sql/query.rb +32 -0
  46. data/lib/influxdb/rails/version.rb +5 -0
  47. data/lib/rails/generators/influxdb/influxdb_generator.rb +15 -0
  48. data/lib/rails/generators/influxdb/templates/initializer.rb +11 -0
  49. data/spec/controllers/widgets_controller_spec.rb +15 -0
  50. data/spec/integration/exceptions_spec.rb +37 -0
  51. data/spec/integration/integration_helper.rb +1 -0
  52. data/spec/integration/metrics_spec.rb +28 -0
  53. data/spec/shared_examples/tags.rb +42 -0
  54. data/spec/spec_helper.rb +31 -0
  55. data/spec/support/rails4/app.rb +44 -0
  56. data/spec/support/rails5/app.rb +44 -0
  57. data/spec/support/views/widgets/_item.html.erb +1 -0
  58. data/spec/support/views/widgets/index.html.erb +5 -0
  59. data/spec/unit/backtrace_spec.rb +85 -0
  60. data/spec/unit/configuration_spec.rb +125 -0
  61. data/spec/unit/context_spec.rb +40 -0
  62. data/spec/unit/exception_presenter_spec.rb +23 -0
  63. data/spec/unit/influxdb_rails_spec.rb +78 -0
  64. data/spec/unit/middleware/render_subscriber_spec.rb +92 -0
  65. data/spec/unit/middleware/request_subscriber_spec.rb +91 -0
  66. data/spec/unit/middleware/sql_subscriber_spec.rb +81 -0
  67. data/spec/unit/sql/normalizer_spec.rb +15 -0
  68. data/spec/unit/sql/query_spec.rb +29 -0
  69. metadata +487 -0
@@ -0,0 +1,32 @@
1
+ require "influxdb/rails/sql/normalizer"
2
+
3
+ module InfluxDB
4
+ module Rails
5
+ module Sql
6
+ class Query # :nodoc:
7
+ attr_reader :query, :name
8
+
9
+ TRACKED_SQL_COMMANDS = %w[SELECT INSERT UPDATE DELETE].freeze
10
+ UNTRACKED_NAMES = %w[SCHEMA].freeze
11
+
12
+ def initialize(payload)
13
+ @query = payload[:sql].to_s.dup.upcase
14
+ @name = payload[:name].to_s.dup
15
+ end
16
+
17
+ def operation
18
+ query.split.first
19
+ end
20
+
21
+ def class_name
22
+ name.split.first
23
+ end
24
+
25
+ def track?
26
+ @track ||= query.start_with?(*TRACKED_SQL_COMMANDS) &&
27
+ !name.upcase.start_with?(*UNTRACKED_NAMES)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module InfluxDB
2
+ module Rails
3
+ VERSION = "1.0.0.beta3".freeze
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require "rails/generators"
2
+
3
+ class InfluxdbGenerator < Rails::Generators::Base # rubocop:disable Style/Documentation
4
+ desc "Description:\n This creates a Rails initializer for InfluxDB::Rails."
5
+
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ def copy_initializer_file
9
+ template "initializer.rb", "config/initializers/influxdb_rails.rb"
10
+ end
11
+
12
+ def install
13
+ # nothing to do here
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ InfluxDB::Rails.configure do |config|
2
+ config.influxdb_database = "rails"
3
+ config.influxdb_username = "root"
4
+ config.influxdb_password = "root"
5
+ config.influxdb_hosts = ["localhost"]
6
+ config.influxdb_port = 8086
7
+
8
+ # config.series_name_for_controller_runtimes = "rails.controller"
9
+ # config.series_name_for_view_runtimes = "rails.view"
10
+ # config.series_name_for_db_runtimes = "rails.db"
11
+ end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe WidgetsController, type: :controller do
4
+ describe "#new" do
5
+ it "should raise an exception" do
6
+ expect { get :new }.to raise_error(ZeroDivisionError)
7
+ end
8
+ end
9
+
10
+ describe "#index" do
11
+ it "should not raise an exception" do
12
+ expect { get :index }.to_not raise_error
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/integration_helper")
2
+
3
+ RSpec.describe "exception handling", type: :request do
4
+ before do
5
+ InfluxDB::Rails.configure do |config|
6
+ config.ignored_environments = %w[development]
7
+ config.instrumentation_enabled = false
8
+ end
9
+ end
10
+
11
+ describe "in an action that raises an exception" do
12
+ it "should add an exception to the queue" do
13
+ expect(InfluxDB::Rails.client).to receive(:write_point)
14
+ get "/widgets/new"
15
+ end
16
+ end
17
+
18
+ describe "in an action that does not raise an exception" do
19
+ it "should not add anything to the queue" do
20
+ expect(InfluxDB::Rails.client).not_to receive(:write_point)
21
+ get "/widgets"
22
+ end
23
+ end
24
+
25
+ describe "for an ignored user agent" do
26
+ it "should not make an HTTP call to the API" do
27
+ expect(InfluxDB::Rails.client).not_to receive(:write_point)
28
+
29
+ # note: GoogleBot is ignored by default
30
+ if Rails::VERSION::MAJOR >= 5
31
+ get "/widgets/new", headers: { "HTTP_USER_AGENT" => "GoogleBot/2.1" }
32
+ else
33
+ get "/widgets/new", {}, "HTTP_USER_AGENT" => "GoogleBot/2.1"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/integration_helper")
2
+
3
+ RSpec.describe WidgetsController, type: :controller do
4
+ render_views
5
+
6
+ before do
7
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
8
+ end
9
+
10
+ describe "in a normal request" do
11
+ it "should result in attempts to write metrics via the client" do
12
+ expect(InfluxDB::Rails.client).to receive(:write_point).exactly(6).times
13
+ get :index
14
+ end
15
+
16
+ context "with sql reports enabled" do
17
+ before do
18
+ allow_any_instance_of(InfluxDB::Rails::Middleware::SqlSubscriber).to receive(:series_name).and_return("rails.sql")
19
+ get :index # to not count ActiveRecord initialization
20
+ end
21
+
22
+ it "should result in attempts to write metrics via the client" do
23
+ expect(InfluxDB::Rails.client).to receive(:write_point).exactly(7).times
24
+ get :index
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.shared_examples_for "with additional tags" do |series_names|
4
+ context "when tags_middleware is overwritten" do
5
+ before do
6
+ allow(config).to receive(:tags_middleware).and_return(tags_middleware)
7
+ end
8
+
9
+ let(:tags_middleware) { ->(tags) { tags.merge(static: "value") } }
10
+
11
+ it "processes tags throught the middleware" do
12
+ tags = data[:tags].merge(static: "value")
13
+
14
+ series_names.each do |series_name|
15
+ expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(series_name, include(tags: tags))
16
+ end
17
+
18
+ subject.call("unused", start, finish, "unused", payload)
19
+ end
20
+ end
21
+
22
+ context "when tags are set in the current context" do
23
+ let(:additional_tags) do
24
+ { another: :value }
25
+ end
26
+
27
+ after do
28
+ InfluxDB::Rails.current.reset
29
+ end
30
+
31
+ it "does include the tags" do
32
+ InfluxDB::Rails.current.tags = additional_tags
33
+ tags = data[:tags].merge(additional_tags)
34
+
35
+ series_names.each do |series_name|
36
+ expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(series_name, include(tags: tags))
37
+ end
38
+
39
+ subject.call("unused", start, finish, "unused", payload)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ ENV["RAILS_ENV"] ||= "test"
4
+
5
+ require "rails"
6
+
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
+ end
10
+
11
+ require "bundler/setup"
12
+ Bundler.require
13
+
14
+ require "fakeweb"
15
+ FakeWeb.allow_net_connect = false
16
+
17
+ puts "Loading Rails v#{Rails.version}..."
18
+
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!
25
+
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
@@ -0,0 +1,44 @@
1
+ require "action_controller/railtie"
2
+ require "active_record"
3
+
4
+ app = Class.new(Rails::Application)
5
+ app.config.secret_key_base = "1234567890abcdef1234567890abcdef"
6
+ app.config.secret_token = "1234567890abcdef1234567890abcdef"
7
+ app.config.session_store :cookie_store, key: "_myapp_session"
8
+ app.config.active_support.deprecation = :log
9
+ app.config.eager_load = false
10
+ app.config.root = __dir__
11
+ Rails.backtrace_cleaner.remove_silencers!
12
+ app.initialize!
13
+
14
+ app.routes.draw do
15
+ resources :widgets
16
+ end
17
+
18
+ InfluxDB::Rails.configure do |config|
19
+ end
20
+
21
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
22
+ ActiveRecord::Schema.define do
23
+ create_table :widgets, force: true do |t|
24
+ t.string :title
25
+
26
+ t.timestamps
27
+ end
28
+ end
29
+
30
+ class Widget < ActiveRecord::Base; end
31
+ class ApplicationController < ActionController::Base; end
32
+ class WidgetsController < ApplicationController
33
+ prepend_view_path File.join(__dir__, "..", "views")
34
+
35
+ def index
36
+ Widget.create!(title: "test")
37
+ end
38
+
39
+ def new
40
+ 1 / 0
41
+ end
42
+ end
43
+
44
+ Object.const_set(:ApplicationHelper, Module.new)
@@ -0,0 +1,44 @@
1
+ require "action_controller/railtie"
2
+ require "active_record"
3
+
4
+ app = Class.new(Rails::Application)
5
+ app.config.secret_key_base = "1234567890abcdef1234567890abcdef"
6
+ app.config.secret_token = "1234567890abcdef1234567890abcdef"
7
+ app.config.session_store :cookie_store, key: "_myapp_session"
8
+ app.config.active_support.deprecation = :log
9
+ app.config.eager_load = false
10
+ app.config.root = __dir__
11
+ Rails.backtrace_cleaner.remove_silencers!
12
+ app.initialize!
13
+
14
+ app.routes.draw do
15
+ resources :widgets
16
+ end
17
+
18
+ InfluxDB::Rails.configure do |config|
19
+ end
20
+
21
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
22
+ ActiveRecord::Schema.define do
23
+ create_table :widgets, force: true do |t|
24
+ t.string :title
25
+
26
+ t.timestamps
27
+ end
28
+ end
29
+
30
+ class Widget < ActiveRecord::Base; end
31
+ class ApplicationController < ActionController::Base; end
32
+ class WidgetsController < ApplicationController
33
+ prepend_view_path File.join(__dir__, "..", "views")
34
+
35
+ def index
36
+ Widget.create!(title: "test")
37
+ end
38
+
39
+ def new
40
+ 1 / 0
41
+ end
42
+ end
43
+
44
+ Object.const_set(:ApplicationHelper, Module.new)
@@ -0,0 +1 @@
1
+ <div>item</div
@@ -0,0 +1,5 @@
1
+ <h1>Index page</h1>
2
+ <div>
3
+ <%= render partial: 'item' %>
4
+ <%= render partial: 'item', collection: [1,2,3] %>
5
+ </div>
@@ -0,0 +1,85 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe InfluxDB::Rails::Backtrace do
4
+ before do
5
+ @raw_backtrace = [
6
+ "/var/www/current/app/models/foo.rb:10:in `bar'",
7
+ "/var/www/current/app/models/foo.rb:19:in `baz'",
8
+ "/var/www/current/app/models/foo.rb:32:in `<main>'"
9
+ ]
10
+
11
+ @backtrace = InfluxDB::Rails::Backtrace.new(@raw_backtrace)
12
+ end
13
+
14
+ it "should accept an exception into the initializer" do
15
+ expect(@backtrace.lines).not_to be_empty
16
+ expect(@backtrace.lines.count).to eq(3)
17
+ end
18
+
19
+ it "should correctly parse lines into their elements" do
20
+ line = @backtrace.lines.first
21
+
22
+ expect(line.file).to eq("/var/www/current/app/models/foo.rb")
23
+ expect(line.number).to eq("10")
24
+ expect(line.method).to eq("bar")
25
+ end
26
+
27
+ describe "#to_a" do
28
+ it "should return an array of lines" do
29
+ expect(@backtrace.to_a.is_a?(Array)).to be_truthy
30
+ end
31
+ end
32
+
33
+ context "nil backtrace" do
34
+ before do
35
+ @raw_backtrace = nil
36
+ @backtrace = InfluxDB::Rails::Backtrace.new(@raw_backtrace)
37
+ end
38
+
39
+ it "should accept an exception into the initializer" do
40
+ expect(@backtrace.lines).to be_empty
41
+ expect(@backtrace.lines.count).to eq(0)
42
+ end
43
+
44
+ describe "#to_a" do
45
+ it "should return an array of lines" do
46
+ expect(@backtrace.to_a.is_a?(Array)).to be_truthy
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "backtrace filters" do
52
+ before do
53
+ InfluxDB::Rails.configure do |config|
54
+ config.application_root = "/var/www/current"
55
+ end
56
+ end
57
+
58
+ it "should apply a single default backtrace filter correctly" do
59
+ filtered_backtrace = InfluxDB::Rails::Backtrace.new(@raw_backtrace)
60
+
61
+ line = filtered_backtrace.lines.first
62
+ expect(line.file).to eq("[APP_ROOT]/app/models/foo.rb")
63
+ end
64
+
65
+ it "should all default backtrace filters correctly" do
66
+ extended_backtrace = @raw_backtrace.dup
67
+ extended_backtrace << "#{Gem.path.first}/lib/foo_gem.rb:1:in `blah'"
68
+
69
+ filtered_backtrace = InfluxDB::Rails::Backtrace.new(extended_backtrace)
70
+ expect(filtered_backtrace.lines.first.file).to eq("[APP_ROOT]/app/models/foo.rb")
71
+ expect(filtered_backtrace.lines.last.file).to eq("[GEM_ROOT]/lib/foo_gem.rb")
72
+ end
73
+
74
+ it "should allow the addition of custom backtrace filters" do
75
+ InfluxDB::Rails.configure do |config|
76
+ config.backtrace_filters << ->(line) { line.gsub(/foo/, "F00") }
77
+ end
78
+
79
+ filtered_backtrace = InfluxDB::Rails::Backtrace.new(@raw_backtrace)
80
+
81
+ line = filtered_backtrace.lines.first
82
+ expect(line.file).to eq("[APP_ROOT]/app/models/F00.rb")
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,125 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe InfluxDB::Rails::Configuration do
4
+ before do
5
+ @configuration = InfluxDB::Rails::Configuration.new
6
+ end
7
+
8
+ describe "#ignore_user_agent?" do
9
+ it "should be true for user agents that have been set as ignorable" do
10
+ @configuration.ignored_user_agents = %w[Googlebot]
11
+ expect(@configuration.ignore_user_agent?("Googlebot/2.1")).to be_truthy
12
+ end
13
+
14
+ it "should be false for user agents that have not been set as ignorable" do
15
+ @configuration.ignored_user_agents = %w[Googlebot]
16
+ expect(@configuration.ignore_user_agent?("Mozilla/5.0")).to be_falsey
17
+ end
18
+
19
+ it "should be false if the ignored user agents list is empty" do
20
+ @configuration.ignored_user_agents = []
21
+ expect(@configuration.ignore_user_agent?("Googlebot/2.1")).to be_falsey
22
+ end
23
+
24
+ it "should be false if the ignored user agents list is inadvertently set to nil" do
25
+ @configuration.ignored_user_agents = nil
26
+ expect(@configuration.ignore_user_agent?("Googlebot/2.1")).to be_falsey
27
+ end
28
+ end
29
+
30
+ describe "#retry" do
31
+ it "defaults to nil" do
32
+ expect(InfluxDB::Rails.configuration.retry).to be_nil
33
+ end
34
+
35
+ it "can be updated" do
36
+ InfluxDB::Rails.configure do |config|
37
+ config.retry = 5
38
+ end
39
+ expect(InfluxDB::Rails.configuration.retry).to eql(5)
40
+ end
41
+ end
42
+
43
+ describe "#open_timeout" do
44
+ it "defaults to 5" do
45
+ expect(InfluxDB::Rails.configuration.open_timeout).to eql(5)
46
+ end
47
+
48
+ it "can be updated" do
49
+ InfluxDB::Rails.configure do |config|
50
+ config.open_timeout = 5
51
+ end
52
+ expect(InfluxDB::Rails.configuration.open_timeout).to eql(5)
53
+ end
54
+ end
55
+
56
+ describe "#read_timeout" do
57
+ it "defaults to 300" do
58
+ expect(InfluxDB::Rails.configuration.read_timeout).to eql(300)
59
+ end
60
+
61
+ it "can be updated" do
62
+ InfluxDB::Rails.configure do |config|
63
+ config.read_timeout = 5
64
+ end
65
+ expect(InfluxDB::Rails.configuration.read_timeout).to eql(5)
66
+ end
67
+ end
68
+
69
+ describe "#max_delay" do
70
+ it "defaults to 30" do
71
+ expect(InfluxDB::Rails.configuration.max_delay).to eql(30)
72
+ end
73
+
74
+ it "can be updated" do
75
+ InfluxDB::Rails.configure do |config|
76
+ config.max_delay = 5
77
+ end
78
+ expect(InfluxDB::Rails.configuration.max_delay).to eql(5)
79
+ end
80
+ end
81
+
82
+ describe "#time_precision" do
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
88
+ InfluxDB::Rails.configure do |config|
89
+ config.time_precision = "ms"
90
+ end
91
+ expect(InfluxDB::Rails.configuration.time_precision).to eql("ms")
92
+ end
93
+ end
94
+
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
101
+ InfluxDB::Rails.configure do |config|
102
+ config.rails_app_name = "my-app"
103
+ end
104
+
105
+ expect(InfluxDB::Rails.configuration.rails_app_name).to eq("my-app")
106
+ end
107
+ end
108
+
109
+ describe "#tags_middleware" do
110
+ let(:middleware) { InfluxDB::Rails.configuration.tags_middleware }
111
+ let(:tags_example) { { a: 1, b: 2 } }
112
+
113
+ it "by default returns unmodified tags" do
114
+ expect(middleware.call(tags_example)).to eq tags_example
115
+ end
116
+
117
+ it "can be updated" do
118
+ InfluxDB::Rails.configure do |config|
119
+ config.tags_middleware = ->(tags) { tags.merge(c: 3) }
120
+ end
121
+
122
+ expect(middleware.call(tags_example)).to eq(tags_example.merge(c: 3))
123
+ end
124
+ end
125
+ end