Skip to content

Commit

Permalink
Merge pull request #103 from ChrisBr/refactoring
Browse files Browse the repository at this point in the history
Several refactorings
  • Loading branch information
hennevogel authored May 5, 2020
2 parents 4c5ea1e + 2e9c63e commit 7df4d5c
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 24 deletions.
2 changes: 0 additions & 2 deletions lib/influxdb/rails/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ def write
return unless enabled?

client.write_point configuration.measurement_name, options
rescue StandardError => e
::Rails.logger.error("[InfluxDB::Rails] Unable to write points: #{e.message}")
end

private
Expand Down
10 changes: 8 additions & 2 deletions lib/influxdb/rails/middleware/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ def initialize(configuration, hook_name)
end

def call(_name, start, finish, _id, payload)
write_metric(start, finish, payload)
rescue StandardError => e
::Rails.logger.error("[InfluxDB::Rails] Unable to write points: #{e.message}")
end

private

def write_metric(start, finish, payload)
InfluxDB::Rails::Metric.new(
values: values(start, finish, payload),
tags: tags(payload),
Expand All @@ -25,8 +33,6 @@ def call(_name, start, finish, _id, payload)
).write
end

private

def tags(*)
raise NotImplementedError, "must be implemented in subclass"
end
Expand Down
1 change: 0 additions & 1 deletion spec/requests/action_controller_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
end
end
before do
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
Expand Down
1 change: 0 additions & 1 deletion spec/requests/action_view_collection_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
end
end
before do
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
Expand Down
1 change: 0 additions & 1 deletion spec/requests/action_view_partial_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
end
end
before do
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
Expand Down
1 change: 0 additions & 1 deletion spec/requests/action_view_template_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
end
end
before do
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
Expand Down
1 change: 0 additions & 1 deletion spec/requests/active_record_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
end
end
before do
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
Expand Down
29 changes: 14 additions & 15 deletions spec/requests/context_spec.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
require File.dirname(__FILE__) + "/../spec_helper"

RSpec.describe "Context", type: :request do
before do
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
end

it "resets the context between requests" do
it "resets the context after a request" do
get "/metrics"

expect_metric(
tags: a_hash_including(
method: "MetricsController#index",
hook: "process_action"
location: "MetricsController#index",
hook: "sql"
)
)

get "/exceptions"
expect(InfluxDB::Rails.current.tags).to be_empty
expect(InfluxDB::Rails.current.values).to be_empty
end

expect_metric(
name: "rails",
tags: a_hash_including(
method: "ExceptionsController#index",
hook: "process_action"
)
)
it "resets the context after a request when exceptioni occurs" do
setup_broken_client

get "/metrics"

expect_no_metric(hook: "process_action")
expect(InfluxDB::Rails.current.tags).to be_empty
expect(InfluxDB::Rails.current.values).to be_empty
end
end
10 changes: 10 additions & 0 deletions spec/requests/logger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require File.dirname(__FILE__) + "/../spec_helper"

RSpec.describe "Logger", type: :request do
it "logs exception" do
setup_broken_client
expect(Rails.logger).to receive(:error).with(/message/).at_least(:once)

get "/metrics"
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
require "active_support"
require File.expand_path(File.dirname(__FILE__) + "/support/matchers")
require File.expand_path(File.dirname(__FILE__) + "/support/broken_client")

ENV["RAILS_ENV"] ||= "test"

Expand Down Expand Up @@ -34,6 +35,8 @@
InfluxDB::Rails.configure

allow(InfluxDB::Rails).to receive(:client).and_return(InfluxDB::Rails::TestClient.new)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])

InfluxDB::Rails::TestClient.metrics.clear
end

Expand All @@ -43,4 +46,5 @@

config.include ActiveSupport::Testing::TimeHelpers
config.include InfluxDB::Rails::Matchers
config.include InfluxDB::Rails::BrokenClient
end
13 changes: 13 additions & 0 deletions spec/support/broken_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require File.expand_path(File.dirname(__FILE__) + "/test_client")

module InfluxDB
module Rails
module BrokenClient
def setup_broken_client
client = double
allow(client).to receive(:write_point).and_raise("message")
allow(InfluxDB::Rails).to receive(:client).and_return(client)
end
end
end
end

0 comments on commit 7df4d5c

Please sign in to comment.