-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2252 from newrelic/fix-active-support-logger-inst…
…rumentation Rails 7.1: Record only one copy of a broadcasted log message
- Loading branch information
Showing
16 changed files
with
417 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
lib/new_relic/agent/instrumentation/active_support_broadcast_logger.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
require_relative 'active_support_broadcast_logger/instrumentation' | ||
require_relative 'active_support_broadcast_logger/chain' | ||
require_relative 'active_support_broadcast_logger/prepend' | ||
|
||
DependencyDetection.defer do | ||
named :'active_support_broadcast_logger' | ||
|
||
depends_on { defined?(ActiveSupport::BroadcastLogger) } | ||
|
||
executes do | ||
NewRelic::Agent.logger.info('Installing ActiveSupport::BroadcastLogger instrumentation') | ||
|
||
if use_prepend? | ||
prepend_instrument ActiveSupport::BroadcastLogger, NewRelic::Agent::Instrumentation::ActiveSupportBroadcastLogger::Prepend | ||
else | ||
chain_instrument NewRelic::Agent::Instrumentation::ActiveSupportBroadcastLogger::Chain | ||
end | ||
end | ||
end |
69 changes: 69 additions & 0 deletions
69
lib/new_relic/agent/instrumentation/active_support_broadcast_logger/chain.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
module NewRelic::Agent::Instrumentation | ||
module ActiveSupportBroadcastLogger::Chain | ||
def self.instrument! | ||
::ActiveSupportBroadcastLogger.class_eval do | ||
include NewRelic::Agent::Instrumentation::ActiveSupportBroadcastLogger | ||
|
||
alias_method(:add_without_new_relic, :add) | ||
|
||
def add(*args, &task) | ||
record_one_broadcast_with_new_relic(*args) do | ||
add_without_new_relic(*args, &traced_task) | ||
end | ||
end | ||
|
||
alias_method(:debug_without_new_relic, :debug) | ||
|
||
def debug(*args, &task) | ||
record_one_broadcast_with_new_relic(*args) do | ||
debug_without_new_relic(*args, &traced_task) | ||
end | ||
end | ||
|
||
alias_method(:info_without_new_relic, :info) | ||
|
||
def info(*args, &task) | ||
record_one_broadcast_with_new_relic(*args) do | ||
info_without_new_relic(*args, &traced_task) | ||
end | ||
end | ||
|
||
alias_method(:warn_without_new_relic, :warn) | ||
|
||
def warn(*args, &task) | ||
record_one_broadcast_with_new_relic(*args) do | ||
warn_without_new_relic(*args, &traced_task) | ||
end | ||
end | ||
|
||
alias_method(:error_without_new_relic, :error) | ||
|
||
def error(*args, &task) | ||
record_one_broadcast_with_new_relic(*args) do | ||
error_without_new_relic(*args, &traced_task) | ||
end | ||
end | ||
|
||
alias_method(:fatal_without_new_relic, :fatal) | ||
|
||
def fatal(*args, &task) | ||
record_one_broadcast_with_new_relic(*args) do | ||
fatal_without_new_relic(*args, &traced_task) | ||
end | ||
end | ||
end | ||
|
||
alias_method(:unknown_without_new_relic, :unknown) | ||
|
||
def unknown(*args, &task) | ||
record_one_broadcast_with_new_relic(*args) do | ||
unknown_without_new_relic(*args, &traced_task) | ||
end | ||
end | ||
end | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
lib/new_relic/agent/instrumentation/active_support_broadcast_logger/instrumentation.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
module NewRelic::Agent::Instrumentation | ||
module ActiveSupportBroadcastLogger | ||
def record_one_broadcast_with_new_relic(*args) | ||
broadcasts[1..-1].each { |broadcasted_logger| broadcasted_logger.instance_variable_set(:@skip_instrumenting, true) } | ||
yield | ||
broadcasts.each { |broadcasted_logger| broadcasted_logger.instance_variable_set(:@skip_instrumenting, false) } | ||
end | ||
end | ||
end |
37 changes: 37 additions & 0 deletions
37
lib/new_relic/agent/instrumentation/active_support_broadcast_logger/prepend.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
module NewRelic::Agent::Instrumentation | ||
module ActiveSupportBroadcastLogger::Prepend | ||
include NewRelic::Agent::Instrumentation::ActiveSupportBroadcastLogger | ||
|
||
def add(*args) | ||
record_one_broadcast_with_new_relic(*args) { super } | ||
end | ||
|
||
def debug(*args) | ||
record_one_broadcast_with_new_relic(*args) { super } | ||
end | ||
|
||
def info(*args) | ||
record_one_broadcast_with_new_relic(*args) { super } | ||
end | ||
|
||
def warn(*args) | ||
record_one_broadcast_with_new_relic(*args) { super } | ||
end | ||
|
||
def error(*args) | ||
record_one_broadcast_with_new_relic(*args) { super } | ||
end | ||
|
||
def fatal(*args) | ||
record_one_broadcast_with_new_relic(*args) { super } | ||
end | ||
|
||
def unknown(*args) | ||
record_one_broadcast_with_new_relic(*args) { super } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
test/multiverse/suites/active_support_broadcast_logger/Envfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
instrumentation_methods :chain, :prepend | ||
|
||
# ActiveSupport::BroadcastLogger introduced in Rails 7.1. | ||
# Rails 7.1 is the latest version at the time of writing. | ||
ACTIVE_SUPPORT_VERSIONS = [ | ||
[nil, 2.7] | ||
] | ||
|
||
unshift_rails_edge(ACTIVE_SUPPORT_VERSIONS) | ||
|
||
def gem_list(activesupport_version = nil) | ||
<<-RB | ||
gem 'activesupport'#{activesupport_version} | ||
RB | ||
end | ||
|
||
create_gemfiles(ACTIVE_SUPPORT_VERSIONS) |
63 changes: 63 additions & 0 deletions
63
...multiverse/suites/active_support_broadcast_logger/active_support_broadcast_logger_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
require 'active_support' | ||
|
||
class ActiveSupportBroadcastLoggerTest < Minitest::Test | ||
include MultiverseHelpers | ||
|
||
MESSAGE = 'Can you hear me, Major Tom?' | ||
|
||
def setup | ||
NewRelic::Agent.manual_start | ||
|
||
@output = StringIO.new | ||
@io_logger = Logger.new(@output) | ||
@output2 = StringIO.new | ||
@io_logger2 = Logger.new(@output2) | ||
@broadcast = ActiveSupport::BroadcastLogger.new(@io_logger, @io_logger2) | ||
@aggregator = NewRelic::Agent.agent.log_event_aggregator | ||
|
||
@aggregator.reset! | ||
end | ||
|
||
def teardown | ||
NewRelic::Agent.shutdown | ||
end | ||
|
||
def test_broadcasted_logger_sends_one_log_event_per_add_call | ||
@broadcast.add(Logger::DEBUG, MESSAGE) | ||
|
||
assert_log_broadcasted_to_both_outputs | ||
assert_log_seen_once_by_new_relic('DEBUG') | ||
end | ||
|
||
def test_broadcasted_logger_sends_one_log_event_per_unknown_call | ||
@broadcast.unknown(MESSAGE) | ||
|
||
assert_log_broadcasted_to_both_outputs | ||
assert_log_seen_once_by_new_relic('ANY') | ||
end | ||
|
||
%w[debug info warn error fatal].each do |method| | ||
define_method("test_broadcasted_logger_sends_one_log_event_per_#{method}_call") do | ||
@broadcast.send(method.to_sym, MESSAGE) | ||
|
||
assert_log_broadcasted_to_both_outputs | ||
assert_log_seen_once_by_new_relic(method.upcase) | ||
end | ||
end | ||
|
||
private | ||
|
||
def assert_log_broadcasted_to_both_outputs | ||
assert_includes(@output.string, MESSAGE) | ||
assert_includes(@output2.string, MESSAGE) | ||
end | ||
|
||
def assert_log_seen_once_by_new_relic(severity) | ||
assert_equal(1, @aggregator.instance_variable_get(:@seen)) | ||
assert_equal({severity => 1}, @aggregator.instance_variable_get(:@seen_by_severity)) | ||
end | ||
end |
32 changes: 32 additions & 0 deletions
32
test/multiverse/suites/active_support_broadcast_logger/config/newrelic.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
common: &default_settings | ||
license_key: 'bd0e1d52adade840f7ca727d29a86249e89a6f1c' | ||
ca_bundle_path: ../../../config/test.cert.crt | ||
host: localhost | ||
api_host: localhost | ||
port: <%= $collector && $collector.port %> | ||
app_name: Rails multiverse test app | ||
enabled: true | ||
apdex_t: 1.0 | ||
capture_params: true | ||
transaction_tracer: | ||
enabled: true | ||
transaction_threshold: apdex_f | ||
record_sql: obfuscated | ||
stack_trace_threshold: 0.500 | ||
error_collector: | ||
enabled: true | ||
ignore_classes: NewRelic::TestHelpers::Exceptions::IgnoredError | ||
instrumentation: | ||
active_support_broadcast_logger <%= $instrumentation_method %> | ||
|
||
development: | ||
<<: *default_settings | ||
|
||
test: | ||
<<: *default_settings | ||
|
||
production: | ||
<<: *default_settings | ||
|
||
staging: | ||
<<: *default_settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
instrumentation_methods :chain, :prepend | ||
|
||
ACTIVE_SUPPORT_VERSIONS = [ | ||
['7.0.4', 2.7], | ||
['6.1.7', 2.5], | ||
['6.0.6', 2.5, 2.7], | ||
['5.2.8', 2.4, 2.7], | ||
['5.1.7', 2.4, 2.7], | ||
['5.0.7', 2.4, 2.7], | ||
['4.2.11', 2.4, 2.4] | ||
] | ||
|
||
def gem_list(activesupport_version = nil) | ||
<<-RB | ||
gem 'activesupport'#{activesupport_version} | ||
RB | ||
end | ||
|
||
create_gemfiles(ACTIVE_SUPPORT_VERSIONS) |
Oops, something went wrong.