lib/opentelemetry/instrumentation/redis/utils.rb in opentelemetry-instrumentation-redis-0.10.1 vs lib/opentelemetry/instrumentation/redis/utils.rb in opentelemetry-instrumentation-redis-0.11.0
- old
+ new
@@ -1,19 +1,18 @@
# frozen_string_literal: true
-# Copyright 2020 OpenTelemetry Authors
+# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0
module OpenTelemetry
module Instrumentation
module Redis
# Utility functions
module Utils
extend self
- STRING_PLACEHOLDER = ''.encode(::Encoding::UTF_8).freeze
PLACEHOLDER = '?'
VALUE_MAX_LEN = 50
CMD_MAX_LEN = 500
def format_command(command_args)
@@ -34,11 +33,11 @@
truncate(cmd, CMD_MAX_LEN)
end
def format_arg(arg)
str = arg.is_a?(Symbol) ? arg.to_s.upcase : arg.to_s
- str = utf8_encode(str, binary: true, placeholder: PLACEHOLDER)
+ 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
@@ -57,27 +56,9 @@
command_args
end
def truncate(string, size)
string.size > size ? "#{string[0...size - 3]}..." : string
- end
-
- def utf8_encode(str, options = {})
- str = str.to_s
-
- if options[:binary]
- # This option is useful for "gracefully" displaying binary data that
- # often contains text such as marshalled objects
- str.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
- elsif str.encoding == ::Encoding::UTF_8
- str
- else
- str.encode(::Encoding::UTF_8)
- end
- rescue StandardError => e
- OpenTelemetry.logger.debug("Error encoding string in UTF-8: #{e}")
-
- options.fetch(:placeholder, STRING_PLACEHOLDER)
end
end
end
end
end