Skip to content
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

Error inbox add set_user_id API method #1868

Merged
merged 8 commits into from
Mar 13, 2023
Merged
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
20 changes: 20 additions & 0 deletions lib/new_relic/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,26 @@ def add_custom_span_attributes(params)
end
end

# 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
#
# @api public
def set_user_id(user_id)
record_api_supportability_metric(:set_user_id)
fallwith marked this conversation as resolved.
Show resolved Hide resolved

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
Expand Down
1 change: 1 addition & 0 deletions lib/new_relic/supportability_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module SupportabilityHelper
:require_test_helper,
:set_sql_obfuscator,
:set_transaction_name,
:set_user_id,
fallwith marked this conversation as resolved.
Show resolved Hide resolved
:shutdown,
:start_segment,
:trace,
Expand Down
29 changes: 29 additions & 0 deletions test/new_relic/agent_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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({}))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This had to be added because this test setting the server side config to not collect errors was causing intermittent failures in our new tests.

end

def test_after_fork
Expand Down Expand Up @@ -529,6 +532,32 @@ def test_modules_and_classes_return_name_properly
end
end

def test_set_user_id_attribute
test_user = 'test_user_id'

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
hannahramadan marked this conversation as resolved.
Show resolved Hide resolved
[nil, ''].each do |test_user_id|
in_transaction do |txn|
NewRelic::Agent.set_user_id(test_user_id)
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
Expand Down