influxdb-rails 1.0.0.beta3 → 1.0.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -0
  3. data/CHANGELOG.md +12 -10
  4. data/README.md +193 -168
  5. data/influxdb-rails.gemspec +13 -7
  6. data/lib/influxdb/rails/configuration.rb +88 -192
  7. data/lib/influxdb/rails/context.rb +19 -1
  8. data/lib/influxdb/rails/instrumentation.rb +4 -4
  9. data/lib/influxdb/rails/middleware/render_subscriber.rb +7 -0
  10. data/lib/influxdb/rails/middleware/request_subscriber.rb +22 -24
  11. data/lib/influxdb/rails/middleware/simple_subscriber.rb +12 -25
  12. data/lib/influxdb/rails/middleware/sql_subscriber.rb +7 -3
  13. data/lib/influxdb/rails/middleware/subscriber.rb +19 -8
  14. data/lib/influxdb/rails/railtie.rb +29 -32
  15. data/lib/influxdb/rails/version.rb +1 -1
  16. data/lib/influxdb-rails.rb +20 -81
  17. data/lib/rails/generators/influxdb/influxdb_generator.rb +1 -1
  18. data/lib/rails/generators/influxdb/templates/initializer.rb +39 -9
  19. data/sample-dashboard/Dockerfile +25 -0
  20. data/sample-dashboard/README.md +74 -0
  21. data/sample-dashboard/Rakefile +8 -0
  22. data/sample-dashboard/Ruby On Rails Performance (per Request).json +1053 -0
  23. data/sample-dashboard/Ruby On Rails Performance.json +2011 -0
  24. data/sample-dashboard/docker-compose.yml +33 -0
  25. data/sample-dashboard/provisioning/grafana-dashboards.yml +12 -0
  26. data/sample-dashboard/provisioning/grafana-datasource.yml +10 -0
  27. data/sample-dashboard/provisioning/performance-request.json +1053 -0
  28. data/sample-dashboard/provisioning/performance.json +2011 -0
  29. data/spec/integration/metrics_spec.rb +12 -13
  30. data/spec/shared_examples/data.rb +61 -0
  31. data/spec/spec_helper.rb +3 -1
  32. data/spec/support/rails4/app.rb +4 -0
  33. data/spec/support/rails5/app.rb +4 -0
  34. data/spec/unit/configuration_spec.rb +47 -65
  35. data/spec/unit/middleware/render_subscriber_spec.rb +13 -9
  36. data/spec/unit/middleware/request_subscriber_spec.rb +33 -21
  37. data/spec/unit/middleware/sql_subscriber_spec.rb +35 -8
  38. metadata +42 -30
  39. data/lib/influxdb/rails/air_traffic_controller.rb +0 -41
  40. data/lib/influxdb/rails/backtrace.rb +0 -44
  41. data/lib/influxdb/rails/exception_presenter.rb +0 -94
  42. data/lib/influxdb/rails/logger.rb +0 -16
  43. data/lib/influxdb/rails/middleware/hijack_render_exception.rb +0 -16
  44. data/lib/influxdb/rails/middleware/hijack_rescue_action_everywhere.rb +0 -31
  45. data/lib/influxdb/rails/rack.rb +0 -24
  46. data/spec/integration/exceptions_spec.rb +0 -37
  47. data/spec/shared_examples/tags.rb +0 -42
  48. data/spec/unit/backtrace_spec.rb +0 -85
  49. data/spec/unit/exception_presenter_spec.rb +0 -23
  50. data/spec/unit/influxdb_rails_spec.rb +0 -78
@@ -1,5 +1,5 @@
1
1
  module InfluxDB
2
2
  module Rails
3
- VERSION = "1.0.0.beta3".freeze
3
+ VERSION = "1.0.0.beta4".freeze
4
4
  end
5
5
  end
@@ -7,12 +7,8 @@ require "influxdb/rails/middleware/request_subscriber"
7
7
  require "influxdb/rails/middleware/sql_subscriber"
8
8
  require "influxdb/rails/sql/query"
9
9
  require "influxdb/rails/version"
10
- require "influxdb/rails/logger"
11
- require "influxdb/rails/exception_presenter"
12
10
  require "influxdb/rails/configuration"
13
- require "influxdb/rails/backtrace"
14
11
  require "influxdb/rails/context"
15
- require "influxdb/rails/rack"
16
12
 
17
13
  require "influxdb/rails/railtie" if defined?(Rails::Railtie)
18
14
 
@@ -21,41 +17,38 @@ module InfluxDB
21
17
  # InfluxDB and Rails. This is a singleton class.
22
18
  module Rails
23
19
  class << self
24
- include InfluxDB::Rails::Logger
25
-
26
20
  attr_writer :configuration
27
21
  attr_writer :client
28
22
 
29
- def configure(_silent = false)
30
- yield(configuration)
31
-
32
- # if we change configuration, reload the client
33
- self.client = nil
23
+ def configure
24
+ return configuration unless block_given?
34
25
 
35
- InfluxDB::Logging.logger = configuration.logger unless configuration.logger.nil?
26
+ yield configuration
27
+ self.client = nil # if we change configuration, reload the client
36
28
  end
37
29
 
38
30
  # rubocop:disable Metrics/MethodLength
39
- # rubocop:disable Metrics/AbcSize
40
31
 
41
32
  def client
42
- @client ||= InfluxDB::Client.new \
43
- database: configuration.influxdb_database,
44
- username: configuration.influxdb_username,
45
- password: configuration.influxdb_password,
46
- hosts: configuration.influxdb_hosts,
47
- port: configuration.influxdb_port,
48
- async: configuration.async,
49
- use_ssl: configuration.use_ssl,
50
- retry: configuration.retry,
51
- open_timeout: configuration.open_timeout,
52
- read_timeout: configuration.read_timeout,
53
- max_delay: configuration.max_delay,
54
- time_precision: configuration.time_precision
33
+ @client ||= begin
34
+ cfg = configuration.client
35
+ InfluxDB::Client.new \
36
+ database: cfg.database,
37
+ username: cfg.username,
38
+ password: cfg.password,
39
+ hosts: cfg.hosts,
40
+ port: cfg.port,
41
+ async: cfg.async,
42
+ use_ssl: cfg.use_ssl,
43
+ retry: cfg.retry,
44
+ open_timeout: cfg.open_timeout,
45
+ read_timeout: cfg.read_timeout,
46
+ max_delay: cfg.max_delay,
47
+ time_precision: cfg.time_precision
48
+ end
55
49
  end
56
50
 
57
51
  # rubocop:enable Metrics/MethodLength
58
- # rubocop:enable Metrics/AbcSize
59
52
 
60
53
  def configuration
61
54
  @configuration ||= InfluxDB::Rails::Configuration.new
@@ -64,60 +57,6 @@ module InfluxDB
64
57
  def current
65
58
  @current ||= InfluxDB::Rails::Context.new
66
59
  end
67
-
68
- def report_exception_unless_ignorable(ex, env = {})
69
- report_exception(ex, env) unless ignorable_exception?(ex)
70
- end
71
- alias transmit_unless_ignorable report_exception_unless_ignorable
72
-
73
- # rubocop:disable Metrics/MethodLength
74
- # rubocop:disable Metrics/AbcSize
75
-
76
- def report_exception(ex, env = {})
77
- timestamp = InfluxDB::Rails.current_timestamp
78
- env = influxdb_request_data if env.empty? && defined? influxdb_request_data
79
- exception_presenter = ExceptionPresenter.new(ex, env)
80
- log :info, "Exception: #{exception_presenter.to_json[0..512]}..."
81
- tags = configuration.tags_middleware.call(
82
- exception_presenter.context.merge(exception_presenter.dimensions)
83
- )
84
-
85
- client.write_point \
86
- configuration.series_name_for_exceptions,
87
- values: exception_presenter.values.merge(ts: timestamp),
88
- tags: tags,
89
- timestamp: timestamp
90
- rescue StandardError => ex
91
- log :info, "[InfluxDB::Rails] Something went terribly wrong." \
92
- " Exception failed to take off! #{ex.class}: #{ex.message}"
93
- end
94
- alias transmit report_exception
95
-
96
- # rubocop:enable Metrics/MethodLength
97
- # rubocop:enable Metrics/AbcSize
98
-
99
- def current_timestamp
100
- InfluxDB.now(configuration.time_precision)
101
- end
102
-
103
- def ignorable_exception?(ex)
104
- configuration.ignore_current_environment? || configuration.ignore_exception?(ex)
105
- end
106
-
107
- def rescue
108
- yield
109
- rescue StandardError => ex
110
- raise ex if configuration.ignore_current_environment?
111
-
112
- transmit_unless_ignorable(ex)
113
- end
114
-
115
- def rescue_and_reraise
116
- yield
117
- rescue StandardError => ex
118
- transmit_unless_ignorable(ex)
119
- raise ex
120
- end
121
60
  end
122
61
  end
123
62
  end
@@ -1,6 +1,6 @@
1
1
  require "rails/generators"
2
2
 
3
- class InfluxdbGenerator < Rails::Generators::Base # rubocop:disable Style/Documentation
3
+ class InfluxdbGenerator < Rails::Generators::Base
4
4
  desc "Description:\n This creates a Rails initializer for InfluxDB::Rails."
5
5
 
6
6
  source_root File.expand_path("templates", __dir__)
@@ -1,11 +1,41 @@
1
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"
2
+ ## The only setting you actually need to update is the name of the
3
+ ## database within the InfluxDB server instance. Don't forget to
4
+ ## create this database as well.
5
+ config.client.database = "rails"
6
+ # config.client.hosts = ["localhost"]
7
+ # config.client.port = 8086
8
+
9
+ ## If you've setup user authentication (and activated it in the server
10
+ ## config), you need to configure the credentials here.
11
+ # config.client.username = "root"
12
+ # config.client.password = "root"
13
+
14
+ ## Various other client and connection options. These are used to create
15
+ ## an `InfluxDB::Client` instance (i.e. `InfluxDB::Rails.client`).
16
+ ##
17
+ ## See docs for the influxdb gem for the canonical list of options:
18
+ ## https://github.com/influxdata/influxdb-ruby#list-of-configuration-options
19
+ ##
20
+ ## These defaults for the influxdb-rails gem deviate from the default
21
+ ## for the influxdb gem:
22
+ # config.client.async = true # false
23
+ # config.client.read_timeout = 30 # 300
24
+ # config.client.max_delay = 300 # 30
25
+ # config.client.time_precision = "ms" # "s"
26
+
27
+ ## Prior to 1.0.0, this gem has written all data points in different
28
+ ## measurements (the config options were named `series_name_for_*`).
29
+ ## Since 1.0.0.beta3, we're now using a single measurements
30
+ # config.measurement_name = "rails"
31
+
32
+ ## Disable rails framework hooks.
33
+ # config.ignored_hooks = ['sql.active_record', 'render_template.action_view']
34
+
35
+ # Modify tags on the fly.
36
+ # config.tags_middleware = ->(tags) { tags }
37
+
38
+ ## Set the application name to something meaningful, by default we
39
+ ## infer the app name from the Rails.application class name.
40
+ # config.application_name = Rails.application.class.parent_name
11
41
  end
@@ -0,0 +1,25 @@
1
+ FROM opensuse/leap:15.1
2
+
3
+ ENV NOKOGIRI_USE_SYSTEM_LIBRARIES 1
4
+ RUN useradd -g users -p rails -d /home/rails -m rails
5
+ RUN echo 'rails ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
6
+
7
+ RUN zypper -n addrepo -f https://dl.yarnpkg.com/rpm/yarn.repo; zypper --gpg-auto-import-keys refresh
8
+ RUN zypper -n install --no-recommends ruby2.5-devel nodejs10 make gcc timezone sudo pkg-config sqlite3-devel libxml2-devel libxslt-devel yarn git-core curl
9
+ RUN zypper -n clean -a
10
+ RUN gem install --no-format-executable --no-document rails -v '~> 6'
11
+
12
+ USER rails
13
+ WORKDIR /home/rails
14
+
15
+ RUN rails new thingiverse;
16
+
17
+ WORKDIR /home/rails/thingiverse
18
+ RUN rails g scaffold thing name:string amount:integer
19
+ RUN echo 'gem "influxdb-rails", :git => "https://github.com/influxdata/influxdb-rails/", :branch => "master"' >> Gemfile
20
+ RUN bundle install; rails webpacker:install; rake db:migrate
21
+ RUN sed -i '2i \ root to: "things#index"' config/routes.rb
22
+ RUN bundle exec rails generate influxdb
23
+ RUN curl -o app/controllers/application_controller.rb https://gist.githubusercontent.com/hennevogel/296e93b9db620e9cf48eeb30cef6a145/raw/8c6d4635ded3bef2ca9cfdf65c5863cf6f03a984/application_controller.rb
24
+ RUN sed -i '2i \ config.client.hosts = "influx"' config/initializers/influxdb_rails.rb
25
+ CMD bundle exec rails server -b 0.0.0.0 -p 4000
@@ -0,0 +1,74 @@
1
+ # Ruby On Rails Performance Dashboard
2
+
3
+ A dashboard providing Ruby on Rails performance insights based on
4
+ [Free Software](https://www.fsf.org/about/what-is-free-software), ready to
5
+ run inside your data-center.
6
+
7
+ ![Screenshot of the dashboard](https://grafana.com/api/dashboards/10428/images/6557/image)
8
+
9
+ By default it measures (in various forms):
10
+
11
+ - Controller Action Runtime
12
+ - View/Partial Render Runtime
13
+ - Database Query Runtime
14
+
15
+ It provides an overview and you can also drill down into numbers on a per request basis. Of course you can use all the awesome features that Influx (Downsampling/Data Retention), Grafana (Alerts, Annotations) and influxdb-rails (custom tags) provide and extend this to your needs. Use your freedom and run, copy, distribute, study, change and improve this software!
16
+
17
+ ## Requirements
18
+
19
+ To be able to measure performance you need the following things available:
20
+
21
+ - [InfluxDB 1.x](https://docs.influxdata.com/influxdb/v1.7/introduction/installation/)
22
+ - [Grafana](https://grafana.com/docs/)
23
+ - A [Ruby On Rails](https://rubyonrails.org/) application with [influxdb-rails](https://github.com/influxdata/influxdb-rails) enabled
24
+
25
+ ## Installation
26
+
27
+ Once you have influx/grafana instances running in your infrastructure just [import both
28
+ dashboards from grafana](https://grafana.com/docs/reference/export_import/#importing-a-dashboard).
29
+
30
+ - [Overview Dashboard](https://grafana.com/dashboards/10428)
31
+ - [Request Dashboard](https://grafana.com/dashboards/10429)
32
+
33
+ You can also paste the `.json` files from this repository.
34
+
35
+ In the unlikely case that you need to change the dashboard *UID*s during import you can configure the *UID* the `Overview` dashboard uses to link to the `Request` dashboard in the [variables](https://grafana.com/docs/reference/templating/#adding-a-variable). Just paste whatever *UID* you've set up for the `Request` dashboard.
36
+
37
+ ## Demo
38
+
39
+ This repository includes a [docker-compose](https://docs.docker.com/compose/) demo setup that brings a simple rails app, influxdb and grafana.
40
+
41
+ ### Starting the demo services
42
+
43
+ Clone this repository and run
44
+
45
+ ```shell
46
+ docker-compose up
47
+ ```
48
+
49
+ ### Browse the sample app...
50
+
51
+ Go to http://0.0.0.0:4000 and do some things. Every request to the rails app will generate performance data in the demo.
52
+
53
+ ### ...or Configure your own Rails app...
54
+
55
+ You can also use the dashboard with any other rails app you already have. Follow our [install instructions](https://github.com/influxdata/influxdb-rails/#installation), the default configuration works with the demo InfluxDB running on localhost:8086.
56
+
57
+ To be able to view individual requests you have to enable request ID tags in your application. Something like:
58
+
59
+ ```ruby
60
+ class ApplicationController < ActionController::Base
61
+
62
+ before_action :set_influx_data
63
+
64
+ def set_influx_data
65
+ InfluxDB::Rails.current.values = { request: request.request_id }
66
+ end
67
+ end
68
+ ```
69
+
70
+ ### ...then see the dashboards in action
71
+
72
+ Just go to http://0.0.0.0:3000 and log in with admin/admin.
73
+
74
+ Enjoy! ❤️
@@ -0,0 +1,8 @@
1
+ task default: %w[prepare]
2
+
3
+ # rubocop:disable Metrics/LineLength
4
+ task :prepare do
5
+ sh "cat 'Ruby On Rails Performance.json' | sed 's/${DS_INFLUXDB}/InfluxDB/g' > provisioning/performance.json"
6
+ sh "cat 'Ruby On Rails Performance (per Request).json' | sed 's/${DS_INFLUXDB}/InfluxDB/g' > provisioning/performance-request.json"
7
+ end
8
+ # rubocop:enable Metrics/LineLength