influxdb-rails 1.0.0 → 1.0.1.beta1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,13 @@
1
+ module InfluxDB
2
+ module Rails
3
+ class TestClient
4
+ cattr_accessor :metrics do
5
+ []
6
+ end
7
+
8
+ def write_point(name, options = {})
9
+ metrics << options.merge(name: name)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ module InfluxDB
2
+ module Rails
3
+ class Values
4
+ def initialize(values: {}, additional_values: InfluxDB::Rails.current.values)
5
+ @values = values
6
+ @additional_values = additional_values
7
+ end
8
+
9
+ def to_h
10
+ expanded_values.reject do |_, value|
11
+ value.nil? || value == ""
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :additional_values, :values
18
+
19
+ def expanded_values
20
+ values.merge(additional_values)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  module InfluxDB
2
2
  module Rails
3
- VERSION = "1.0.0".freeze
3
+ VERSION = "1.0.1.beta1".freeze
4
4
  end
5
5
  end
@@ -18,7 +18,7 @@ It provides an overview and you can also drill down into numbers on a per reques
18
18
 
19
19
  To be able to measure performance you need the following things available:
20
20
 
21
- - [InfluxDB 1.x](https://docs.influxdata.com/influxdb/v1.7/introduction/installation/)
21
+ - [InfluxDB 1.x](https://docs.influxdata.com/influxdb/v1.8/introduction/install/)
22
22
  - [Grafana](https://grafana.com/docs/)
23
23
  - A [Ruby On Rails](https://rubyonrails.org/) application with [influxdb-rails](https://github.com/influxdata/influxdb-rails) enabled
24
24
 
@@ -0,0 +1,83 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ RSpec.describe "ActionController metrics", type: :request do
4
+ let(:tags_middleware) do
5
+ lambda do |tags|
6
+ tags.merge(tags_middleware: :tags_middleware)
7
+ end
8
+ end
9
+ before do
10
+ allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
11
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
12
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
13
+ end
14
+
15
+ it "writes metric" do
16
+ get "/metrics"
17
+
18
+ expect_metric(
19
+ name: "rails",
20
+ tags: a_hash_including(
21
+ method: "MetricsController#index",
22
+ hook: "process_action",
23
+ status: 200,
24
+ format: :html,
25
+ http_method: "GET",
26
+ additional_tag: :value,
27
+ server: Socket.gethostname,
28
+ app_name: :app_name,
29
+ tags_middleware: :tags_middleware
30
+ ),
31
+ values: a_hash_including(
32
+ additional_value: :value,
33
+ request_id: :request_id,
34
+ view: be_between(1, 30),
35
+ db: be_between(1, 30),
36
+ controller: be_between(1, 30)
37
+ )
38
+ )
39
+ end
40
+
41
+ it "includes correct timestamps" do
42
+ travel_to Time.zone.local(2018, 1, 1, 9, 0, 0)
43
+
44
+ get "/metrics"
45
+
46
+ expect_metric(
47
+ name: "rails",
48
+ tags: a_hash_including(
49
+ method: "MetricsController#index",
50
+ hook: "process_action"
51
+ ),
52
+ values: a_hash_including(
53
+ started: 1_514_797_200
54
+ ),
55
+ timestamp: 1_514_797_200
56
+ )
57
+ end
58
+
59
+ it "does not write metric when hook is ignored" do
60
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_hooks).and_return(["process_action.action_controller"])
61
+
62
+ get "/metrics"
63
+
64
+ expect_no_metric(
65
+ tags: a_hash_including(
66
+ method: "MetricsController#index",
67
+ hook: "process_action"
68
+ )
69
+ )
70
+ end
71
+
72
+ it "does not crash when controller throws and exception" do
73
+ get "/exceptions"
74
+
75
+ expect_metric(
76
+ tags: a_hash_including(
77
+ method: "ExceptionsController#index",
78
+ hook: "process_action",
79
+ exception: "ZeroDivisionError"
80
+ )
81
+ )
82
+ end
83
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ RSpec.describe "ActionView collection metrics", type: :request do
4
+ let(:tags_middleware) do
5
+ lambda do |tags|
6
+ tags.merge(tags_middleware: :tags_middleware)
7
+ end
8
+ end
9
+ before do
10
+ allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
11
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
12
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
13
+ end
14
+
15
+ it "writes metric" do
16
+ get "/metrics"
17
+
18
+ expect_metric(
19
+ name: "rails",
20
+ tags: a_hash_including(
21
+ location: "MetricsController#index",
22
+ hook: "render_collection",
23
+ additional_tag: :value,
24
+ filename: include("spec/support/views/metrics/_item.html.erb"),
25
+ server: Socket.gethostname,
26
+ app_name: :app_name,
27
+ tags_middleware: :tags_middleware
28
+ ),
29
+ values: a_hash_including(
30
+ additional_value: :value,
31
+ count: 3,
32
+ request_id: :request_id,
33
+ value: be_between(1, 30)
34
+ )
35
+ )
36
+ end
37
+
38
+ it "includes correct timestamps" do
39
+ travel_to Time.zone.local(2018, 1, 1, 9, 0, 0)
40
+
41
+ get "/metrics"
42
+
43
+ expect_metric(
44
+ name: "rails",
45
+ tags: a_hash_including(
46
+ location: "MetricsController#index",
47
+ hook: "render_collection"
48
+ ),
49
+ timestamp: 1_514_797_200
50
+ )
51
+ end
52
+
53
+ it "does not write metric when hook is ignored" do
54
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_hooks).and_return(["render_collection.action_view"])
55
+
56
+ get "/metrics"
57
+
58
+ expect_no_metric(
59
+ name: "rails",
60
+ tags: a_hash_including(
61
+ location: "MetricsController#index",
62
+ hook: "render_collection"
63
+ )
64
+ )
65
+ end
66
+ end
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ RSpec.describe "ActionView partial metrics", type: :request do
4
+ let(:tags_middleware) do
5
+ lambda do |tags|
6
+ tags.merge(tags_middleware: :tags_middleware)
7
+ end
8
+ end
9
+ before do
10
+ allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
11
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
12
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
13
+ end
14
+
15
+ it "writes metric" do
16
+ get "/metrics"
17
+
18
+ expect_metric(
19
+ tags: a_hash_including(
20
+ location: "MetricsController#index",
21
+ hook: "render_partial",
22
+ additional_tag: :value,
23
+ filename: include("spec/support/views/metrics/_item.html.erb"),
24
+ server: Socket.gethostname,
25
+ app_name: :app_name,
26
+ tags_middleware: :tags_middleware
27
+ ),
28
+ values: a_hash_including(
29
+ additional_value: :value,
30
+ request_id: :request_id,
31
+ value: be_between(1, 30)
32
+ )
33
+ )
34
+ end
35
+
36
+ it "includes correct timestamps" do
37
+ travel_to Time.zone.local(2018, 1, 1, 9, 0, 0)
38
+
39
+ get "/metrics"
40
+
41
+ expect_metric(
42
+ tags: a_hash_including(
43
+ location: "MetricsController#index",
44
+ hook: "render_partial"
45
+ ),
46
+ timestamp: 1_514_797_200
47
+ )
48
+ end
49
+
50
+ it "does not write metric when hook is ignored" do
51
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_hooks).and_return(["render_partial.action_view"])
52
+
53
+ get "/metrics"
54
+
55
+ expect_no_metric(
56
+ tags: a_hash_including(
57
+ location: "MetricsController#index",
58
+ hook: "render_partial"
59
+ )
60
+ )
61
+ end
62
+ end
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ RSpec.describe "ActionView template metrics", type: :request do
4
+ let(:tags_middleware) do
5
+ lambda do |tags|
6
+ tags.merge(tags_middleware: :tags_middleware)
7
+ end
8
+ end
9
+ before do
10
+ allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
11
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
12
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
13
+ end
14
+
15
+ it "writes metric" do
16
+ get "/metrics"
17
+
18
+ expect_metric(
19
+ tags: a_hash_including(
20
+ location: "MetricsController#index",
21
+ hook: "render_template",
22
+ additional_tag: :value,
23
+ filename: include("spec/support/views/metrics/index.html.erb"),
24
+ server: Socket.gethostname,
25
+ app_name: :app_name,
26
+ tags_middleware: :tags_middleware
27
+ ),
28
+ values: a_hash_including(
29
+ additional_value: :value,
30
+ request_id: :request_id,
31
+ value: be_between(1, 30)
32
+ )
33
+ )
34
+ end
35
+
36
+ it "includes correct timestamps" do
37
+ travel_to Time.zone.local(2018, 1, 1, 9, 0, 0)
38
+
39
+ get "/metrics"
40
+
41
+ expect_metric(
42
+ tags: a_hash_including(
43
+ location: "MetricsController#index",
44
+ hook: "render_template"
45
+ ),
46
+ timestamp: 1_514_797_200
47
+ )
48
+ end
49
+
50
+ it "does not write metric when hook is ignored" do
51
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_hooks).and_return(["render_template.action_view"])
52
+
53
+ get "/metrics"
54
+
55
+ expect_no_metric(
56
+ tags: a_hash_including(
57
+ location: "MetricsController#index",
58
+ hook: "render_template"
59
+ )
60
+ )
61
+ end
62
+ end
@@ -0,0 +1,65 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ RSpec.describe "ActiveJob enqueue metrics", type: :request do
4
+ let(:tags_middleware) do
5
+ lambda do |tags|
6
+ tags.merge(tags_middleware: :tags_middleware)
7
+ end
8
+ end
9
+ before do
10
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
11
+ allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
12
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
13
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
14
+ end
15
+
16
+ it "writes metric" do
17
+ get "/metrics"
18
+
19
+ expect_metric(
20
+ tags: a_hash_including(
21
+ location: "MetricsController#index",
22
+ hook: "enqueue",
23
+ job: "MetricJob",
24
+ queue: "default",
25
+ state: "queued",
26
+ additional_tag: :value,
27
+ server: Socket.gethostname,
28
+ app_name: :app_name,
29
+ tags_middleware: :tags_middleware
30
+ ),
31
+ values: a_hash_including(
32
+ additional_value: :value,
33
+ request_id: :request_id,
34
+ value: 1
35
+ )
36
+ )
37
+ end
38
+
39
+ it "includes correct timestamps" do
40
+ travel_to Time.zone.local(2018, 1, 1, 9, 0, 0)
41
+
42
+ get "/metrics"
43
+
44
+ expect_metric(
45
+ tags: a_hash_including(
46
+ location: "MetricsController#index",
47
+ hook: "enqueue"
48
+ ),
49
+ timestamp: 1_514_797_200
50
+ )
51
+ end
52
+
53
+ it "does not write metric when hook is ignored" do
54
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_hooks).and_return(["enqueue.active_job"])
55
+
56
+ get "/metrics"
57
+
58
+ expect_no_metric(
59
+ tags: a_hash_including(
60
+ location: "MetricsController#index",
61
+ hook: "enqueue"
62
+ )
63
+ )
64
+ end
65
+ end
@@ -0,0 +1,68 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ RSpec.describe "ActiveJobs perform metrics", type: :request do
4
+ let(:tags_middleware) do
5
+ lambda do |tags|
6
+ tags.merge(tags_middleware: :tags_middleware)
7
+ end
8
+ end
9
+ before do
10
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
11
+ allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
12
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
13
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
14
+ end
15
+
16
+ it "writes metric" do
17
+ perform_enqueued_jobs do
18
+ get "/metrics"
19
+ end
20
+
21
+ expect_metric(
22
+ tags: a_hash_including(
23
+ location: "MetricsController#index",
24
+ hook: "perform",
25
+ state: "succeeded",
26
+ job: "MetricJob",
27
+ queue: "default",
28
+ server: Socket.gethostname,
29
+ app_name: :app_name,
30
+ tags_middleware: :tags_middleware
31
+ ),
32
+ values: a_hash_including(
33
+ value: be_between(0, 30)
34
+ )
35
+ )
36
+ end
37
+
38
+ it "includes correct timestamps" do
39
+ travel_to Time.zone.local(2018, 1, 1, 9, 0, 0)
40
+
41
+ perform_enqueued_jobs do
42
+ get "/metrics"
43
+ end
44
+
45
+ expect_metric(
46
+ tags: a_hash_including(
47
+ location: "MetricsController#index",
48
+ hook: "perform"
49
+ ),
50
+ timestamp: 1_514_797_200
51
+ )
52
+ end
53
+
54
+ it "does not write metric when hook is ignored" do
55
+ allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_hooks).and_return(["perform.active_job"])
56
+
57
+ perform_enqueued_jobs do
58
+ get "/metrics"
59
+ end
60
+
61
+ expect_no_metric(
62
+ tags: a_hash_including(
63
+ location: "MetricsController#index",
64
+ hook: "perform"
65
+ )
66
+ )
67
+ end
68
+ end