Module: Datadog::Utils

Defined in:
lib/ddtrace/utils.rb,
lib/ddtrace/utils/time.rb,
lib/ddtrace/utils/database.rb

Overview

Utils contains low-level utilities, typically to provide pseudo-random trace IDs.

Defined Under Namespace

Modules: Database, Time

Constant Summary collapse

STRING_PLACEHOLDER =
''.encode(::Encoding::UTF_8).freeze

Class Method Summary collapse

Class Method Details

.next_idObject

Return a span id



12
13
14
15
16
# File 'lib/ddtrace/utils.rb', line 12

def self.next_id
  reset! if was_forked?

  @rnd.rand(Datadog::Span::MAX_ID)
end

.truncate(value, size, omission = '...'.freeze) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ddtrace/utils.rb', line 31

def self.truncate(value, size, omission = '...'.freeze)
  string = value.to_s

  return string if string.size <= size

  string = string.slice(0, size - 1)

  if size < omission.size
    string[0, size] = omission
  else
    string[size - omission.size, size] = omission
  end

  string
end

.utf8_encode(str, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ddtrace/utils.rb', line 47

def self.utf8_encode(str, options = {})
  str = str.to_s

  if options[:binary]
    # This option is useful for "gracefully" displaying binary data that
    # often contains text such as marshalled objects
    str.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
  elsif str.encoding == ::Encoding::UTF_8
    str
  else
    str.encode(::Encoding::UTF_8)
  end
rescue => e
  Tracer.log.debug("Error encoding string in UTF-8: #{e}")

  options.fetch(:placeholder, STRING_PLACEHOLDER)
end