Sha256: 7b61e71a2d4fac2108f0805297744085cb1180ddd0cac23abe0b861b7172ad21

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

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

module OpenTelemetry
  module SDK
    # @api private
    #
    # Internal contains helpers used by the reference implementation.
    module Internal
      extend self

      def boolean?(value)
        value.is_a?(TrueClass) || value.is_a?(FalseClass)
      end

      def valid_key?(key)
        key.instance_of?(String)
      end

      def valid_simple_value?(value)
        value.instance_of?(String) || value == false || value == true || value.is_a?(Numeric)
      end

      def valid_array_value?(value)
        return false unless value.is_a?(Array)
        return true if value.empty?

        case value.first
        when String
          value.all? { |v| v.instance_of?(String) }
        when TrueClass, FalseClass
          value.all? { |v| boolean?(v) }
        when Numeric
          value.all? { |v| v.is_a?(Numeric) }
        else
          false
        end
      end

      def valid_value?(value)
        valid_simple_value?(value) || valid_array_value?(value)
      end

      def valid_attributes?(attrs)
        attrs.nil? || attrs.all? do |k, v|
          if !valid_key?(k)
            OpenTelemetry.handle_error(message: "invalid attribute key type #{k.class}")
            false
          elsif !valid_value?(v)
            OpenTelemetry.handle_error(message: "invalid attribute value type #{v.class}")
            false
          else
            true
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
opentelemetry-sdk-0.17.0 lib/opentelemetry/sdk/internal.rb