Module: OpenTelemetry::Common::Utilities

Extended by:
Utilities
Included in:
Utilities
Defined in:
lib/opentelemetry/common/utilities.rb

Overview

Utilities contains common helpers.

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#cleanse_url(url) ⇒ String

Returns a URL string with userinfo removed.

Parameters:

  • url (String)

    The URL string to cleanse.

Returns:

  • (String)

    the cleansed URL.



68
69
70
71
72
73
74
75
# File 'lib/opentelemetry/common/utilities.rb', line 68

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) ⇒ Object

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



17
18
19
20
21
22
# File 'lib/opentelemetry/common/utilities.rb', line 17

def maybe_timeout(timeout, start_time)
  return nil if timeout.nil?

  timeout -= (Time.now - start_time)
  timeout.positive? ? timeout : 0
end

#truncate(string, size) ⇒ String

Truncates a string if it exceeds the size provided.

Parameters:

  • string (String)

    The string to be truncated

  • size (Integer)

    The max size of the string

Returns:

  • (String)


55
56
57
# File 'lib/opentelemetry/common/utilities.rb', line 55

def truncate(string, size)
  string.size > size ? "#{string[0...size - 3]}..." : string
end

#untracedObject



59
60
61
# File 'lib/opentelemetry/common/utilities.rb', line 59

def untraced
  OpenTelemetry::Trace.with_span(OpenTelemetry::Trace::Span.new) { yield }
end

#utf8_encode(string, binary: false, placeholder: STRING_PLACEHOLDER) ⇒ String

Encodes a string in utf8

Parameters:

  • string (String)

    The string to be utf8 encoded

  • binary (optional boolean) (defaults to: false)

    This option is for displaying binary data

  • placeholder (optional String) (defaults to: STRING_PLACEHOLDER)

    The fallback string to be used if encoding fails

Returns:

  • (String)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/opentelemetry/common/utilities.rb', line 31

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) ⇒ Boolean

Returns true if exporter is a valid exporter.

Returns:

  • (Boolean)


78
79
80
# File 'lib/opentelemetry/common/utilities.rb', line 78

def valid_exporter?(exporter)
  exporter && %i[export shutdown force_flush].all? { |m| exporter.respond_to?(m) }
end