From 5c091a81a728f3aba9cf6d7f0e75f5f74e73be24 Mon Sep 17 00:00:00 2001 From: tung <63999152+tungmq@users.noreply.github.com> Date: Sat, 10 Dec 2022 14:05:53 +0700 Subject: [PATCH 1/2] Add default MAX_RAND value for length 16 & 32 --- lib/new_relic/agent/guid_generator.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/new_relic/agent/guid_generator.rb b/lib/new_relic/agent/guid_generator.rb index 9c2de8b3dd..1808b4d521 100644 --- a/lib/new_relic/agent/guid_generator.rb +++ b/lib/new_relic/agent/guid_generator.rb @@ -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 From e0774d84e70c3f88e8d68d2b8aa8a90d561f3141 Mon Sep 17 00:00:00 2001 From: Hannah Ramadan <76922290+hannahramadan@users.noreply.github.com> Date: Mon, 12 Dec 2022 13:40:02 -0800 Subject: [PATCH 2/2] Align case / end for style We use Rubocop as our linter. This commit addresses a warning under the Layout/EndAlignment cop. --- lib/new_relic/agent/guid_generator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/new_relic/agent/guid_generator.rb b/lib/new_relic/agent/guid_generator.rb index 1808b4d521..51a09ee9ed 100644 --- a/lib/new_relic/agent/guid_generator.rb +++ b/lib/new_relic/agent/guid_generator.rb @@ -22,7 +22,7 @@ def generate_guid(length = 16) rand(MAX_RAND_32) else rand(16**length) - end.to_s(16) + end.to_s(16) guid.length < length ? guid.rjust(length, '0') : guid end end