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.



95
96
97
98
99
100
101
102
# File 'lib/opentelemetry/common/utilities.rb', line 95

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

#config_opt(*env_vars, default: nil) ⇒ Object

Returns the first non nil environment variable requested, or the default value if provided.

Parameters:

  • env_vars (String)

    The environment variable(s) to retrieve

  • default (defaults to: nil)

    The fallback value to return if the requested env var(s) are not present



112
113
114
# File 'lib/opentelemetry/common/utilities.rb', line 112

def config_opt(*env_vars, default: nil)
  ENV.values_at(*env_vars).compact.fetch(0, default)
end

#maybe_timeout(timeout, start_time) ⇒ Numeric

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

Parameters:

  • timeout (Numeric)

    The timeout in seconds. May be nil.

  • start_time (Numeric)

    Start time for timeout returned by #timeout_timestamp.

Returns:

  • (Numeric)

    remaining (positive) time left in seconds. May be nil.



24
25
26
27
28
29
# 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_timestampNumeric

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

Returns:

  • (Numeric)


36
37
38
# File 'lib/opentelemetry/common/utilities.rb', line 36

def timeout_timestamp
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
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)


71
72
73
# File 'lib/opentelemetry/common/utilities.rb', line 71

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

#truncate_attribute_value(value, limit) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/opentelemetry/common/utilities.rb', line 75

def truncate_attribute_value(value, limit)
  case value
  when Array
    value.map { |x| truncate_attribute_value(x, limit) }
  when String
    truncate(value, limit)
  else
    value
  end
end

#untracedObject



86
87
88
# File 'lib/opentelemetry/common/utilities.rb', line 86

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


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Boolean

Returns true if exporter is a valid exporter.

Returns:

  • (Boolean)


131
132
133
# File 'lib/opentelemetry/common/utilities.rb', line 131

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

#valid_url?(url) ⇒ boolean

Returns a true if the provided url is valid

Parameters:

  • url (String)

    the URL string to test validity

Returns:

  • (boolean)


121
122
123
124
125
126
127
128
# File 'lib/opentelemetry/common/utilities.rb', line 121

def valid_url?(url)
  return false if url.nil? || url.strip.empty?

  URI(url)
  true
rescue URI::InvalidURIError
  false
end