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

Add default MAX_RAND value for length 16 & 32 #1693

Merged
merged 2 commits into from
Dec 12, 2022
Merged
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
13 changes: 11 additions & 2 deletions lib/new_relic/agent/guid_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ module Agent
module GuidGenerator
module_function

MAX_RAND_16 = 16**16
MAX_RAND_32 = 16**32
# This method intentionally does not use SecureRandom, because it relies
# on urandom, which raises an exception in MRI when the interpreter runs
# out of allocated file descriptors.
# The guids generated by this method may not be _secure_, but they are
# random enough for our purposes.
def generate_guid(length = 16)
guid = rand(16**length).to_s(16)
guid.length < length ? guid.rjust(length, "0") : guid
guid = case length
when 16
rand(MAX_RAND_16)
when 32
rand(MAX_RAND_32)
else
rand(16**length)
end.to_s(16)
guid.length < length ? guid.rjust(length, '0') : guid
end
end
end
Expand Down