Sha256: 8a3be8f83a5b77e528511862a6ed030bdfa8d1a86dd953284c83ba13de22323a

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'ddtrace/utils/database'

module Datadog
  # Utils contains low-level utilities, typically to provide pseudo-random trace IDs.
  module Utils
    STRING_PLACEHOLDER = ''.encode(::Encoding::UTF_8).freeze
    # We use a custom random number generator because we want no interference
    # with the default one. Using the default prng, we could break code that
    # would rely on srand/rand sequences.

    # Return a span id
    def self.next_id
      reset! if was_forked?

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

    def self.reset!
      @pid = Process.pid
      @rnd = Random.new
    end

    def self.was_forked?
      Process.pid != @pid
    end

    private_class_method :reset!, :was_forked?

    reset!

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

      return string if string.size <= size

      string.slice(0, size - omission.size) + omission
    end

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ddtrace-0.13.0.beta1 lib/ddtrace/utils.rb