Module: OpenTelemetry::Common::Utilities
Overview
Utilities contains common helpers.
Constant Summary collapse
- STRING_PLACEHOLDER =
''.encode(::Encoding::UTF_8).freeze
Instance Method Summary collapse
-
#cleanse_url(url) ⇒ String
Returns a URL string with userinfo removed.
-
#config_opt(*env_vars, default: nil) ⇒ String
Returns the first non nil environment variable requested, or the default value if provided.
-
#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.
-
#time_in_nanoseconds(timestamp = Time.now) ⇒ Integer
Converts the provided timestamp to nanosecond integer.
-
#timeout_timestamp ⇒ Numeric
Returns a timestamp suitable to pass as the start_time argument to #maybe_timeout.
-
#truncate(string, size) ⇒ String
Truncates a string if it exceeds the size provided.
- #truncate_attribute_value(value, limit) ⇒ Object
-
#untraced(context = Context.current) ⇒ Object
Disables tracing within the provided block If no block is provided instead returns an untraced ctx.
-
#untraced?(context = nil) ⇒ Boolean
Detects whether the current context has been set to disable tracing.
-
#utf8_encode(string, binary: false, placeholder: STRING_PLACEHOLDER) ⇒ String?
Encodes a string in utf8.
-
#valid_exporter?(exporter) ⇒ Boolean
Returns true if exporter is a valid exporter.
-
#valid_url?(url) ⇒ boolean
Returns a true if the provided url is valid.
Instance Method Details
#cleanse_url(url) ⇒ String
Returns a URL string with userinfo removed.
129 130 131 132 133 134 135 136 |
# File 'lib/opentelemetry/common/utilities.rb', line 129 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) ⇒ String
Returns the first non nil environment variable requested, or the default value if provided.
146 147 148 |
# File 'lib/opentelemetry/common/utilities.rb', line 146 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.
29 30 31 32 33 34 |
# File 'lib/opentelemetry/common/utilities.rb', line 29 def maybe_timeout(timeout, start_time) return nil if timeout.nil? timeout -= ( - start_time) timeout.positive? ? timeout : 0 end |
#time_in_nanoseconds(timestamp = Time.now) ⇒ Integer
Converts the provided timestamp to nanosecond integer
49 50 51 |
# File 'lib/opentelemetry/common/utilities.rb', line 49 def time_in_nanoseconds( = Time.now) (.to_r * 1_000_000_000).to_i end |
#timeout_timestamp ⇒ Numeric
Returns a timestamp suitable to pass as the start_time argument to #maybe_timeout. This has no meaning outside of the current process.
41 42 43 |
# File 'lib/opentelemetry/common/utilities.rb', line 41 def Process.clock_gettime(Process::CLOCK_MONOTONIC) end |
#truncate(string, size) ⇒ String
Truncates a string if it exceeds the size provided.
89 90 91 |
# File 'lib/opentelemetry/common/utilities.rb', line 89 def truncate(string, size) string.size > size ? "#{string[0...(size - 3)]}..." : string end |
#truncate_attribute_value(value, limit) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/opentelemetry/common/utilities.rb', line 93 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 |
#untraced(context = Context.current) ⇒ Object
Disables tracing within the provided block If no block is provided instead returns an untraced ctx.
109 110 111 112 113 114 115 116 |
# File 'lib/opentelemetry/common/utilities.rb', line 109 def untraced(context = Context.current) context = context.set_value(UNTRACED_KEY, true) if block_given? Context.with_current(context) { |ctx| yield ctx } # rubocop:disable Style/ExplicitBlockArgument else context end end |
#untraced?(context = nil) ⇒ Boolean
Detects whether the current context has been set to disable tracing.
119 120 121 122 |
# File 'lib/opentelemetry/common/utilities.rb', line 119 def untraced?(context = nil) context ||= Context.current !!context.value(UNTRACED_KEY) end |
#utf8_encode(string, binary: false, placeholder: STRING_PLACEHOLDER) ⇒ String?
Encodes a string in utf8
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/opentelemetry/common/utilities.rb', line 60 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 elsif string.encoding == ::Encoding::ASCII_8BIT utf8_string = string.dup.force_encoding(::Encoding::UTF_8) raise Encoding::InvalidByteSequenceError, 'binary string is not valid UTF-8' unless utf8_string.valid_encoding? utf8_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.
165 166 167 |
# File 'lib/opentelemetry/common/utilities.rb', line 165 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
155 156 157 158 159 160 161 162 |
# File 'lib/opentelemetry/common/utilities.rb', line 155 def valid_url?(url) return false if url.nil? || url.strip.empty? URI(url) true rescue URI::InvalidURIError false end |