influxdb-rails 1.0.0.beta1 → 1.0.1.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +11 -0
  3. data/.gitignore +2 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +7 -1
  6. data/.travis.yml +9 -10
  7. data/CHANGELOG.md +48 -11
  8. data/Gemfile +6 -0
  9. data/README.md +278 -100
  10. data/Rakefile +17 -18
  11. data/gemfiles/Gemfile.rails-5.0.x +2 -0
  12. data/gemfiles/Gemfile.rails-6.0.x +10 -0
  13. data/influxdb-rails.gemspec +20 -10
  14. data/lib/influxdb-rails.rb +35 -118
  15. data/lib/influxdb/rails/configuration.rb +99 -179
  16. data/lib/influxdb/rails/context.rb +26 -0
  17. data/lib/influxdb/rails/helpers/rspec_matchers.rb +48 -0
  18. data/lib/influxdb/rails/metric.rb +39 -0
  19. data/lib/influxdb/rails/middleware/active_job_subscriber.rb +67 -0
  20. data/lib/influxdb/rails/middleware/active_record_subscriber.rb +26 -0
  21. data/lib/influxdb/rails/middleware/block_instrumentation_subscriber.rb +24 -0
  22. data/lib/influxdb/rails/middleware/render_subscriber.rb +32 -0
  23. data/lib/influxdb/rails/middleware/request_subscriber.rb +44 -0
  24. data/lib/influxdb/rails/middleware/sql_subscriber.rb +37 -0
  25. data/lib/influxdb/rails/middleware/subscriber.rb +68 -0
  26. data/lib/influxdb/rails/railtie.rb +28 -25
  27. data/lib/influxdb/rails/sql/normalizer.rb +27 -0
  28. data/lib/influxdb/rails/sql/query.rb +32 -0
  29. data/lib/influxdb/rails/tags.rb +33 -0
  30. data/lib/influxdb/rails/test_client.rb +13 -0
  31. data/lib/influxdb/rails/values.rb +24 -0
  32. data/lib/influxdb/rails/version.rb +1 -1
  33. data/lib/rails/generators/influxdb/influxdb_generator.rb +1 -1
  34. data/lib/rails/generators/influxdb/templates/initializer.rb +39 -9
  35. data/sample-dashboard/Dockerfile +24 -0
  36. data/sample-dashboard/README.md +74 -0
  37. data/sample-dashboard/Rakefile +9 -0
  38. data/sample-dashboard/Ruby On Rails Performance (per Action).json +1576 -0
  39. data/sample-dashboard/Ruby On Rails Performance (per Request).json +1053 -0
  40. data/sample-dashboard/Ruby On Rails Performance.json +2041 -0
  41. data/sample-dashboard/docker-compose.yml +34 -0
  42. data/sample-dashboard/provisioning/grafana-dashboards.yml +12 -0
  43. data/sample-dashboard/provisioning/grafana-datasource.yml +10 -0
  44. data/sample-dashboard/provisioning/performance-action.json +1576 -0
  45. data/sample-dashboard/provisioning/performance-request.json +1053 -0
  46. data/sample-dashboard/provisioning/performance.json +2041 -0
  47. data/spec/requests/action_controller_metrics_spec.rb +83 -0
  48. data/spec/requests/action_view_collection_metrics_spec.rb +66 -0
  49. data/spec/requests/action_view_partial_metrics_spec.rb +62 -0
  50. data/spec/requests/action_view_template_metrics_spec.rb +62 -0
  51. data/spec/requests/active_job_enqueue_metrics_spec.rb +65 -0
  52. data/spec/requests/active_job_perform_metrics_spec.rb +68 -0
  53. data/spec/requests/active_job_perform_start_metrics_spec.rb +68 -0
  54. data/spec/requests/active_record_instantiation_metrics_spec.rb +65 -0
  55. data/spec/requests/active_record_sql_metrics_spec.rb +103 -0
  56. data/spec/requests/block_inistrumentation_spec.rb +64 -0
  57. data/spec/requests/context_spec.rb +27 -0
  58. data/spec/requests/logger_spec.rb +10 -0
  59. data/spec/spec_helper.rb +12 -4
  60. data/spec/support/broken_client.rb +11 -0
  61. data/spec/support/rails5/app.rb +44 -5
  62. data/spec/support/rails6/app.rb +70 -0
  63. data/spec/support/views/metrics/_item.html.erb +1 -0
  64. data/spec/support/views/metrics/index.html.erb +5 -0
  65. data/spec/support/views/metrics/show.html.erb +4 -0
  66. data/spec/unit/block_instrumentation_spec.rb +18 -0
  67. data/spec/unit/configuration_spec.rb +64 -65
  68. data/spec/unit/sql/normalizer_spec.rb +15 -0
  69. data/spec/unit/sql/query_spec.rb +29 -0
  70. metadata +167 -44
  71. data/gemfiles/Gemfile.rails-4.2.x +0 -7
  72. data/lib/influxdb/rails/air_traffic_controller.rb +0 -41
  73. data/lib/influxdb/rails/backtrace.rb +0 -44
  74. data/lib/influxdb/rails/exception_presenter.rb +0 -94
  75. data/lib/influxdb/rails/instrumentation.rb +0 -34
  76. data/lib/influxdb/rails/logger.rb +0 -16
  77. data/lib/influxdb/rails/middleware/hijack_render_exception.rb +0 -16
  78. data/lib/influxdb/rails/middleware/hijack_rescue_action_everywhere.rb +0 -31
  79. data/lib/influxdb/rails/rack.rb +0 -24
  80. data/spec/controllers/widgets_controller_spec.rb +0 -15
  81. data/spec/integration/exceptions_spec.rb +0 -37
  82. data/spec/integration/integration_helper.rb +0 -1
  83. data/spec/integration/metrics_spec.rb +0 -21
  84. data/spec/support/rails4/app.rb +0 -30
  85. data/spec/unit/backtrace_spec.rb +0 -85
  86. data/spec/unit/exception_presenter_spec.rb +0 -23
  87. data/spec/unit/influxdb_rails_spec.rb +0 -164
data/Rakefile CHANGED
@@ -2,28 +2,27 @@ require "bundler/gem_tasks"
2
2
  require "rubocop/rake_task"
3
3
  RuboCop::RakeTask.new
4
4
 
5
- begin
6
- targeted_files = ARGV.drop(1)
7
- file_pattern = targeted_files.empty? ? "spec/**/*_spec.rb" : targeted_files
5
+ targeted_files = ARGV.drop(1)
6
+ file_pattern = targeted_files.empty? ? "spec/**/*_spec.rb" : targeted_files
8
7
 
9
- require "rspec/core"
10
- require "rspec/core/rake_task"
8
+ require "rspec/core"
9
+ require "rspec/core/rake_task"
11
10
 
12
- RSpec::Core::RakeTask.new(:spec) do |t|
13
- t.pattern = FileList[file_pattern]
14
- end
11
+ RSpec::Core::RakeTask.new(:spec) do |t|
12
+ t.pattern = FileList[file_pattern]
13
+ end
15
14
 
16
- RSpec.configure do |config|
17
- config.color = true
18
- config.formatter = :documentation
19
- end
20
- rescue LoadError
21
- require "spec/rake/spectask"
15
+ RSpec.configure do |config|
16
+ config.color = true
17
+ config.formatter = :documentation
18
+ end
22
19
 
23
- puts file_pattern
20
+ task default: %i[spec rubocop]
24
21
 
25
- Spec::Rake::SpecTask.new(:spec) do |t|
26
- t.pattern = FileList[file_pattern]
27
- t.spec_opts += ["--color"]
22
+ task "test:all" => :default do
23
+ Dir.glob("gemfiles/Gemfile.rails-*.x") do |gemfile|
24
+ puts RSpec::Core::Formatters::ConsoleCodes.wrap(gemfile, :cyan)
25
+ sh({ "BUNDLE_GEMFILE" => gemfile }, "bundle", "install", "--quiet", "--retry=2", "--jobs=2")
26
+ sh({ "BUNDLE_GEMFILE" => gemfile }, "bundle", "exec", "rspec")
28
27
  end
29
28
  end
@@ -3,5 +3,7 @@ source "https://rubygems.org"
3
3
  gem 'actionpack', '~> 5.0.0'
4
4
  gem 'activesupport', '~> 5.0.0'
5
5
  gem 'activemodel', '~> 5.0.0'
6
+ gem 'sqlite3', '1.3.13'
7
+
6
8
 
7
9
  gemspec :path => '../'
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'actionpack', '~> 6.0.0'
4
+ gem 'activesupport', '~> 6.0.0'
5
+ gem 'activemodel', '~> 6.0.0'
6
+ gem 'sqlite3', '~> 1.4'
7
+ # FIXME: https://github.com/rspec/rspec-rails/issues/2177
8
+ gem 'rspec-rails', '~> 4.0.0.beta2'
9
+
10
+ gemspec :path => '../'
@@ -4,30 +4,40 @@ require "influxdb/rails/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "influxdb-rails"
7
- spec.version = InfluxDB::Rails::VERSION
8
- spec.authors = ["Dominik Menke", "Todd Persen"]
9
- spec.email = ["dominik.menke@gmail.com", "todd@influxdb.com"]
10
- spec.homepage = "https://influxdata.com"
11
7
  spec.summary = "InfluxDB bindings for Ruby on Rails."
12
- spec.description = "This gem automatically instruments your Ruby on Rails" \
13
- " 4.2/5.x applications using InfluxDB for storage."
8
+ spec.description = "This gem instruments your Ruby on Rails application using InfluxDB."
9
+ spec.version = InfluxDB::Rails::VERSION
10
+ spec.authors = ["Christian Bruckmayer", "Henne Vogelsang"]
11
+ spec.email = ["christian@bruckmayer.net", "hvogel@hennevogel.de"]
14
12
  spec.licenses = ["MIT"]
13
+ spec.homepage = "https://influxdata.com"
14
+ spec.metadata = {
15
+ "bug_tracker_uri" => "https://github.com/influxdata/influxdb-rails/issues",
16
+ "changelog_uri" => "https://github.com/influxdata/influxdb-rails/blob/master/CHANGELOG.md",
17
+ "documentation_uri" => "https://github.com/influxdata/influxdb-rails/blob/master/README.md",
18
+ "source_code_uri" => "https://github.com/influxdata/influxdb-rails",
19
+ }
15
20
 
16
21
  spec.files = `git ls-files`.split($/) # rubocop:disable Style/SpecialGlobalVars
17
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features|smoke)/})
18
23
  spec.require_paths = ["lib"]
19
24
 
20
- spec.required_ruby_version = ">= 2.3.0"
25
+ spec.required_ruby_version = ">= 2.4.0"
21
26
 
22
- spec.add_runtime_dependency "influxdb", "~> 0.5.0"
23
- spec.add_runtime_dependency "railties", "> 3"
27
+ spec.add_runtime_dependency "influxdb", "~> 0.6", ">= 0.6.4"
28
+ spec.add_runtime_dependency "railties", ">= 5.0"
24
29
 
30
+ spec.add_development_dependency "activejob"
31
+ spec.add_development_dependency "activerecord"
25
32
  spec.add_development_dependency "bundler", ">= 1.0.0"
26
33
  spec.add_development_dependency "fakeweb"
34
+ spec.add_development_dependency "launchy"
35
+ spec.add_development_dependency "pry"
27
36
  spec.add_development_dependency "rake"
28
37
  spec.add_development_dependency "rdoc"
29
38
  spec.add_development_dependency "rspec"
30
39
  spec.add_development_dependency "rspec-rails", ">= 3.0.0"
31
- spec.add_development_dependency "rubocop", "~> 0.60.0"
40
+ spec.add_development_dependency "rubocop", "~> 0.61.1"
41
+ spec.add_development_dependency "sqlite3"
32
42
  spec.add_development_dependency "tzinfo"
33
43
  end
@@ -2,12 +2,16 @@ require "net/http"
2
2
  require "net/https"
3
3
  require "rubygems"
4
4
  require "socket"
5
+ require "influxdb/rails/middleware/block_instrumentation_subscriber"
6
+ require "influxdb/rails/middleware/render_subscriber"
7
+ require "influxdb/rails/middleware/request_subscriber"
8
+ require "influxdb/rails/middleware/sql_subscriber"
9
+ require "influxdb/rails/middleware/active_record_subscriber"
10
+ require "influxdb/rails/middleware/active_job_subscriber"
11
+ require "influxdb/rails/sql/query"
5
12
  require "influxdb/rails/version"
6
- require "influxdb/rails/logger"
7
- require "influxdb/rails/exception_presenter"
8
13
  require "influxdb/rails/configuration"
9
- require "influxdb/rails/backtrace"
10
- require "influxdb/rails/rack"
14
+ require "influxdb/rails/context"
11
15
 
12
16
  require "influxdb/rails/railtie" if defined?(Rails::Railtie)
13
17
 
@@ -16,140 +20,53 @@ module InfluxDB
16
20
  # InfluxDB and Rails. This is a singleton class.
17
21
  module Rails
18
22
  class << self
19
- include InfluxDB::Rails::Logger
20
-
21
23
  attr_writer :configuration
22
24
  attr_writer :client
23
25
 
24
- def configure(_silent = false)
25
- yield(configuration)
26
-
27
- # if we change configuration, reload the client
28
- self.client = nil
26
+ def configure
27
+ return configuration unless block_given?
29
28
 
30
- InfluxDB::Logging.logger = configuration.logger unless configuration.logger.nil?
29
+ yield configuration
30
+ self.client = nil # if we change configuration, reload the client
31
31
  end
32
32
 
33
33
  # rubocop:disable Metrics/MethodLength
34
- # rubocop:disable Metrics/AbcSize
35
34
 
36
35
  def client
37
- @client ||= InfluxDB::Client.new \
38
- database: configuration.influxdb_database,
39
- username: configuration.influxdb_username,
40
- password: configuration.influxdb_password,
41
- hosts: configuration.influxdb_hosts,
42
- port: configuration.influxdb_port,
43
- async: configuration.async,
44
- use_ssl: configuration.use_ssl,
45
- retry: configuration.retry,
46
- open_timeout: configuration.open_timeout,
47
- read_timeout: configuration.read_timeout,
48
- max_delay: configuration.max_delay,
49
- time_precision: configuration.time_precision
36
+ @client ||= begin
37
+ cfg = configuration.client
38
+ InfluxDB::Client.new \
39
+ database: cfg.database,
40
+ username: cfg.username,
41
+ password: cfg.password,
42
+ auth_method: cfg.auth_method,
43
+ hosts: cfg.hosts,
44
+ port: cfg.port,
45
+ async: cfg.async,
46
+ use_ssl: cfg.use_ssl,
47
+ retry: cfg.retry,
48
+ open_timeout: cfg.open_timeout,
49
+ read_timeout: cfg.read_timeout,
50
+ max_delay: cfg.max_delay,
51
+ time_precision: cfg.time_precision
52
+ end
50
53
  end
51
54
 
52
55
  # rubocop:enable Metrics/MethodLength
53
- # rubocop:enable Metrics/AbcSize
54
56
 
55
57
  def configuration
56
58
  @configuration ||= InfluxDB::Rails::Configuration.new
57
59
  end
58
60
 
59
- def report_exception_unless_ignorable(ex, env = {})
60
- report_exception(ex, env) unless ignorable_exception?(ex)
61
- end
62
- alias transmit_unless_ignorable report_exception_unless_ignorable
63
-
64
- # rubocop:disable Metrics/MethodLength
65
- # rubocop:disable Metrics/AbcSize
66
-
67
- def report_exception(ex, env = {})
68
- timestamp = InfluxDB::Rails.current_timestamp
69
- env = influxdb_request_data if env.empty? && defined? influxdb_request_data
70
- exception_presenter = ExceptionPresenter.new(ex, env)
71
- log :info, "Exception: #{exception_presenter.to_json[0..512]}..."
72
-
73
- client.write_point \
74
- configuration.series_name_for_exceptions,
75
- values: exception_presenter.values.merge(ts: timestamp),
76
- tags: exception_presenter.context.merge(exception_presenter.dimensions),
77
- timestamp: timestamp
78
- rescue StandardError => ex
79
- log :info, "[InfluxDB::Rails] Something went terribly wrong." \
80
- " Exception failed to take off! #{ex.class}: #{ex.message}"
81
- end
82
- alias transmit report_exception
83
-
84
- def handle_action_controller_metrics(_name, start, finish, _id, payload)
85
- tags = {
86
- method: "#{payload[:controller]}##{payload[:action]}",
87
- status: payload[:status],
88
- format: payload[:format],
89
- http_method: payload[:method],
90
- path: payload[:path],
91
- server: Socket.gethostname,
92
- app_name: configuration.application_name,
93
- }.reject { |_, value| value.nil? }
94
-
95
- ts = convert_timestamp(finish.utc)
96
-
97
- begin
98
- {
99
- configuration.series_name_for_controller_runtimes => ((finish - start) * 1000).ceil,
100
- configuration.series_name_for_view_runtimes => (payload[:view_runtime] || 0).ceil,
101
- configuration.series_name_for_db_runtimes => (payload[:db_runtime] || 0).ceil,
102
- }.each do |series_name, value|
103
- client.write_point series_name, values: { value: value }, tags: tags, timestamp: ts
104
- end
105
- rescue StandardError => e
106
- log :error, "[InfluxDB::Rails] Unable to write points: #{e.message}"
107
- end
61
+ def current
62
+ @current ||= InfluxDB::Rails::Context.new
108
63
  end
109
64
 
110
- # rubocop:enable Metrics/MethodLength
111
- # rubocop:enable Metrics/AbcSize
112
-
113
- TIMESTAMP_CONVERSIONS = {
114
- "ns" => 1e9.to_r,
115
- nil => 1e9.to_r,
116
- "u" => 1e6.to_r,
117
- "ms" => 1e3.to_r,
118
- "s" => 1.to_r,
119
- "m" => 1.to_r / 60,
120
- "h" => 1.to_r / 60 / 60,
121
- }.freeze
122
- private_constant :TIMESTAMP_CONVERSIONS
123
-
124
- def convert_timestamp(time)
125
- conv = TIMESTAMP_CONVERSIONS.fetch(configuration.time_precision) do
126
- raise "Invalid time precision: #{configuration.time_precision}"
65
+ def instrument(name, options = {})
66
+ ActiveSupport::Notifications.instrument "block_instrumentation.influxdb_rails",
67
+ **options.merge(name: name) do
68
+ yield if block_given?
127
69
  end
128
-
129
- (time.to_r * conv).to_i
130
- end
131
-
132
- def current_timestamp
133
- convert_timestamp(Time.now.utc)
134
- end
135
-
136
- def ignorable_exception?(ex)
137
- configuration.ignore_current_environment? || configuration.ignore_exception?(ex)
138
- end
139
-
140
- def rescue
141
- yield
142
- rescue StandardError => ex
143
- raise ex if configuration.ignore_current_environment?
144
-
145
- transmit_unless_ignorable(ex)
146
- end
147
-
148
- def rescue_and_reraise
149
- yield
150
- rescue StandardError => ex
151
- transmit_unless_ignorable(ex)
152
- raise ex
153
70
  end
154
71
  end
155
72
  end
@@ -1,196 +1,116 @@
1
+ require "active_support/concern"
2
+
1
3
  module InfluxDB
2
4
  module Rails
3
- # rubocop:disable Metrics/ClassLength
4
-
5
- class Configuration # rubocop:disable Style/Documentation
6
- attr_accessor :influxdb_hosts
7
- attr_accessor :influxdb_port
8
- attr_accessor :influxdb_username
9
- attr_accessor :influxdb_password
10
- attr_accessor :influxdb_database
11
- attr_accessor :async
12
- attr_accessor :use_ssl
13
- attr_accessor :retry
14
- attr_accessor :open_timeout
15
- attr_accessor :read_timeout
16
- attr_accessor :max_delay
17
- attr_accessor :time_precision
18
-
19
- attr_accessor :series_name_for_controller_runtimes
20
- attr_accessor :series_name_for_view_runtimes
21
- attr_accessor :series_name_for_db_runtimes
22
- attr_accessor :series_name_for_exceptions
23
- attr_accessor :series_name_for_instrumentation
24
-
25
- attr_accessor :rails_app_name
26
-
27
- attr_accessor :application_name
28
- attr_accessor :application_root
29
-
30
- attr_accessor :logger
31
- attr_accessor :environment
32
- attr_accessor :framework
33
- attr_accessor :framework_version
34
- attr_accessor :language
35
- attr_accessor :language_version
36
- attr_accessor :ignored_exceptions
37
- attr_accessor :ignored_exception_messages
38
- attr_accessor :ignored_reports
39
- attr_accessor :ignored_environments
40
- attr_accessor :ignored_user_agents
41
- attr_accessor :backtrace_filters
42
- attr_accessor :aggregated_exception_classes
43
- attr_accessor :environment_variables
44
- attr_accessor :environment_variable_filters
45
-
46
- attr_accessor :instrumentation_enabled
47
- attr_accessor :debug
48
-
49
- DEFAULTS = {
50
- influxdb_hosts: ["localhost"].freeze,
51
- influxdb_port: 8086,
52
- influxdb_username: "root".freeze,
53
- influxdb_password: "root".freeze,
54
- influxdb_database: nil,
55
- async: true,
56
- use_ssl: false,
57
- retry: nil,
58
- open_timeout: 5,
59
- read_timeout: 300,
60
- max_delay: 30,
61
- time_precision: "s".freeze,
62
-
63
- series_name_for_controller_runtimes: "rails.controller".freeze,
64
- series_name_for_view_runtimes: "rails.view".freeze,
65
- series_name_for_db_runtimes: "rails.db".freeze,
66
- series_name_for_exceptions: "rails.exceptions".freeze,
67
- series_name_for_instrumentation: "instrumentation".freeze,
68
-
69
- rails_app_name: nil,
70
-
71
- ignored_exceptions: %w[
72
- ActiveRecord::RecordNotFound
73
- ActionController::RoutingError
74
- ].freeze,
75
-
76
- ignored_exception_messages: [].freeze,
77
- ignored_reports: [].freeze,
78
- ignored_environments: %w[test cucumber selenium].freeze,
79
- ignored_user_agents: %w[GoogleBot].freeze,
80
- environment_variable_filters: [
81
- /password/i,
82
- /key/i,
83
- /secret/i,
84
- /ps1/i,
85
- /rvm_.*_clr/i,
86
- /color/i,
87
- ].freeze,
88
-
89
- backtrace_filters: [
90
- ->(line) { line.gsub(%r{^\./}, "") },
91
- lambda { |line|
92
- return line if InfluxDB::Rails.configuration.application_root.to_s.empty?
93
-
94
- line.gsub(/#{InfluxDB::Rails.configuration.application_root}/, "[APP_ROOT]")
95
- },
96
- lambda { |line|
97
- if defined?(Gem) && !Gem.path.nil? && !Gem.path.empty?
98
- Gem.path.each { |path| line = line.gsub(/#{path}/, "[GEM_ROOT]") }
99
- end
100
- line
101
- },
102
- ].freeze,
103
- }.freeze
104
-
105
- # rubocop:disable Metrics/MethodLength
106
- # rubocop:disable Metrics/AbcSize
107
-
108
- def initialize
109
- @influxdb_hosts = DEFAULTS[:influxdb_hosts]
110
- @influxdb_port = DEFAULTS[:influxdb_port]
111
- @influxdb_username = DEFAULTS[:influxdb_username]
112
- @influxdb_password = DEFAULTS[:influxdb_password]
113
- @influxdb_database = DEFAULTS[:influxdb_database]
114
- @async = DEFAULTS[:async]
115
- @use_ssl = DEFAULTS[:use_ssl]
116
- @retry = DEFAULTS[:retry]
117
- @open_timeout = DEFAULTS[:open_timeout]
118
- @read_timeout = DEFAULTS[:read_timeout]
119
- @max_delay = DEFAULTS[:max_delay]
120
- @time_precision = DEFAULTS[:time_precision]
121
-
122
- @series_name_for_controller_runtimes = DEFAULTS[:series_name_for_controller_runtimes]
123
- @series_name_for_view_runtimes = DEFAULTS[:series_name_for_view_runtimes]
124
- @series_name_for_db_runtimes = DEFAULTS[:series_name_for_db_runtimes]
125
- @series_name_for_exceptions = DEFAULTS[:series_name_for_exceptions]
126
- @series_name_for_instrumentation = DEFAULTS[:series_name_for_instrumentation]
127
-
128
- @rails_app_name = DEFAULTS[:rails_app_name]
129
-
130
- @ignored_exceptions = DEFAULTS[:ignored_exceptions].dup
131
- @ignored_exception_messages = DEFAULTS[:ignored_exception_messages].dup
132
- @ignored_reports = DEFAULTS[:ignored_reports].dup
133
- @ignored_environments = DEFAULTS[:ignored_environments].dup
134
- @ignored_user_agents = DEFAULTS[:ignored_user_agents].dup
135
- @backtrace_filters = DEFAULTS[:backtrace_filters].dup
136
- @environment_variable_filters = DEFAULTS[:environment_variable_filters]
137
- @aggregated_exception_classes = []
138
-
139
- @debug = false
140
- @rescue_global_exceptions = false
141
- @instrumentation_enabled = true
5
+ module Configurable
6
+ extend ActiveSupport::Concern
7
+
8
+ class_methods do
9
+ def defaults
10
+ @defaults ||= {}
11
+ end
12
+
13
+ def set_defaults(**values) # rubocop:disable Naming/AccessorMethodName:
14
+ defaults.merge! values
15
+ attr_accessor(*defaults.keys)
16
+ end
142
17
  end
143
18
 
144
- # rubocop:enable Metrics/MethodLength
145
- # rubocop:enable Metrics/AbcSize
146
-
147
- def debug?
148
- !!@debug # rubocop:disable Style/DoubleNegation
19
+ def load_defaults
20
+ self.class.defaults.each do |key, value|
21
+ val = value.dup rescue value
22
+ public_send "#{key}=", val
23
+ end
149
24
  end
25
+ end
26
+ private_constant :Configurable
27
+
28
+ class ClientConfig
29
+ include Configurable
30
+
31
+ set_defaults(
32
+ hosts: ["localhost"].freeze,
33
+ port: 8086,
34
+ username: "root".freeze,
35
+ password: "root".freeze,
36
+ database: nil,
37
+ auth_method: "params".freeze,
38
+ async: true,
39
+ use_ssl: false,
40
+ retry: nil,
41
+ open_timeout: 5,
42
+ read_timeout: 300,
43
+ max_delay: 30,
44
+ time_precision: "s".freeze
45
+ )
150
46
 
151
- def instrumentation_enabled?
152
- !!@instrumentation_enabled # rubocop:disable Style/DoubleNegation
47
+ def initialize
48
+ load_defaults
153
49
  end
50
+ end
51
+ private_constant :ClientConfig
52
+
53
+ class Configuration
54
+ include Configurable
55
+
56
+ set_defaults(
57
+ measurement_name: "rails".freeze,
58
+ ignored_hooks: [].freeze,
59
+ tags_middleware: ->(tags) { tags },
60
+ rails_app_name: nil,
61
+ ignored_environments: %w[test cucumber selenium].freeze,
62
+ environment: ::Rails.env,
63
+ debug: false
64
+ )
65
+
66
+ # config option set after_initialize
67
+ attr_accessor(:environment, :application_name)
68
+
69
+ # configuration passed to InfluxDB::Client
70
+ attr_reader :client
71
+
72
+ # FIXME: Old configuration options, remove this in 1.0.1
73
+ attr_writer \
74
+ :series_name_for_controller_runtimes,
75
+ :series_name_for_view_runtimes,
76
+ :series_name_for_db_runtimes,
77
+ :series_name_for_render_template,
78
+ :series_name_for_render_partial,
79
+ :series_name_for_render_collection,
80
+ :series_name_for_sql,
81
+ :series_name_for_exceptions,
82
+ :series_name_for_instrumentation,
83
+ :ignored_exceptions,
84
+ :ignored_exception_messages,
85
+ :ignored_user_agents,
86
+ :application_root,
87
+ :environment_variable_filters,
88
+ :backtrace_filters,
89
+ :influxdb_database,
90
+ :influxdb_username,
91
+ :influxdb_password,
92
+ :influxdb_hosts,
93
+ :influxdb_port,
94
+ :async,
95
+ :use_ssl,
96
+ :retry,
97
+ :open_timeout,
98
+ :read_timeout,
99
+ :max_delay,
100
+ :time_precision
154
101
 
155
- def ignore_user_agent?(incoming_user_agent)
156
- return false if ignored_user_agents.nil?
102
+ def initialize
103
+ @client = ClientConfig.new
104
+ load_defaults
105
+ end
157
106
 
158
- ignored_user_agents.any? { |agent| incoming_user_agent =~ /#{agent}/ }
107
+ def debug?
108
+ @debug
159
109
  end
160
110
 
161
111
  def ignore_current_environment?
162
112
  ignored_environments.include?(environment)
163
113
  end
164
-
165
- def ignore_exception?(ex)
166
- !ignored_exception_messages.find { |msg| /.*#{msg}.*/ =~ ex.message }.nil? ||
167
- ignored_exceptions.include?(ex.class.to_s)
168
- end
169
-
170
- def define_custom_exception_data(&block)
171
- @custom_exception_data_handler = block
172
- end
173
-
174
- def add_custom_exception_data(exception_presenter)
175
- @custom_exception_data_handler&.call(exception_presenter)
176
- end
177
-
178
- def load_rails_defaults
179
- @logger ||= ::Rails.logger
180
- @environment ||= ::Rails.env
181
- @application_root ||= ::Rails.root
182
- @application_name ||= ::Rails.application.class.parent_name
183
- @framework = "Rails"
184
- @framework_version = ::Rails.version
185
- end
186
-
187
- private
188
-
189
- def initialize_http_connection
190
- Net::HTTP.new(@app_host, "80")
191
- end
192
114
  end
193
-
194
- # rubocop:enable Metrics/ClassLength
195
115
  end
196
116
  end