Module: NewRelic::Agent::GuidGenerator

Defined in:
lib/new_relic/agent/guid_generator.rb

Constant Summary collapse

MAX_RAND_16 =
16**16
MAX_RAND_32 =
16**32

Class Method Summary collapse

Class Method Details

.generate_guid(length = 16) ⇒ Object

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.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/new_relic/agent/guid_generator.rb', line 17

def generate_guid(length = 16)
  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