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.
-
#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.
-
#truncate(string, size) ⇒ String
Truncates a string if it exceeds the size provided.
- #untraced ⇒ Object
-
#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.
Instance Method Details
#cleanse_url(url) ⇒ String
Returns a URL string with userinfo removed.
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.
55 56 57 |
# File 'lib/opentelemetry/common/utilities.rb', line 55 def truncate(string, size) string.size > size ? "#{string[0...size - 3]}..." : string end |
#untraced ⇒ Object
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
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.
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 |