influxdb-rails 1.0.0.beta3 → 1.0.0.beta4
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -0
- data/CHANGELOG.md +12 -10
- data/README.md +193 -168
- data/influxdb-rails.gemspec +13 -7
- data/lib/influxdb/rails/configuration.rb +88 -192
- data/lib/influxdb/rails/context.rb +19 -1
- data/lib/influxdb/rails/instrumentation.rb +4 -4
- data/lib/influxdb/rails/middleware/render_subscriber.rb +7 -0
- data/lib/influxdb/rails/middleware/request_subscriber.rb +22 -24
- data/lib/influxdb/rails/middleware/simple_subscriber.rb +12 -25
- data/lib/influxdb/rails/middleware/sql_subscriber.rb +7 -3
- data/lib/influxdb/rails/middleware/subscriber.rb +19 -8
- data/lib/influxdb/rails/railtie.rb +29 -32
- data/lib/influxdb/rails/version.rb +1 -1
- data/lib/influxdb-rails.rb +20 -81
- data/lib/rails/generators/influxdb/influxdb_generator.rb +1 -1
- data/lib/rails/generators/influxdb/templates/initializer.rb +39 -9
- data/sample-dashboard/Dockerfile +25 -0
- data/sample-dashboard/README.md +74 -0
- data/sample-dashboard/Rakefile +8 -0
- data/sample-dashboard/Ruby On Rails Performance (per Request).json +1053 -0
- data/sample-dashboard/Ruby On Rails Performance.json +2011 -0
- data/sample-dashboard/docker-compose.yml +33 -0
- data/sample-dashboard/provisioning/grafana-dashboards.yml +12 -0
- data/sample-dashboard/provisioning/grafana-datasource.yml +10 -0
- data/sample-dashboard/provisioning/performance-request.json +1053 -0
- data/sample-dashboard/provisioning/performance.json +2011 -0
- data/spec/integration/metrics_spec.rb +12 -13
- data/spec/shared_examples/data.rb +61 -0
- data/spec/spec_helper.rb +3 -1
- data/spec/support/rails4/app.rb +4 -0
- data/spec/support/rails5/app.rb +4 -0
- data/spec/unit/configuration_spec.rb +47 -65
- data/spec/unit/middleware/render_subscriber_spec.rb +13 -9
- data/spec/unit/middleware/request_subscriber_spec.rb +33 -21
- data/spec/unit/middleware/sql_subscriber_spec.rb +35 -8
- metadata +42 -30
- data/lib/influxdb/rails/air_traffic_controller.rb +0 -41
- data/lib/influxdb/rails/backtrace.rb +0 -44
- data/lib/influxdb/rails/exception_presenter.rb +0 -94
- data/lib/influxdb/rails/logger.rb +0 -16
- data/lib/influxdb/rails/middleware/hijack_render_exception.rb +0 -16
- data/lib/influxdb/rails/middleware/hijack_rescue_action_everywhere.rb +0 -31
- data/lib/influxdb/rails/rack.rb +0 -24
- data/spec/integration/exceptions_spec.rb +0 -37
- data/spec/shared_examples/tags.rb +0 -42
- data/spec/unit/backtrace_spec.rb +0 -85
- data/spec/unit/exception_presenter_spec.rb +0 -23
- data/spec/unit/influxdb_rails_spec.rb +0 -78
@@ -1,27 +1,26 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + "/integration_helper")
|
2
2
|
|
3
|
-
RSpec.describe
|
4
|
-
render_views
|
5
|
-
|
3
|
+
RSpec.describe "User visits widgets", type: :request do
|
6
4
|
before do
|
7
5
|
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
|
8
6
|
end
|
9
7
|
|
10
8
|
describe "in a normal request" do
|
11
9
|
it "should result in attempts to write metrics via the client" do
|
12
|
-
expect(InfluxDB::Rails.client).to receive(:write_point).exactly(
|
13
|
-
get
|
10
|
+
expect(InfluxDB::Rails.client).to receive(:write_point).exactly(5).times
|
11
|
+
get "/widgets"
|
14
12
|
end
|
15
13
|
|
16
|
-
context "
|
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
|
-
|
14
|
+
context "additional values" do
|
22
15
|
it "should result in attempts to write metrics via the client" do
|
23
|
-
|
24
|
-
|
16
|
+
allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
|
17
|
+
expect(InfluxDB::Rails.client).to receive(:write_point).with(
|
18
|
+
"rails", a_hash_including(
|
19
|
+
tags: a_hash_including(method: "WidgetsController#index", hook: "process_action"),
|
20
|
+
values: a_hash_including(request_id: :request_id, key: :value)
|
21
|
+
)
|
22
|
+
)
|
23
|
+
get "/widgets"
|
25
24
|
end
|
26
25
|
end
|
27
26
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec.shared_examples_for "with additional data" do
|
4
|
+
context "values" do
|
5
|
+
let(:additional_values) do
|
6
|
+
{ another: :value }
|
7
|
+
end
|
8
|
+
|
9
|
+
after do
|
10
|
+
InfluxDB::Rails.current.reset
|
11
|
+
end
|
12
|
+
|
13
|
+
it "does include the tags" do
|
14
|
+
InfluxDB::Rails.current.values = additional_values
|
15
|
+
|
16
|
+
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(config.measurement_name, hash_including(values: hash_including(another: :value)))
|
17
|
+
|
18
|
+
subject.call("unused", start, finish, "unused", payload)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "tags" do
|
23
|
+
context "when tags_middleware is overwritten" do
|
24
|
+
before do
|
25
|
+
allow(config).to receive(:tags_middleware).and_return(tags_middleware)
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:tags_middleware) { ->(tags) { tags.merge(static: "value", nil: nil, empty: "") } }
|
29
|
+
|
30
|
+
it "processes tags throught the middleware" do
|
31
|
+
tags = data[:tags].merge(static: "value")
|
32
|
+
|
33
|
+
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(config.measurement_name, include(tags: tags))
|
34
|
+
|
35
|
+
subject.call("unused", start, finish, "unused", payload)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "when tags are set in the current context" do
|
40
|
+
let(:input) do
|
41
|
+
{ another: :value, nil: nil, empty: "" }
|
42
|
+
end
|
43
|
+
let(:output) do
|
44
|
+
{ another: :value }
|
45
|
+
end
|
46
|
+
|
47
|
+
after do
|
48
|
+
InfluxDB::Rails.current.reset
|
49
|
+
end
|
50
|
+
|
51
|
+
it "does include the tags" do
|
52
|
+
InfluxDB::Rails.current.tags = input
|
53
|
+
tags = data[:tags].merge(output)
|
54
|
+
|
55
|
+
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(config.measurement_name, include(tags: tags))
|
56
|
+
|
57
|
+
subject.call("unused", start, finish, "unused", payload)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -19,6 +19,8 @@ puts "Loading Rails v#{Rails.version}..."
|
|
19
19
|
require "support/rails#{Rails::VERSION::MAJOR}/app"
|
20
20
|
require "rspec/rails"
|
21
21
|
|
22
|
+
require "pry"
|
23
|
+
|
22
24
|
RSpec.configure do |config|
|
23
25
|
# use expect syntax
|
24
26
|
config.disable_monkey_patching!
|
@@ -26,6 +28,6 @@ RSpec.configure do |config|
|
|
26
28
|
# reset configuration for each spec
|
27
29
|
config.before :each do
|
28
30
|
InfluxDB::Rails.instance_variable_set :@configuration, nil
|
29
|
-
InfluxDB::Rails.configure
|
31
|
+
InfluxDB::Rails.configure
|
30
32
|
end
|
31
33
|
end
|
data/spec/support/rails4/app.rb
CHANGED
@@ -32,6 +32,10 @@ class ApplicationController < ActionController::Base; end
|
|
32
32
|
class WidgetsController < ApplicationController
|
33
33
|
prepend_view_path File.join(__dir__, "..", "views")
|
34
34
|
|
35
|
+
before_action do
|
36
|
+
InfluxDB::Rails.current.values = { key: :value }
|
37
|
+
end
|
38
|
+
|
35
39
|
def index
|
36
40
|
Widget.create!(title: "test")
|
37
41
|
end
|
data/spec/support/rails5/app.rb
CHANGED
@@ -32,6 +32,10 @@ class ApplicationController < ActionController::Base; end
|
|
32
32
|
class WidgetsController < ApplicationController
|
33
33
|
prepend_view_path File.join(__dir__, "..", "views")
|
34
34
|
|
35
|
+
before_action do
|
36
|
+
InfluxDB::Rails.current.values = { key: :value }
|
37
|
+
end
|
38
|
+
|
35
39
|
def index
|
36
40
|
Widget.create!(title: "test")
|
37
41
|
end
|
@@ -5,90 +5,72 @@ RSpec.describe InfluxDB::Rails::Configuration do
|
|
5
5
|
@configuration = InfluxDB::Rails::Configuration.new
|
6
6
|
end
|
7
7
|
|
8
|
-
describe "
|
9
|
-
|
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
|
8
|
+
describe "client configuration" do
|
9
|
+
subject { InfluxDB::Rails.configuration.client }
|
23
10
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
describe "#retry" do
|
31
|
-
it "defaults to nil" do
|
32
|
-
expect(InfluxDB::Rails.configuration.retry).to be_nil
|
33
|
-
end
|
11
|
+
describe "#retry" do
|
12
|
+
it "defaults to nil" do
|
13
|
+
expect(subject.retry).to be_nil
|
14
|
+
end
|
34
15
|
|
35
|
-
|
36
|
-
|
37
|
-
|
16
|
+
it "can be updated" do
|
17
|
+
InfluxDB::Rails.configure do |config|
|
18
|
+
config.client.retry = 5
|
19
|
+
end
|
20
|
+
expect(subject.retry).to eql(5)
|
38
21
|
end
|
39
|
-
expect(InfluxDB::Rails.configuration.retry).to eql(5)
|
40
22
|
end
|
41
|
-
end
|
42
23
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
24
|
+
describe "#open_timeout" do
|
25
|
+
it "defaults to 5" do
|
26
|
+
expect(subject.open_timeout).to eql(5)
|
27
|
+
end
|
47
28
|
|
48
|
-
|
49
|
-
|
50
|
-
|
29
|
+
it "can be updated" do
|
30
|
+
InfluxDB::Rails.configure do |config|
|
31
|
+
config.client.open_timeout = 5
|
32
|
+
end
|
33
|
+
expect(subject.open_timeout).to eql(5)
|
51
34
|
end
|
52
|
-
expect(InfluxDB::Rails.configuration.open_timeout).to eql(5)
|
53
35
|
end
|
54
|
-
end
|
55
36
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
37
|
+
describe "#read_timeout" do
|
38
|
+
it "defaults to 300" do
|
39
|
+
expect(subject.read_timeout).to eql(300)
|
40
|
+
end
|
60
41
|
|
61
|
-
|
62
|
-
|
63
|
-
|
42
|
+
it "can be updated" do
|
43
|
+
InfluxDB::Rails.configure do |config|
|
44
|
+
config.client.read_timeout = 5
|
45
|
+
end
|
46
|
+
expect(subject.read_timeout).to eql(5)
|
64
47
|
end
|
65
|
-
expect(InfluxDB::Rails.configuration.read_timeout).to eql(5)
|
66
48
|
end
|
67
|
-
end
|
68
49
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
50
|
+
describe "#max_delay" do
|
51
|
+
it "defaults to 30" do
|
52
|
+
expect(subject.max_delay).to eql(30)
|
53
|
+
end
|
73
54
|
|
74
|
-
|
75
|
-
|
76
|
-
|
55
|
+
it "can be updated" do
|
56
|
+
InfluxDB::Rails.configure do |config|
|
57
|
+
config.client.max_delay = 5
|
58
|
+
end
|
59
|
+
expect(subject.max_delay).to eql(5)
|
77
60
|
end
|
78
|
-
expect(InfluxDB::Rails.configuration.max_delay).to eql(5)
|
79
61
|
end
|
80
|
-
end
|
81
62
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
63
|
+
describe "#time_precision" do
|
64
|
+
it "defaults to seconds" do
|
65
|
+
expect(subject.time_precision).to eql("s")
|
66
|
+
end
|
86
67
|
|
87
|
-
|
88
|
-
|
89
|
-
|
68
|
+
it "can be updated" do
|
69
|
+
InfluxDB::Rails.configure do |config|
|
70
|
+
config.client.time_precision = "ms"
|
71
|
+
end
|
72
|
+
expect(subject.time_precision).to eql("ms")
|
90
73
|
end
|
91
|
-
expect(InfluxDB::Rails.configuration.time_precision).to eql("ms")
|
92
74
|
end
|
93
75
|
end
|
94
76
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require "spec_helper"
|
2
|
-
require "shared_examples/
|
2
|
+
require "shared_examples/data"
|
3
3
|
|
4
4
|
RSpec.describe InfluxDB::Rails::Middleware::RenderSubscriber do
|
5
5
|
let(:config) { InfluxDB::Rails::Configuration.new }
|
@@ -8,13 +8,13 @@ RSpec.describe InfluxDB::Rails::Middleware::RenderSubscriber do
|
|
8
8
|
before do
|
9
9
|
allow(config).to receive(:application_name).and_return("my-rails-app")
|
10
10
|
allow(config).to receive(:ignored_environments).and_return([])
|
11
|
-
allow(config).to receive(:time_precision).and_return("ms")
|
11
|
+
allow(config.client).to receive(:time_precision).and_return("ms")
|
12
12
|
end
|
13
13
|
|
14
14
|
describe ".call" do
|
15
15
|
let(:start) { Time.at(1_517_567_368) }
|
16
16
|
let(:finish) { Time.at(1_517_567_370) }
|
17
|
-
let(:
|
17
|
+
let(:hook_name) { "render_partial.action_view" }
|
18
18
|
let(:payload) { { identifier: "index.html", count: 43, cache_hits: 42 } }
|
19
19
|
let(:data) do
|
20
20
|
{
|
@@ -26,12 +26,13 @@ RSpec.describe InfluxDB::Rails::Middleware::RenderSubscriber do
|
|
26
26
|
tags: {
|
27
27
|
filename: "index.html",
|
28
28
|
location: "Foo#bar",
|
29
|
+
hook: "render_partial",
|
29
30
|
},
|
30
31
|
timestamp: 1_517_567_370_000
|
31
32
|
}
|
32
33
|
end
|
33
34
|
|
34
|
-
subject { described_class.new(config,
|
35
|
+
subject { described_class.new(config, hook_name) }
|
35
36
|
|
36
37
|
before do
|
37
38
|
InfluxDB::Rails.current.controller = "Foo"
|
@@ -45,12 +46,12 @@ RSpec.describe InfluxDB::Rails::Middleware::RenderSubscriber do
|
|
45
46
|
context "successfully" do
|
46
47
|
it "writes to InfluxDB" do
|
47
48
|
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(
|
48
|
-
|
49
|
+
config.measurement_name, data
|
49
50
|
)
|
50
51
|
subject.call("name", start, finish, "id", payload)
|
51
52
|
end
|
52
53
|
|
53
|
-
it_behaves_like "with additional
|
54
|
+
it_behaves_like "with additional data"
|
54
55
|
|
55
56
|
context "with an empty value" do
|
56
57
|
before do
|
@@ -60,13 +61,17 @@ RSpec.describe InfluxDB::Rails::Middleware::RenderSubscriber do
|
|
60
61
|
|
61
62
|
it "does not write empty value" do
|
62
63
|
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(
|
63
|
-
|
64
|
+
config.measurement_name, data
|
64
65
|
)
|
65
66
|
subject.call("name", start, finish, "id", payload)
|
66
67
|
end
|
67
68
|
end
|
68
69
|
|
69
70
|
context "disabled" do
|
71
|
+
before do
|
72
|
+
allow(config).to receive(:ignored_hooks).and_return(["render_partial.action_view"])
|
73
|
+
end
|
74
|
+
|
70
75
|
subject { described_class.new(config, nil) }
|
71
76
|
|
72
77
|
it "does not write a data point" do
|
@@ -78,13 +83,12 @@ RSpec.describe InfluxDB::Rails::Middleware::RenderSubscriber do
|
|
78
83
|
|
79
84
|
context "unsuccessfully" do
|
80
85
|
before do
|
81
|
-
allow(config).to receive(:logger).and_return(logger)
|
82
86
|
InfluxDB::Rails.configuration = config
|
83
87
|
end
|
84
88
|
|
85
89
|
it "does log exceptions" do
|
86
90
|
allow_any_instance_of(InfluxDB::Client).to receive(:write_point).and_raise("boom")
|
87
|
-
expect(logger).to receive(:error).with(/boom/)
|
91
|
+
expect(::Rails.logger).to receive(:error).with(/boom/)
|
88
92
|
subject.call("name", start, finish, "id", payload)
|
89
93
|
end
|
90
94
|
end
|
@@ -1,14 +1,15 @@
|
|
1
1
|
require "spec_helper"
|
2
|
-
require "shared_examples/
|
2
|
+
require "shared_examples/data"
|
3
3
|
|
4
4
|
RSpec.describe InfluxDB::Rails::Middleware::RequestSubscriber do
|
5
5
|
let(:config) { InfluxDB::Rails::Configuration.new }
|
6
6
|
|
7
7
|
before do
|
8
|
-
allow(config).to receive(:time_precision).and_return("ms")
|
8
|
+
allow(config.client).to receive(:time_precision).and_return("ms")
|
9
|
+
allow(config).to receive(:environment).and_return("production")
|
9
10
|
end
|
10
11
|
|
11
|
-
subject { described_class.new(config) }
|
12
|
+
subject { described_class.new(config, "process_action.action_controller") }
|
12
13
|
|
13
14
|
describe "#call" do
|
14
15
|
let(:start) { Time.at(1_517_567_368) }
|
@@ -17,10 +18,12 @@ RSpec.describe InfluxDB::Rails::Middleware::RequestSubscriber do
|
|
17
18
|
let(:data) do
|
18
19
|
{
|
19
20
|
values: {
|
20
|
-
|
21
|
+
controller: 2,
|
22
|
+
started: InfluxDB.convert_timestamp(start.utc, config.client.time_precision),
|
21
23
|
},
|
22
24
|
tags: {
|
23
25
|
method: "MyController#show",
|
26
|
+
hook: "process_action",
|
24
27
|
status: 200,
|
25
28
|
format: "*/*",
|
26
29
|
http_method: "GET",
|
@@ -38,52 +41,61 @@ RSpec.describe InfluxDB::Rails::Middleware::RequestSubscriber do
|
|
38
41
|
|
39
42
|
it "sends metrics with taggings and timestamps" do
|
40
43
|
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(
|
41
|
-
|
44
|
+
config.measurement_name, data.deep_merge(values: { controller: 2000, db: 2, view: 2 })
|
42
45
|
)
|
43
|
-
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with("rails.view", data)
|
44
|
-
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with("rails.db", data)
|
45
46
|
|
46
47
|
subject.call("unused", start, finish, "unused", payload)
|
47
48
|
end
|
48
49
|
|
49
|
-
it_behaves_like "with additional
|
50
|
+
it_behaves_like "with additional data", ["requests"]
|
50
51
|
end
|
51
52
|
|
52
53
|
context "application_name is nil" do
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
it "does not add the app_name tag to metrics" do
|
58
|
-
tags = {
|
54
|
+
let(:tags) do
|
55
|
+
{
|
59
56
|
method: "MyController#show",
|
57
|
+
hook: "process_action",
|
60
58
|
status: 200,
|
61
59
|
format: "*/*",
|
62
60
|
http_method: "GET",
|
63
61
|
server: Socket.gethostname,
|
64
62
|
}
|
63
|
+
end
|
64
|
+
|
65
|
+
before do
|
66
|
+
allow(config).to receive(:application_name).and_return(nil)
|
67
|
+
end
|
65
68
|
|
69
|
+
it "does not add the app_name tag to metrics" do
|
66
70
|
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(
|
67
|
-
|
71
|
+
config.measurement_name, data.merge(tags: tags).deep_merge(values: { controller: 2000, db: 2, view: 2 })
|
68
72
|
)
|
69
|
-
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with("rails.view", data.merge(tags: tags))
|
70
|
-
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with("rails.db", data.merge(tags: tags))
|
71
73
|
|
72
74
|
subject.call("unused", start, finish, "unused", payload)
|
73
75
|
end
|
74
76
|
end
|
75
77
|
|
76
78
|
context "not successfull" do
|
77
|
-
let(:logger) { double(:logger) }
|
78
|
-
|
79
79
|
before do
|
80
|
-
allow(config).to receive(:logger).and_return(logger)
|
81
80
|
InfluxDB::Rails.configuration = config
|
82
81
|
end
|
83
82
|
|
84
83
|
it "does log an error" do
|
85
84
|
allow_any_instance_of(InfluxDB::Client).to receive(:write_point).and_raise("boom")
|
86
|
-
expect(logger).to receive(:error).with(/boom/)
|
85
|
+
expect(::Rails.logger).to receive(:error).with(/boom/)
|
86
|
+
subject.call("name", start, finish, "id", payload)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context "disabled" do
|
91
|
+
before do
|
92
|
+
allow(config).to receive(:ignored_hooks).and_return(["process_action.action_controller"])
|
93
|
+
end
|
94
|
+
|
95
|
+
subject { described_class.new(config, "process_action.action_controller") }
|
96
|
+
|
97
|
+
it "does not write a data point" do
|
98
|
+
expect_any_instance_of(InfluxDB::Client).not_to receive(:write_point)
|
87
99
|
subject.call("name", start, finish, "id", payload)
|
88
100
|
end
|
89
101
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require "spec_helper"
|
2
|
-
require "shared_examples/
|
2
|
+
require "shared_examples/data"
|
3
3
|
|
4
4
|
RSpec.describe InfluxDB::Rails::Middleware::SqlSubscriber do
|
5
5
|
let(:config) { InfluxDB::Rails::Configuration.new }
|
@@ -8,13 +8,13 @@ RSpec.describe InfluxDB::Rails::Middleware::SqlSubscriber do
|
|
8
8
|
before do
|
9
9
|
allow(config).to receive(:application_name).and_return("my-rails-app")
|
10
10
|
allow(config).to receive(:ignored_environments).and_return([])
|
11
|
-
allow(config).to receive(:time_precision).and_return("ms")
|
11
|
+
allow(config.client).to receive(:time_precision).and_return("ms")
|
12
12
|
end
|
13
13
|
|
14
14
|
describe ".call" do
|
15
15
|
let(:start) { Time.at(1_517_567_368) }
|
16
16
|
let(:finish) { Time.at(1_517_567_370) }
|
17
|
-
let(:
|
17
|
+
let(:hook_name) { "sql.active_record" }
|
18
18
|
let(:payload) { { sql: "SELECT * FROM POSTS WHERE id = 1", name: "Post Load", binds: %w[1 2 3] } }
|
19
19
|
let(:data) do
|
20
20
|
{
|
@@ -26,13 +26,14 @@ RSpec.describe InfluxDB::Rails::Middleware::SqlSubscriber do
|
|
26
26
|
location: "Foo#bar",
|
27
27
|
operation: "SELECT",
|
28
28
|
class_name: "Post",
|
29
|
+
hook: "sql",
|
29
30
|
name: "Post Load",
|
30
31
|
},
|
31
32
|
timestamp: 1_517_567_370_000
|
32
33
|
}
|
33
34
|
end
|
34
35
|
|
35
|
-
subject { described_class.new(config,
|
36
|
+
subject { described_class.new(config, hook_name) }
|
36
37
|
|
37
38
|
before do
|
38
39
|
InfluxDB::Rails.current.controller = "Foo"
|
@@ -46,7 +47,7 @@ RSpec.describe InfluxDB::Rails::Middleware::SqlSubscriber do
|
|
46
47
|
context "successfully" do
|
47
48
|
it "writes to InfluxDB" do
|
48
49
|
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(
|
49
|
-
|
50
|
+
config.measurement_name, data
|
50
51
|
)
|
51
52
|
subject.call("name", start, finish, "id", payload)
|
52
53
|
end
|
@@ -62,18 +63,44 @@ RSpec.describe InfluxDB::Rails::Middleware::SqlSubscriber do
|
|
62
63
|
end
|
63
64
|
end
|
64
65
|
|
65
|
-
it_behaves_like "with additional
|
66
|
+
it_behaves_like "with additional data", ["sql"]
|
67
|
+
|
68
|
+
context "without location" do
|
69
|
+
before do
|
70
|
+
InfluxDB::Rails.current.reset
|
71
|
+
end
|
72
|
+
|
73
|
+
it "does use the default location" do
|
74
|
+
data[:tags] = data[:tags].merge(location: :raw)
|
75
|
+
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(
|
76
|
+
config.measurement_name, data
|
77
|
+
)
|
78
|
+
subject.call("name", start, finish, "id", payload)
|
79
|
+
end
|
80
|
+
end
|
66
81
|
end
|
67
82
|
|
68
83
|
context "unsuccessfully" do
|
69
84
|
before do
|
70
|
-
allow(config).to receive(:logger).and_return(logger)
|
71
85
|
InfluxDB::Rails.configuration = config
|
72
86
|
end
|
73
87
|
|
74
88
|
it "does log exceptions" do
|
75
89
|
allow_any_instance_of(InfluxDB::Client).to receive(:write_point).and_raise("boom")
|
76
|
-
expect(logger).to receive(:error).with(/boom/)
|
90
|
+
expect(::Rails.logger).to receive(:error).with(/boom/)
|
91
|
+
subject.call("name", start, finish, "id", payload)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "disabled" do
|
96
|
+
before do
|
97
|
+
allow(config).to receive(:ignored_hooks).and_return(["sql.active_record"])
|
98
|
+
end
|
99
|
+
|
100
|
+
subject { described_class.new(config, "sql.active_record") }
|
101
|
+
|
102
|
+
it "does not write a data point" do
|
103
|
+
expect_any_instance_of(InfluxDB::Client).not_to receive(:write_point)
|
77
104
|
subject.call("name", start, finish, "id", payload)
|
78
105
|
end
|
79
106
|
end
|