Module: OpenTelemetry::Common::Utilities
Overview
Utilities contains common helpers.
Constant Summary collapse
- STRING_PLACEHOLDER =
 ''.encode(::Encoding::UTF_8).freeze
Instance Method Summary collapse
- 
  
    
      #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.
 - #untraced ⇒ Object
 - 
  
    
      #utf8_encode(string, binary: false, placeholder: STRING_PLACEHOLDER)  ⇒ String 
    
    
  
  
  
  
  
  
  
  
  
    
Encodes a string in utf8.
 
Instance Method Details
#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  | 
  
#untraced ⇒ Object
      49 50 51  | 
    
      # File 'lib/opentelemetry/common/utilities.rb', line 49 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  |