def convert_to_utf8(input)
      if input.is_a?(Hash)
	record = {}
	input.each do |key, value|
	  record[convert_to_utf8(key)] = convert_to_utf8(value)
	end

	return record
      end
      return input.map { |value| convert_to_utf8(value) } if input.is_a?(Array)
      return input unless input.respond_to?(:encode)

      if @coerce_to_utf8
	input.encode(
	  'utf-8',
	  invalid: :replace,
	  undef: :replace,
	  replace: @non_utf8_replacement_string)
      else
	begin
	  input.encode('utf-8')
	rescue EncodingError
	  log.error { 'Encountered encoding issues potentially due to non ' \
		     'UTF-8 characters. To allow non-UTF-8 characters and ' \
		     'replace them with spaces, please set "coerce_to_utf8" ' \
		     'to true.' }
	  raise
  end
      end
    end
def convert_to_utf8(input)
      if input.is_a?(Hash)
	record = {}
	input.each do |key, value|
	  record[convert_to_utf8(key)] = convert_to_utf8(value)
	end

	return record
      end
      return input.map { |value| convert_to_utf8(value) } if input.is_a?(Array)
      return input unless input.respond_to?(:encode)

      if @coerce_to_utf8
	input.encode(
	  'utf-8',
	  invalid: :replace,
	  undef: :replace,
	  replace: @non_utf8_replacement_string)
      else
	begin
	  input.encode('utf-8')
	rescue EncodingError
	  log.error { 'Encountered encoding issues potentially due to non ' \
		     'UTF-8 characters. To allow non-UTF-8 characters and ' \
		     'replace them with spaces, please set "coerce_to_utf8" ' \
		     'to true.' }
	  raise
  end
      end
    end
Encode as UTF-8. If 'coerce_to_utf8' is set to true in the config, any
 non-UTF-8 character would be replaced by the string specified by
 'non_utf8_replacement_string'. If 'coerce_to_utf8' is set to false, any
 non-UTF-8 character would trigger the plugin to error out.
 Thanks to
 https://github.com/GoogleCloudPlatform/fluent-plugin-google-cloud/blob/dbc28575/lib/fluent/plugin/out_google_cloud.rb#L1284