Sha256: 643b853f44eb7d99fde2a6c33c084d2f304663a9236fe36249100999e7366848

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
  module Common
    # Utilities contains common helpers.
    module Utilities
      extend self

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

      # Returns nil if timeout is nil, 0 if timeout has expired,
      # or the remaining (positive) time left in seconds.
      def maybe_timeout(timeout, start_time)
        return nil if timeout.nil?

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

      # Encodes a string in utf8
      #
      # @param [String] string The string to be utf8 encoded
      # @param [optional boolean] binary This option is for displaying binary data
      # @param [optional String] placeholder The fallback string to be used if encoding fails
      #
      # @return [String]
      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
    end
  end
end

require_relative './http/client_context'

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opentelemetry-common-0.12.0 lib/opentelemetry/common/utilities.rb
opentelemetry-common-0.11.0 lib/opentelemetry/common/utilities.rb