Sha256: 44e27bfb521e751db83296c08591bf0779ed3cd7138e663371a65de994166d86

Contents?: true

Size: 1.83 KB

Versions: 5

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

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

module OpenTelemetry
  module Instrumentation
    module Redis
      # Utility functions
      module Utils
        extend self

        PLACEHOLDER = '?'
        VALUE_MAX_LEN = 50
        CMD_MAX_LEN = 500

        def format_command(command_args)
          format_arg(resolve_command_args(command_args).first)
        end

        def format_pipeline_statement(command_args)
          command_args[0].commands.map do |args|
            format_statement(args)
          end.join("\n")
        end

        def format_statement(command_args)
          command_args = resolve_command_args(command_args)
          return 'AUTH ?' if auth_command?(command_args)

          cmd = command_args.map { |x| format_arg(x) }.join(' ')
          truncate(cmd, CMD_MAX_LEN)
        end

        def format_arg(arg)
          str = arg.is_a?(Symbol) ? arg.to_s.upcase : arg.to_s
          str = OpenTelemetry::Common::Utilities.utf8_encode(str, binary: true)
          truncate(str, VALUE_MAX_LEN)
        rescue StandardError => e
          OpenTelemetry.logger.debug("non formattable Redis arg #{str}: #{e}")
          PLACEHOLDER
        end

        def auth_command?(command_args)
          return false unless command_args.is_a?(Array) && !command_args.empty?

          command_args.first.to_sym == :auth
        end

        # Unwraps command array when Redis is called with the following syntax:
        #   redis.call([:cmd, 'arg1', ...])
        def resolve_command_args(command_args)
          return command_args.first if command_args.is_a?(Array) && command_args.first.is_a?(Array)

          command_args
        end

        def truncate(string, size)
          string.size > size ? "#{string[0...size - 3]}..." : string
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-redis-0.15.0 lib/opentelemetry/instrumentation/redis/utils.rb
opentelemetry-instrumentation-redis-0.14.0 lib/opentelemetry/instrumentation/redis/utils.rb
opentelemetry-instrumentation-redis-0.13.0 lib/opentelemetry/instrumentation/redis/utils.rb
opentelemetry-instrumentation-redis-0.12.0 lib/opentelemetry/instrumentation/redis/utils.rb
opentelemetry-instrumentation-redis-0.11.0 lib/opentelemetry/instrumentation/redis/utils.rb