From a41efe37b5bc3dc798f002fd0c125906c893d20c Mon Sep 17 00:00:00 2001 From: Tanna McClure Date: Mon, 6 Mar 2023 15:19:01 -0800 Subject: [PATCH 1/8] add set_user_id api method --- lib/new_relic/agent.rb | 20 ++++++++++++++++++++ lib/new_relic/supportability_helper.rb | 1 + 2 files changed, 21 insertions(+) diff --git a/lib/new_relic/agent.rb b/lib/new_relic/agent.rb index cbafd56336..d012c0db4d 100644 --- a/lib/new_relic/agent.rb +++ b/lib/new_relic/agent.rb @@ -605,6 +605,26 @@ def add_custom_span_attributes(params) end end + # Set the user id for the current transaction + # + # @param [String] user_id The user id to add to the current transaction attributes + # + # @api public + def set_user_id(user_id) + record_api_supportability_metric(:set_user_id) + + if user_id.nil? || user_id.empty? + ::NewRelic::Agent.logger.warn('NewRelic::Agent.set_user_id called with a nil or empty user id.') + return + end + + default_destinations = NewRelic::Agent::AttributeFilter::DST_TRANSACTION_TRACER | + NewRelic::Agent::AttributeFilter::DST_TRANSACTION_EVENTS | + NewRelic::Agent::AttributeFilter::DST_ERROR_COLLECTOR + + NewRelic::Agent::Transaction.add_agent_attribute('enduser.id', user_id, default_destinations) + end + # @!endgroup # @!group Transaction naming diff --git a/lib/new_relic/supportability_helper.rb b/lib/new_relic/supportability_helper.rb index abee118dac..fe97770ce9 100644 --- a/lib/new_relic/supportability_helper.rb +++ b/lib/new_relic/supportability_helper.rb @@ -46,6 +46,7 @@ module SupportabilityHelper :require_test_helper, :set_sql_obfuscator, :set_transaction_name, + :set_user_id, :shutdown, :start_segment, :trace, From 9bb144c47b828976b1bc30fa6317ea0cc764a34c Mon Sep 17 00:00:00 2001 From: Tanna McClure Date: Tue, 7 Mar 2023 15:03:49 -0800 Subject: [PATCH 2/8] add test for user id --- test/new_relic/agent_test.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/new_relic/agent_test.rb b/test/new_relic/agent_test.rb index 2b4a2fa6e0..647d017008 100644 --- a/test/new_relic/agent_test.rb +++ b/test/new_relic/agent_test.rb @@ -73,6 +73,9 @@ def test_configure_agent_applied_server_side_config refute NewRelic::Agent.config[:'transaction_tracer.enabled'] refute NewRelic::Agent.config[:'error_collector.enabled'] end + + # resets the SSC to empty + NewRelic::Agent.config.replace_or_add_config(NewRelic::Agent::Configuration::ServerSource.new({})) end def test_after_fork @@ -529,6 +532,33 @@ def test_modules_and_classes_return_name_properly end end + def test_set_user_id_attribute + test_user = 'test_user_id' + + reset_error_traces! + in_transaction do |txn| + NewRelic::Agent.set_user_id(test_user) + NewRelic::Agent.notice_error(NewRelic::TestHelpers::Exceptions::TestError.new) + end + + [harvest_transaction_events!, harvest_error_events!].each do |events| + assert_equal events[1][0][2]['enduser.id'], test_user + end + end + + def test_set_user_id_nil_or_empty_error + [nil, ''].each do |test_user_id| + in_transaction do |txn| + NewRelic::Agent.set_user_id(nil) + NewRelic::Agent.notice_error(NewRelic::TestHelpers::Exceptions::TestError.new) + end + + [harvest_transaction_events!, harvest_error_events!].each do |events| + refute events[1][0][2].key?('enduser.id') + end + end + end + private def with_unstarted_agent From df8a734e6724672abddd5ddbcc80b7c5724a1086 Mon Sep 17 00:00:00 2001 From: Tanna McClure Date: Tue, 7 Mar 2023 15:21:40 -0800 Subject: [PATCH 3/8] remove unneeded call to reset_error_traces --- test/new_relic/agent_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/new_relic/agent_test.rb b/test/new_relic/agent_test.rb index 647d017008..f91bb99512 100644 --- a/test/new_relic/agent_test.rb +++ b/test/new_relic/agent_test.rb @@ -535,7 +535,6 @@ def test_modules_and_classes_return_name_properly def test_set_user_id_attribute test_user = 'test_user_id' - reset_error_traces! in_transaction do |txn| NewRelic::Agent.set_user_id(test_user) NewRelic::Agent.notice_error(NewRelic::TestHelpers::Exceptions::TestError.new) From 99298ee404e4b74e1abf578d73b6d09e886e4a71 Mon Sep 17 00:00:00 2001 From: Tanna McClure Date: Tue, 7 Mar 2023 15:22:47 -0800 Subject: [PATCH 4/8] use test_user_id variable for user id --- test/new_relic/agent_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/new_relic/agent_test.rb b/test/new_relic/agent_test.rb index f91bb99512..d7d231e119 100644 --- a/test/new_relic/agent_test.rb +++ b/test/new_relic/agent_test.rb @@ -548,7 +548,7 @@ def test_set_user_id_attribute def test_set_user_id_nil_or_empty_error [nil, ''].each do |test_user_id| in_transaction do |txn| - NewRelic::Agent.set_user_id(nil) + NewRelic::Agent.set_user_id(test_user_id) NewRelic::Agent.notice_error(NewRelic::TestHelpers::Exceptions::TestError.new) end From 78c3d992cc926df755bf94d246681e4590a94348 Mon Sep 17 00:00:00 2001 From: Tanna McClure Date: Wed, 8 Mar 2023 13:42:26 -0800 Subject: [PATCH 5/8] Update lib/new_relic/agent.rb Co-authored-by: Kayla Reopelle (she/her) <87386821+kaylareopelle@users.noreply.github.com> --- lib/new_relic/agent.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/new_relic/agent.rb b/lib/new_relic/agent.rb index d012c0db4d..cb07751e3e 100644 --- a/lib/new_relic/agent.rb +++ b/lib/new_relic/agent.rb @@ -605,7 +605,7 @@ def add_custom_span_attributes(params) end end - # Set the user id for the current transaction + # Set the user id for the current transaction. When present, this value will be included in the agent attributes for transaction and error events as 'enduser.id'. # # @param [String] user_id The user id to add to the current transaction attributes # From 3d588a784c2d689dc19055e37bf29fd0dc3fbc5c Mon Sep 17 00:00:00 2001 From: Tanna McClure Date: Wed, 8 Mar 2023 16:36:03 -0800 Subject: [PATCH 6/8] Update lib/new_relic/agent.rb Co-authored-by: James Bunch --- lib/new_relic/agent.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/new_relic/agent.rb b/lib/new_relic/agent.rb index cb07751e3e..58852a5012 100644 --- a/lib/new_relic/agent.rb +++ b/lib/new_relic/agent.rb @@ -622,7 +622,7 @@ def set_user_id(user_id) NewRelic::Agent::AttributeFilter::DST_TRANSACTION_EVENTS | NewRelic::Agent::AttributeFilter::DST_ERROR_COLLECTOR - NewRelic::Agent::Transaction.add_agent_attribute('enduser.id', user_id, default_destinations) + NewRelic::Agent::Transaction.add_agent_attribute(:'enduser.id', user_id, default_destinations) end # @!endgroup From a6ddc3a36dfdaaaf1f0d4cdbaae8db5501d923a7 Mon Sep 17 00:00:00 2001 From: Tanna McClure Date: Wed, 8 Mar 2023 16:36:12 -0800 Subject: [PATCH 7/8] Update test/new_relic/agent_test.rb Co-authored-by: James Bunch --- test/new_relic/agent_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/new_relic/agent_test.rb b/test/new_relic/agent_test.rb index d7d231e119..cb1d116992 100644 --- a/test/new_relic/agent_test.rb +++ b/test/new_relic/agent_test.rb @@ -553,7 +553,7 @@ def test_set_user_id_nil_or_empty_error end [harvest_transaction_events!, harvest_error_events!].each do |events| - refute events[1][0][2].key?('enduser.id') + refute events[1][0][2].key?(:'enduser.id') end end end From e2226c78fccda02bc5db3fc9b247300b0cf37237 Mon Sep 17 00:00:00 2001 From: Tanna McClure Date: Wed, 8 Mar 2023 16:36:19 -0800 Subject: [PATCH 8/8] Update test/new_relic/agent_test.rb Co-authored-by: James Bunch --- test/new_relic/agent_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/new_relic/agent_test.rb b/test/new_relic/agent_test.rb index cb1d116992..c47a63cac7 100644 --- a/test/new_relic/agent_test.rb +++ b/test/new_relic/agent_test.rb @@ -541,7 +541,7 @@ def test_set_user_id_attribute end [harvest_transaction_events!, harvest_error_events!].each do |events| - assert_equal events[1][0][2]['enduser.id'], test_user + assert_equal events[1][0][2][:'enduser.id'], test_user end end