Skip to content

Added config parameter to configure app name … #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ InfluxDB::Rails.configure do |config|
# config.series_name_for_controller_runtimes = "rails.controller"
# config.series_name_for_view_runtimes = "rails.view"
# config.series_name_for_db_runtimes = "rails.db"

config.rails_app_name = "my-rails-app"
end
```

Expand Down
67 changes: 34 additions & 33 deletions lib/influxdb-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def configure(silent = false)
end

def client
@client ||= InfluxDB::Client.new configuration.influxdb_database,
@client ||= InfluxDB::Client.new(
configuration.influxdb_database,
:username => configuration.influxdb_username,
:password => configuration.influxdb_password,
:hosts => configuration.influxdb_hosts,
Expand All @@ -43,6 +44,7 @@ def client
:read_timeout => configuration.read_timeout,
:max_delay => configuration.max_delay,
:time_precision => configuration.time_precision
)
end

def configuration
Expand Down Expand Up @@ -74,6 +76,7 @@ def report_exception(e, env = {})
log :info, "[InfluxDB::Rails] Something went terribly wrong. Exception failed to take off! #{e.class}: #{e.message}"
end
end

alias_method :transmit, :report_exception

def handle_action_controller_metrics(name, start, finish, id, payload)
Expand All @@ -83,40 +86,38 @@ def handle_action_controller_metrics(name, start, finish, id, payload)
db_runtime = (payload[:db_runtime] || 0).ceil
method = "#{payload[:controller]}##{payload[:action]}"
hostname = Socket.gethostname
app_name = configuration.rails_app_name

begin
client.write_point configuration.series_name_for_controller_runtimes, {
values: {
value: controller_runtime,
},
tags: {
method: method,
server: hostname,
},
timestamp: timestamp,
}
tags = { method: method, server: hostname, app_name: app_name }
tags.delete_if { |k, v| v.nil? }

client.write_point configuration.series_name_for_view_runtimes, {
values: {
value: view_runtime,
},
tags: {
method: method,
server: hostname,
},
timestamp: timestamp,
}

client.write_point configuration.series_name_for_db_runtimes, {
values: {
value: db_runtime,
},
tags: {
method: method,
server: hostname,
},
timestamp: timestamp,
}
begin
client.write_point(
configuration.series_name_for_controller_runtimes,
{
values: { value: controller_runtime },
tags: tags,
timestamp: timestamp,
}
)

client.write_point(
configuration.series_name_for_view_runtimes,
{
values: { value: view_runtime },
tags: tags,
timestamp: timestamp,
}
)

client.write_point(
configuration.series_name_for_db_runtimes,
{
values: { value: db_runtime },
tags: tags,
timestamp: timestamp,
}
)
rescue => e
log :error, "[InfluxDB::Rails] Unable to write points: #{e.message}"
end
Expand Down
6 changes: 6 additions & 0 deletions lib/influxdb/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Configuration
attr_accessor :series_name_for_view_runtimes
attr_accessor :series_name_for_db_runtimes

attr_accessor :rails_app_name

attr_accessor :application_id
deprecate :application_id => "This method serve no purpose and will be removed in the release after 0.1.12"

Expand Down Expand Up @@ -65,6 +67,8 @@ class Configuration
:series_name_for_view_runtimes => "rails.view",
:series_name_for_db_runtimes => "rails.db",

:rails_app_name => nil,

:ignored_exceptions => %w{ActiveRecord::RecordNotFound
ActionController::RoutingError},
:ignored_exception_messages => [],
Expand Down Expand Up @@ -112,6 +116,8 @@ def initialize
@series_name_for_view_runtimes = DEFAULTS[:series_name_for_view_runtimes]
@series_name_for_db_runtimes = DEFAULTS[:series_name_for_db_runtimes]

@rails_app_name = DEFAULTS[:rails_app_name]

@ignored_exceptions = DEFAULTS[:ignored_exceptions].dup
@ignored_exception_messages = DEFAULTS[:ignored_exception_messages].dup
@ignored_reports = DEFAULTS[:ignored_reports].dup
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,18 @@
expect(InfluxDB::Rails.configuration.time_precision).to eql('ms')
end
end

describe "#rails_app_name" do
it 'defaults to nil' do
expect(InfluxDB::Rails.configuration.rails_app_name).to be(nil)
end

it "can be set to own name" do
InfluxDB::Rails.configure do |config|
config.rails_app_name = 'my-app'
end

expect(InfluxDB::Rails.configuration.rails_app_name).to eq('my-app')
end
end
end
57 changes: 56 additions & 1 deletion spec/unit/influxdb_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,62 @@

RSpec.describe InfluxDB::Rails do
before do
InfluxDB::Rails.configure { |config| config.ignored_environments = [] }
InfluxDB::Rails.configure do |config|
config.rails_app_name = 'my-rails-app'
config.ignored_environments = []
end
end

describe '.handle_action_controller_metrics' do
let(:start) { Time.at(1517567368) }
let(:finish) { Time.at(1517567370) }
let(:payload) { { view_runtime: 2, db_runtime: 2, controller: 'MyController', action: 'show' } }
let(:data) {
{
values: {
value: 2
},
tags: {
method: 'MyController#show',
server: Socket.gethostname,
app_name: 'my-rails-app'
},
timestamp: 1517567370000
}
}

context 'rails_app_name is set' do
it 'sends metrics with taggings and timestamps' do
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(
'rails.controller', data.merge({ values: { value: 2000}})
)
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with('rails.view', data)
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with('rails.db', data)

described_class.handle_action_controller_metrics('unused', start, finish, 'unused', payload)
end
end

context 'rails_app_name is nil' do
before do
InfluxDB::Rails.configure do |config|
config.rails_app_name = nil
config.ignored_environments = []
end
end

it 'doesn not add the app_name tag to metrics' do
tags = { method: 'MyController#show', server: Socket.gethostname }

expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with(
'rails.controller', data.merge({ values: { value: 2000}, tags: tags })
)
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with('rails.view', data.merge(tags: tags))
expect_any_instance_of(InfluxDB::Client).to receive(:write_point).with('rails.db', data.merge(tags: tags))

described_class.handle_action_controller_metrics('unused', start, finish, 'unused', payload)
end
end
end

describe '.convert_timestamp' do
Expand Down