module OpenTelemetry::Common::Utilities

Utilities contains common helpers.

Constants

STRING_PLACEHOLDER

Public Instance Methods

cleanse_url(url) click to toggle source

Returns a URL string with userinfo removed.

@param [String] url The URL string to cleanse.

@return [String] the cleansed URL.

# File lib/opentelemetry/common/utilities.rb, line 84
def cleanse_url(url)
  cleansed_url = URI.parse(url)
  cleansed_url.password = nil
  cleansed_url.user = nil
  cleansed_url.to_s
rescue URI::Error
  url
end
maybe_timeout(timeout, start_time) click to toggle source

Returns nil if timeout is nil, 0 if timeout has expired, or the remaining (positive) time left in seconds.

@param [Numeric] timeout The timeout in seconds. May be nil. @param [Numeric] start_time Start time for timeout returned

by {timeout_timestamp}.

@return [Numeric] remaining (positive) time left in seconds.

May be nil.
# File lib/opentelemetry/common/utilities.rb, line 24
def maybe_timeout(timeout, start_time)
  return nil if timeout.nil?

  timeout -= (timeout_timestamp - start_time)
  timeout.positive? ? timeout : 0
end
timeout_timestamp() click to toggle source

Returns a timestamp suitable to pass as the start_time argument to {maybe_timeout}. This has no meaning outside of the current process.

@return [Numeric]

# File lib/opentelemetry/common/utilities.rb, line 36
def timeout_timestamp
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
truncate(string, size) click to toggle source

Truncates a string if it exceeds the size provided.

@param [String] string The string to be truncated @param [Integer] size The max size of the string

@return [String]

# File lib/opentelemetry/common/utilities.rb, line 71
def truncate(string, size)
  string.size > size ? "#{string[0...size - 3]}..." : string
end
untraced() { || ... } click to toggle source
# File lib/opentelemetry/common/utilities.rb, line 75
def untraced
  OpenTelemetry::Trace.with_span(OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new)) { yield }
end
utf8_encode(string, binary: false, placeholder: STRING_PLACEHOLDER) click to toggle source

Encodes a string in utf8

@param [String] string The string to be utf8 encoded @param [optional boolean] binary This option is for displaying binary data @param [optional String] placeholder The fallback string to be used if encoding fails

@return [String]

# File lib/opentelemetry/common/utilities.rb, line 47
def utf8_encode(string, binary: false, placeholder: STRING_PLACEHOLDER)
  string = string.to_s

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

  placeholder
end
valid_exporter?(exporter) click to toggle source

Returns true if exporter is a valid exporter.

# File lib/opentelemetry/common/utilities.rb, line 94
def valid_exporter?(exporter)
  exporter && %i[export shutdown force_flush].all? { |m| exporter.respond_to?(m) }
end