Skip to content

Commit

Permalink
Merge pull request #1693 from tungmq/dev
Browse files Browse the repository at this point in the history
Add default MAX_RAND value for length 16 & 32
  • Loading branch information
hannahramadan authored Dec 12, 2022
2 parents 2655f4f + e0774d8 commit 0b89d57
Showing 1 changed file with 11 additions and 2 deletions.
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

0 comments on commit 0b89d57

Please sign in to comment.