influxdb-rails 1.0.0 → 1.0.1.beta1

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 (62) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +11 -0
  3. data/.gitignore +1 -0
  4. data/.rubocop.yml +1 -1
  5. data/.travis.yml +4 -7
  6. data/CHANGELOG.md +17 -0
  7. data/README.md +47 -5
  8. data/Rakefile +0 -6
  9. data/gemfiles/Gemfile.rails-5.0.x +2 -0
  10. data/gemfiles/Gemfile.rails-6.0.x +10 -0
  11. data/influxdb-rails.gemspec +5 -3
  12. data/lib/influxdb-rails.rb +11 -0
  13. data/lib/influxdb/rails/configuration.rb +8 -12
  14. data/lib/influxdb/rails/context.rb +6 -40
  15. data/lib/influxdb/rails/helpers/rspec_matchers.rb +48 -0
  16. data/lib/influxdb/rails/metric.rb +39 -0
  17. data/lib/influxdb/rails/middleware/active_job_subscriber.rb +67 -0
  18. data/lib/influxdb/rails/middleware/active_record_subscriber.rb +26 -0
  19. data/lib/influxdb/rails/middleware/block_instrumentation_subscriber.rb +24 -0
  20. data/lib/influxdb/rails/middleware/render_subscriber.rb +15 -16
  21. data/lib/influxdb/rails/middleware/request_subscriber.rb +16 -21
  22. data/lib/influxdb/rails/middleware/sql_subscriber.rb +18 -18
  23. data/lib/influxdb/rails/middleware/subscriber.rb +40 -27
  24. data/lib/influxdb/rails/railtie.rb +15 -18
  25. data/lib/influxdb/rails/tags.rb +33 -0
  26. data/lib/influxdb/rails/test_client.rb +13 -0
  27. data/lib/influxdb/rails/values.rb +24 -0
  28. data/lib/influxdb/rails/version.rb +1 -1
  29. data/sample-dashboard/README.md +1 -1
  30. data/spec/requests/action_controller_metrics_spec.rb +83 -0
  31. data/spec/requests/action_view_collection_metrics_spec.rb +66 -0
  32. data/spec/requests/action_view_partial_metrics_spec.rb +62 -0
  33. data/spec/requests/action_view_template_metrics_spec.rb +62 -0
  34. data/spec/requests/active_job_enqueue_metrics_spec.rb +65 -0
  35. data/spec/requests/active_job_perform_metrics_spec.rb +68 -0
  36. data/spec/requests/active_job_perform_start_metrics_spec.rb +68 -0
  37. data/spec/requests/active_record_instantiation_metrics_spec.rb +65 -0
  38. data/spec/requests/active_record_sql_metrics_spec.rb +103 -0
  39. data/spec/requests/block_inistrumentation_spec.rb +64 -0
  40. data/spec/requests/context_spec.rb +27 -0
  41. data/spec/requests/logger_spec.rb +10 -0
  42. data/spec/spec_helper.rb +10 -4
  43. data/spec/support/broken_client.rb +11 -0
  44. data/spec/support/rails5/app.rb +32 -10
  45. data/spec/support/rails6/app.rb +70 -0
  46. data/spec/support/views/{widgets → metrics}/_item.html.erb +0 -0
  47. data/spec/support/views/{widgets → metrics}/index.html.erb +0 -0
  48. data/spec/support/views/metrics/show.html.erb +4 -0
  49. data/spec/unit/block_instrumentation_spec.rb +18 -0
  50. metadata +87 -37
  51. data/gemfiles/Gemfile.rails-4.2.x +0 -7
  52. data/lib/influxdb/rails/instrumentation.rb +0 -34
  53. data/lib/influxdb/rails/middleware/simple_subscriber.rb +0 -33
  54. data/spec/controllers/widgets_controller_spec.rb +0 -15
  55. data/spec/integration/integration_helper.rb +0 -1
  56. data/spec/integration/metrics_spec.rb +0 -27
  57. data/spec/shared_examples/data.rb +0 -61
  58. data/spec/support/rails4/app.rb +0 -48
  59. data/spec/unit/context_spec.rb +0 -40
  60. data/spec/unit/middleware/render_subscriber_spec.rb +0 -96
  61. data/spec/unit/middleware/request_subscriber_spec.rb +0 -103
  62. data/spec/unit/middleware/sql_subscriber_spec.rb +0 -108
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63aa7cd5948c23cbde98aef16b1e6655e1e27e94211aa743626345e07d11db26
4
- data.tar.gz: 64218acdb06b4e28749388c382fb41c7bf1375926d5f0e297740d2735e79530d
3
+ metadata.gz: 0c7bbb899eeb093fce13f1997b9f99eeb4451ca059676cd7b23e44fc29058769
4
+ data.tar.gz: c05d83cb5fbbb99b1b8a34794a174e1c4a1d6193d4aa2560fa3dba22fa665da1
5
5
  SHA512:
6
- metadata.gz: db7242ce0f9a22088340b3f8c708d41e3f60ee3d5e71215f9221e9036c08c8e813eaf75e41f28a1488561ea30a2ae173cbbf92426fcd6de40bdc102399a8e533
7
- data.tar.gz: c1037d0c4df07bc0b4c4ddefab93919e1d2775eb0fea1fcff128c7975fdc6edff4a5d6f419159eb5737add1fc54f0902a9516004b5001d6cde4037979bb39ccb
6
+ metadata.gz: c1681dc59869917c09f4da05beaa880565814a263df81567c33af9ab6b5b529d49e8c7dbf666a9758e998adf08f2f521b2fa4734b80845381b3672c824040450
7
+ data.tar.gz: 420b905817df05e25cda3a22dfa05bf32118e94104915a3aba212dc56f39396f1ff1aadf3261a2018b650b56f1431140829b6142a7324022fea8542413872697
@@ -0,0 +1,11 @@
1
+ version: 2
2
+ updates:
3
+ # Enable version updates for bundler
4
+ - package-ecosystem: "bundler"
5
+ directory: "/"
6
+ schedule:
7
+ interval: "daily"
8
+ allow:
9
+ - dependency-type: "all"
10
+ labels:
11
+ - "dependencies"
data/.gitignore CHANGED
@@ -8,3 +8,4 @@ pkg/*
8
8
  spec/support/rails*/log/*
9
9
  .ruby-*
10
10
  vendor/bundle
11
+ tmp
@@ -11,7 +11,7 @@ AllCops:
11
11
  - 'vendor/bundle/**/*'
12
12
  DisplayCopNames: true
13
13
  StyleGuideCopsOnly: false
14
- TargetRubyVersion: 2.3
14
+ TargetRubyVersion: 2.4
15
15
 
16
16
  Rails:
17
17
  Enabled: false
@@ -7,15 +7,15 @@ before_install:
7
7
  - gem update bundler --no-doc
8
8
  rvm:
9
9
  - ruby-head
10
+ - "2.7"
10
11
  - "2.6"
11
12
  - "2.5"
12
13
  - "2.4"
13
- - "2.3"
14
14
  gemfile:
15
+ - gemfiles/Gemfile.rails-6.0.x
15
16
  - gemfiles/Gemfile.rails-5.2.x
16
17
  - gemfiles/Gemfile.rails-5.1.x
17
18
  - gemfiles/Gemfile.rails-5.0.x
18
- - gemfiles/Gemfile.rails-4.2.x
19
19
  env:
20
20
  - TEST_TASK=spec
21
21
  matrix:
@@ -24,11 +24,8 @@ matrix:
24
24
  include:
25
25
  - { rvm: "2.6", gemfile: "Gemfile", env: [TEST_TASK=rubocop] }
26
26
  exclude:
27
- # Rails < 5 not on MRI 2.4+
28
- - { rvm: ruby-head, gemfile: "gemfiles/Gemfile.rails-4.2.x" }
29
- - { rvm: "2.6", gemfile: "gemfiles/Gemfile.rails-4.2.x" }
30
- - { rvm: "2.5", gemfile: "gemfiles/Gemfile.rails-4.2.x" }
31
- - { rvm: "2.4", gemfile: "gemfiles/Gemfile.rails-4.2.x" }
27
+ # Rails > 5 not on MRI 2.4+
28
+ - { rvm: "2.4", gemfile: "gemfiles/Gemfile.rails-6.0.x" }
32
29
  addons:
33
30
  apt:
34
31
  packages:
@@ -2,6 +2,23 @@
2
2
 
3
3
  For the full commit log, [see here](https://github.com/influxdata/influxdb-rails/commits/master).
4
4
 
5
+ ## v1.0.1.beta1, released 2020-08-21
6
+ - Drop support for Ruby 2.3
7
+ - Drop support for Rails 4.x
8
+ - Add `auth_method` to client configuration (#96, @anlek)
9
+ - Drop undocumented `instrumentation_enabled` setting, use
10
+ `ignored_environments` do disable instrumentation
11
+ - Simplified spec with a PORO test client
12
+ - Implement `instantiation.active_record` subscriber (https://guides.rubyonrails.org/active_support_instrumentation.html#instantiation-active-record)
13
+ - Implement `enqueue.active_job` subscriber (https://guides.rubyonrails.org/active_support_instrumentation.html#enqueue-active-job)
14
+ - Implement `perform_start.active_job` subscriber (https://guides.rubyonrails.org/active_support_instrumentation.html#perform-start-active-job)
15
+ - Implement `perform.active_job` subscriber (https://guides.rubyonrails.org/active_support_instrumentation.html#perform-active-job)
16
+ - Implement block instrumentation `InfluxDB::Rails.instrument do; 1 + 1; end`
17
+ - Record unhandled exceptions as tags for process_action.action_controller
18
+
19
+ ## v1.0.0, released 2019-10-23
20
+ The Final release, no code changes.
21
+
5
22
  ## v1.0.0.beta5, unreleased
6
23
  - Silently eat all dropped configuration options and do not crash
7
24
  - Add per action view to the sample dashboard
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
- > You are looking at the README for the master branch of this gem.
2
- > The latest released version lives in the stable-04 branch,
3
- > [see here](https://github.com/influxdata/influxdb-rails/tree/stable-04#readme)
4
- > for an online version.
1
+ > :warning: You are looking at the README for the master branch of this gem.
2
+ > See the latest [released version (1.0.0)](https://github.com/influxdata/influxdb-rails/tree/v1.0.0#readme)
3
+ > instead.
5
4
 
6
5
  # influxdb-rails
7
6
 
@@ -72,10 +71,15 @@ Reported tags:
72
71
  method: "PostsController#index",
73
72
  http_method: "GET",
74
73
  format: "html",
75
- status: "200"
74
+ status: ["200", ""]
75
+ exception: ["", "ArgumentError"]
76
76
  }
77
77
  ```
78
78
 
79
+ *Note*: If an exception happens during that particular instrumentation the
80
+ `status` will be blank and the tag `exception` will contain the name of the
81
+ exception class.
82
+
79
83
  ### Action View
80
84
 
81
85
  Reported ActiveSupport instrumentation hooks:
@@ -177,6 +181,44 @@ class ApplicationController
177
181
  end
178
182
  ```
179
183
 
184
+ ### Block Instrumentation
185
+ If you want to add custom instrumentation, you can wrap any code into a block instrumentation
186
+
187
+ ```ruby
188
+ InfluxDB::Rails.instrument "expensive_operation", tags: { }, values: { } do
189
+ expensive_operation
190
+ end
191
+ ```
192
+
193
+ Reported tags:
194
+
195
+ ```ruby
196
+ hook: "block_instrumentation"
197
+ server: Socket.gethostname,
198
+ app_name: configuration.application_name,
199
+ location: "PostsController#index",
200
+ name: "expensive_operation"
201
+ ```
202
+
203
+ Reported values:
204
+ ```ruby
205
+ value: 100 # execution time of the block
206
+ ```
207
+
208
+ You can also overwrite the `value`
209
+
210
+ ```ruby
211
+ InfluxDB::Rails.instrument "user_count", values: { value: 1 } do
212
+ User.create(name: 'mickey', surname: 'mouse')
213
+ end
214
+ ```
215
+
216
+ or call it even without a block
217
+
218
+ ```ruby
219
+ InfluxDB::Rails.instrument "temperature", values: { value: 25 }
220
+ ```
221
+
180
222
  ### Custom client configuration
181
223
 
182
224
  The settings named `config.client.*` are used to construct an `InfluxDB::Client`
data/Rakefile CHANGED
@@ -21,12 +21,6 @@ task default: %i[spec rubocop]
21
21
 
22
22
  task "test:all" => :default do
23
23
  Dir.glob("gemfiles/Gemfile.rails-*.x") do |gemfile|
24
- if RUBY_VERSION >= "2.6.0" && gemfile == "gemfiles/Gemfile.rails-4.2.x"
25
- msg = "ignore #{gemfile} on Ruby #{RUBY_VERSION}"
26
- puts RSpec::Core::Formatters::ConsoleCodes.wrap(msg, :yellow)
27
- next
28
- end
29
-
30
24
  puts RSpec::Core::Formatters::ConsoleCodes.wrap(gemfile, :cyan)
31
25
  sh({ "BUNDLE_GEMFILE" => gemfile }, "bundle", "install", "--quiet", "--retry=2", "--jobs=2")
32
26
  sh({ "BUNDLE_GEMFILE" => gemfile }, "bundle", "exec", "rspec")
@@ -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 => '../'
@@ -22,20 +22,22 @@ Gem::Specification.new do |spec|
22
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features|smoke)/})
23
23
  spec.require_paths = ["lib"]
24
24
 
25
- spec.required_ruby_version = ">= 2.3.0"
25
+ spec.required_ruby_version = ">= 2.4.0"
26
26
 
27
27
  spec.add_runtime_dependency "influxdb", "~> 0.6", ">= 0.6.4"
28
- spec.add_runtime_dependency "railties", ">= 4.2"
28
+ spec.add_runtime_dependency "railties", ">= 5.0"
29
29
 
30
+ spec.add_development_dependency "activejob"
30
31
  spec.add_development_dependency "activerecord"
31
32
  spec.add_development_dependency "bundler", ">= 1.0.0"
32
33
  spec.add_development_dependency "fakeweb"
34
+ spec.add_development_dependency "launchy"
33
35
  spec.add_development_dependency "pry"
34
36
  spec.add_development_dependency "rake"
35
37
  spec.add_development_dependency "rdoc"
36
38
  spec.add_development_dependency "rspec"
37
39
  spec.add_development_dependency "rspec-rails", ">= 3.0.0"
38
40
  spec.add_development_dependency "rubocop", "~> 0.61.1"
39
- spec.add_development_dependency "sqlite3", "1.3.13"
41
+ spec.add_development_dependency "sqlite3"
40
42
  spec.add_development_dependency "tzinfo"
41
43
  end
@@ -2,9 +2,12 @@ require "net/http"
2
2
  require "net/https"
3
3
  require "rubygems"
4
4
  require "socket"
5
+ require "influxdb/rails/middleware/block_instrumentation_subscriber"
5
6
  require "influxdb/rails/middleware/render_subscriber"
6
7
  require "influxdb/rails/middleware/request_subscriber"
7
8
  require "influxdb/rails/middleware/sql_subscriber"
9
+ require "influxdb/rails/middleware/active_record_subscriber"
10
+ require "influxdb/rails/middleware/active_job_subscriber"
8
11
  require "influxdb/rails/sql/query"
9
12
  require "influxdb/rails/version"
10
13
  require "influxdb/rails/configuration"
@@ -36,6 +39,7 @@ module InfluxDB
36
39
  database: cfg.database,
37
40
  username: cfg.username,
38
41
  password: cfg.password,
42
+ auth_method: cfg.auth_method,
39
43
  hosts: cfg.hosts,
40
44
  port: cfg.port,
41
45
  async: cfg.async,
@@ -57,6 +61,13 @@ module InfluxDB
57
61
  def current
58
62
  @current ||= InfluxDB::Rails::Context.new
59
63
  end
64
+
65
+ def instrument(name, options = {})
66
+ ActiveSupport::Notifications.instrument "block_instrumentation.influxdb_rails",
67
+ **options.merge(name: name) do
68
+ yield if block_given?
69
+ end
70
+ end
60
71
  end
61
72
  end
62
73
  end
@@ -34,6 +34,7 @@ module InfluxDB
34
34
  username: "root".freeze,
35
35
  password: "root".freeze,
36
36
  database: nil,
37
+ auth_method: "params".freeze,
37
38
  async: true,
38
39
  use_ssl: false,
39
40
  retry: nil,
@@ -53,14 +54,13 @@ module InfluxDB
53
54
  include Configurable
54
55
 
55
56
  set_defaults(
56
- measurement_name: "rails".freeze,
57
- ignored_hooks: [].freeze,
58
- tags_middleware: ->(tags) { tags },
59
- rails_app_name: nil,
60
- ignored_environments: %w[test cucumber selenium].freeze,
61
- environment: ::Rails.env,
62
- debug: false,
63
- instrumentation_enabled: true
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
64
  )
65
65
 
66
66
  # config option set after_initialize
@@ -108,10 +108,6 @@ module InfluxDB
108
108
  @debug
109
109
  end
110
110
 
111
- def instrumentation_enabled?
112
- @instrumentation_enabled
113
- end
114
-
115
111
  def ignore_current_environment?
116
112
  ignored_environments.include?(environment)
117
113
  end
@@ -1,59 +1,25 @@
1
1
  module InfluxDB
2
2
  module Rails
3
3
  class Context
4
- def controller
5
- Thread.current[:_influxdb_rails_controller]
6
- end
7
-
8
- def controller=(value)
9
- Thread.current[:_influxdb_rails_controller] = value
10
- end
11
-
12
- def action
13
- Thread.current[:_influxdb_rails_action]
14
- end
15
-
16
- def action=(value)
17
- Thread.current[:_influxdb_rails_action] = value
18
- end
19
-
20
- def request_id=(value)
21
- Thread.current[:_influxdb_rails_request_id] = value
22
- end
23
-
24
- def request_id
25
- Thread.current[:_influxdb_rails_request_id]
26
- end
27
-
28
- def location
29
- [
30
- controller,
31
- action,
32
- ].reject(&:blank?).join("#")
33
- end
34
-
35
4
  def reset
36
- Thread.current[:_influxdb_rails_controller] = nil
37
- Thread.current[:_influxdb_rails_action] = nil
38
- Thread.current[:_influxdb_rails_tags] = nil
39
- Thread.current[:_influxdb_rails_values] = nil
40
- Thread.current[:_influxdb_rails_request_id] = nil
5
+ Thread.current[:_influxdb_rails_tags] = {}
6
+ Thread.current[:_influxdb_rails_values] = {}
41
7
  end
42
8
 
43
9
  def tags
44
- Thread.current[:_influxdb_rails_tags] || {}
10
+ Thread.current[:_influxdb_rails_tags].to_h
45
11
  end
46
12
 
47
13
  def tags=(tags)
48
- Thread.current[:_influxdb_rails_tags] = tags
14
+ Thread.current[:_influxdb_rails_tags] = self.tags.merge(tags)
49
15
  end
50
16
 
51
17
  def values
52
- Thread.current[:_influxdb_rails_values].to_h.merge(request_id: request_id)
18
+ Thread.current[:_influxdb_rails_values].to_h
53
19
  end
54
20
 
55
21
  def values=(values)
56
- Thread.current[:_influxdb_rails_values] = values
22
+ Thread.current[:_influxdb_rails_values] = self.values.merge(values)
57
23
  end
58
24
  end
59
25
  end
@@ -0,0 +1,48 @@
1
+ require_relative "../test_client"
2
+ require "launchy"
3
+
4
+ module InfluxDB
5
+ module Rails
6
+ module Matchers
7
+ def expect_metric(name: "rails", **options)
8
+ expect(metrics).to include(
9
+ a_hash_including(options.merge(name: name))
10
+ )
11
+ end
12
+
13
+ def expect_no_metric(name: "rails", **options)
14
+ expect(metrics).not_to include(
15
+ a_hash_including(options.merge(name: name))
16
+ )
17
+ end
18
+
19
+ def save_and_open_metrics
20
+ dir = File.join(File.dirname(__FILE__), "..", "..", "tmp")
21
+ FileUtils.mkdir_p(dir)
22
+ file_path = File.join(dir, "metrics.json")
23
+ output = JSON.pretty_generate(metrics)
24
+ File.write(file_path, output, mode: "wb")
25
+ ::Launchy.open(file_path)
26
+ end
27
+
28
+ def metrics
29
+ TestClient.metrics
30
+ end
31
+
32
+ RSpec.configure do |config|
33
+ config.before :each do
34
+ InfluxDB::Rails.instance_variable_set :@configuration, nil
35
+ InfluxDB::Rails.configure
36
+
37
+ allow(InfluxDB::Rails).to receive(:client).and_return(InfluxDB::Rails::TestClient.new)
38
+ allow_any_instance_of(InfluxDB::Rails::Configuration)
39
+ .to receive(:ignored_environments).and_return(%w[development])
40
+
41
+ InfluxDB::Rails::TestClient.metrics.clear
42
+ end
43
+
44
+ config.include InfluxDB::Rails::Matchers
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,39 @@
1
+ require "influxdb/rails/values"
2
+ require "influxdb/rails/tags"
3
+
4
+ module InfluxDB
5
+ module Rails
6
+ class Metric
7
+ def initialize(configuration:, timestamp:, tags: {}, values: {})
8
+ @configuration = configuration
9
+ @timestamp = timestamp
10
+ @tags = tags
11
+ @values = values
12
+ end
13
+
14
+ def write
15
+ client.write_point configuration.measurement_name, options
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :configuration, :tags, :values, :timestamp
21
+
22
+ def options
23
+ {
24
+ values: Values.new(values: values).to_h,
25
+ tags: Tags.new(tags: tags, config: configuration).to_h,
26
+ timestamp: timestamp_with_precision,
27
+ }
28
+ end
29
+
30
+ def timestamp_with_precision
31
+ InfluxDB.convert_timestamp(timestamp.utc, configuration.client.time_precision)
32
+ end
33
+
34
+ def client
35
+ InfluxDB::Rails.client
36
+ end
37
+ end
38
+ end
39
+ end