diff --git a/.rubocop.yml b/.rubocop.yml index 084f227c..df89c415 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,6 +6,10 @@ AllCops: # Generated files. - 'lib/google/**/*' +Style/GlobalVars: + AllowedVariables: + - $log + # TODO: Re-enable this and fix frozen strings. Style/FrozenStringLiteralComment: Enabled: false diff --git a/lib/fluent/plugin/filter_add_insert_ids.rb b/lib/fluent/plugin/filter_add_insert_ids.rb index 7de56623..a1d37d8a 100644 --- a/lib/fluent/plugin/filter_add_insert_ids.rb +++ b/lib/fluent/plugin/filter_add_insert_ids.rb @@ -56,13 +56,12 @@ module ConfigConstants def start super - @log = $log # rubocop:disable Style/GlobalVars # Initialize the insertID. - @log.info "Started the add_insert_ids plugin with #{@insert_id_key}" \ - ' as the insert ID key.' + log.info "Started the add_insert_ids plugin with #{@insert_id_key}" \ + ' as the insert ID key.' @insert_id = generate_initial_insert_id - @log.info "Initialized the insert ID key to #{@insert_id}." + log.info "Initialized the insert ID key to #{@insert_id}." end def configure(conf) diff --git a/lib/fluent/plugin/out_google_cloud.rb b/lib/fluent/plugin/out_google_cloud.rb index c17e2828..efb4f22e 100644 --- a/lib/fluent/plugin/out_google_cloud.rb +++ b/lib/fluent/plugin/out_google_cloud.rb @@ -459,8 +459,6 @@ module InternalConstants def initialize super - # use the global logger - @log = $log # rubocop:disable Style/GlobalVars @failed_requests_count = nil @successful_requests_count = nil @@ -477,7 +475,7 @@ def configure(conf) # TODO(qingling128): Remove this warning after the support is added. Also # remove the comment in the description of this configuration. unless @logging_api_url == DEFAULT_LOGGING_API_URL || @use_grpc - @log.warn 'Detected customized logging_api_url while use_grpc is not' \ + $log.warn 'Detected customized logging_api_url while use_grpc is not' \ ' enabled. Customized logging_api_url for the non-gRPC path' \ ' is not supported. The logging_api_url option will be' \ ' ignored.' @@ -588,7 +586,7 @@ def configure(conf) if [Platform::GCE, Platform::EC2].include?(@platform) # Log an informational message containing the Logs viewer URL - @log.info 'Logs viewer address: https://console.cloud.google.com/logs/', + $log.info 'Logs viewer address: https://console.cloud.google.com/logs/', "viewer?project=#{@project_id}&resource=#{@resource.type}/", "instance_id/#{@vm_id}" end @@ -836,7 +834,7 @@ def write_request_via_grpc(entries:, # aid with verification and troubleshooting. unless @successful_call @successful_call = true - @log.info 'Successfully sent gRPC to Stackdriver Logging API.' + log.info 'Successfully sent gRPC to Stackdriver Logging API.' end rescue Google::Gax::GaxError => gax_error @@ -857,8 +855,8 @@ def write_request_via_grpc(entries:, # HTTP status 504 (Gateway Timeout). GRPC::DeadlineExceeded increment_retried_entries_count(entries_count, error.code) - @log.debug "Retrying #{entries_count} log message(s) later.", - error: error.to_s, error_code: error.code.to_s + log.debug "Retrying #{entries_count} log message(s) later.", + error: error.to_s, error_code: error.code.to_s raise error # Most client errors indicate a problem with the request itself and @@ -885,8 +883,8 @@ def write_request_via_grpc(entries:, GRPC::Unknown increment_failed_requests_count(error.code) increment_dropped_entries_count(entries_count, error.code) - @log.warn "Dropping #{entries_count} log message(s)", - error: error.to_s, error_code: error.code.to_s + log.warn "Dropping #{entries_count} log message(s)", + error: error.to_s, error_code: error.code.to_s # If partial_success is enabled, valid entries should have be # written even if some other entries fail due to InvalidArgument or @@ -900,16 +898,16 @@ def write_request_via_grpc(entries:, if error_details_map.empty? increment_failed_requests_count(error.code) increment_dropped_entries_count(entries_count, error.code) - @log.warn "Dropping #{entries_count} log message(s)", - error: error.to_s, error_code: error.code.to_s + log.warn "Dropping #{entries_count} log message(s)", + error: error.to_s, error_code: error.code.to_s else error_details_map.each do |(error_code, error_message), indexes| partial_errors_count = indexes.length increment_dropped_entries_count(partial_errors_count, error_code) entries_count -= partial_errors_count - @log.warn "Dropping #{partial_errors_count} log message(s)", - error: error_message, error_code: error_code.to_s + log.warn "Dropping #{partial_errors_count} log message(s)", + error: error_message, error_code: error_code.to_s end # Consider partially successful requests successful. increment_successful_requests_count @@ -925,9 +923,9 @@ def write_request_via_grpc(entries:, end increment_failed_requests_count(error_code) increment_dropped_entries_count(entries_count, error_code) - @log.error "Unknown response code #{error_code} from the server," \ - " dropping #{entries_count} log message(s)", - error: error.to_s, error_code: error_code.to_s + log.error "Unknown response code #{error_code} from the server," \ + " dropping #{entries_count} log message(s)", + error: error.to_s, error_code: error_code.to_s end # Got an unexpected error (not Google::Gax::GaxError) from the @@ -936,9 +934,9 @@ def write_request_via_grpc(entries:, increment_failed_requests_count(GRPC::Core::StatusCodes::UNKNOWN) increment_dropped_entries_count(entries_count, GRPC::Core::StatusCodes::UNKNOWN) - @log.error "Unexpected error type #{error.class.name} from the client" \ - " library, dropping #{entries_count} log message(s)", - error: error.to_s + log.error "Unexpected error type #{error.class.name} from the client" \ + " library, dropping #{entries_count} log message(s)", + error: error.to_s end def write_request_via_rest(entries:, @@ -964,14 +962,14 @@ def write_request_via_rest(entries:, # with verification and troubleshooting. unless @successful_call @successful_call = true - @log.info 'Successfully sent to Stackdriver Logging API.' + log.info 'Successfully sent to Stackdriver Logging API.' end rescue Google::Apis::ServerError => error # 5xx server errors. Retry via re-raising the error. increment_retried_entries_count(entries_count, error.status_code) - @log.debug "Retrying #{entries_count} log message(s) later.", - error: error.to_s, error_code: error.status_code.to_s + log.debug "Retrying #{entries_count} log message(s) later.", + error: error.to_s, error_code: error.status_code.to_s raise error rescue Google::Apis::AuthorizationError => error @@ -980,8 +978,8 @@ def write_request_via_rest(entries:, # the permissions on the Google Cloud project. increment_failed_requests_count(error.status_code) increment_dropped_entries_count(entries_count, error.status_code) - @log.warn "Dropping #{entries_count} log message(s)", - error: error.to_s, error_code: error.status_code.to_s + log.warn "Dropping #{entries_count} log message(s)", + error: error.to_s, error_code: error.status_code.to_s rescue Google::Apis::ClientError => error # 4xx client errors. Most client errors indicate a problem with the @@ -990,16 +988,16 @@ def write_request_via_rest(entries:, if error_details_map.empty? increment_failed_requests_count(error.status_code) increment_dropped_entries_count(entries_count, error.status_code) - @log.warn "Dropping #{entries_count} log message(s)", - error: error.to_s, error_code: error.status_code.to_s + log.warn "Dropping #{entries_count} log message(s)", + error: error.to_s, error_code: error.status_code.to_s else error_details_map.each do |(error_code, error_message), indexes| partial_errors_count = indexes.length increment_dropped_entries_count(partial_errors_count, error_code) entries_count -= partial_errors_count - @log.warn "Dropping #{partial_errors_count} log message(s)", - error: error_message, - error_code: "google.rpc.Code[#{error_code}]" + log.warn "Dropping #{partial_errors_count} log message(s)", + error: error_message, + error_code: "google.rpc.Code[#{error_code}]" end # Consider partially successful requests successful. increment_successful_requests_count @@ -1040,26 +1038,26 @@ module Platform # service (unless the user has explicitly disabled using that). def detect_platform unless @use_metadata_service - @log.info 'use_metadata_service is false; not detecting platform' + $log.info 'use_metadata_service is false; not detecting platform' return Platform::OTHER end begin open('http://' + METADATA_SERVICE_ADDR, proxy: false) do |f| if f.meta['metadata-flavor'] == 'Google' - @log.info 'Detected GCE platform' + $log.info 'Detected GCE platform' return Platform::GCE end if f.meta['server'] == 'EC2ws' - @log.info 'Detected EC2 platform' + $log.info 'Detected EC2 platform' return Platform::EC2 end end rescue StandardError => e - @log.error 'Failed to access metadata service: ', error: e + $log.error 'Failed to access metadata service: ', error: e end - @log.info 'Unable to determine platform' + $log.info 'Unable to determine platform' Platform::OTHER end @@ -1132,7 +1130,7 @@ def set_vm_id @vm_id ||= fetch_gce_metadata('instance/id') if @platform == Platform::GCE @vm_id ||= ec2_metadata['instanceId'] if @platform == Platform::EC2 rescue StandardError => e - @log.error 'Failed to obtain vm_id: ', error: e + log.error 'Failed to obtain vm_id: ', error: e end # 1. Return the value if it is explicitly set in the config already. @@ -1140,7 +1138,7 @@ def set_vm_id def set_vm_name @vm_name ||= Socket.gethostname rescue StandardError => e - @log.error 'Failed to obtain vm name: ', error: e + log.error 'Failed to obtain vm name: ', error: e end # 1. Return the value if it is explicitly set in the config already. @@ -1157,7 +1155,7 @@ def set_location @zone ||= 'aws:' + ec2_metadata[aws_location_key] if @platform == Platform::EC2 && ec2_metadata.key?(aws_location_key) rescue StandardError => e - @log.error 'Failed to obtain location: ', error: e + log.error 'Failed to obtain location: ', error: e end # Retrieve monitored resource via the legacy way. @@ -1196,7 +1194,7 @@ def determine_agent_level_monitored_resource_type return resource_type if attributes.superset?(expected) end rescue StandardError => e - @log.error 'Failed to detect subservice: ', error: e + log.error 'Failed to detect subservice: ', error: e end end @@ -1265,8 +1263,8 @@ def determine_agent_level_monitored_resource_labels(type) {} rescue StandardError => e if [Platform::GCE, Platform::EC2].include?(@platform) - @log.error "Failed to set monitored resource labels for #{type}: ", - error: e + log.error "Failed to set monitored resource labels for #{type}: ", + error: e end {} end @@ -1308,14 +1306,14 @@ def group_log_entries_by_tag_and_local_resource_id(chunk) groups = {} chunk.msgpack_each do |tag, time, record| unless record.is_a?(Hash) - @log.warn 'Dropping log entries with malformed record: ' \ + log.warn 'Dropping log entries with malformed record: ' \ "'#{record.inspect}' from tag '#{tag}' at '#{time}'. " \ 'A log record should be in JSON format.' next end sanitized_tag = sanitize_tag(tag) if sanitized_tag.nil? - @log.warn "Dropping log entries with invalid tag: '#{tag.inspect}'." \ + log.warn "Dropping log entries with invalid tag: '#{tag.inspect}'." \ ' A tag should be a string with utf8 characters.' next end @@ -1429,11 +1427,11 @@ def determine_group_level_monitored_resource_and_labels(tag, def monitored_resource_from_local_resource_id(local_resource_id) return unless local_resource_id if @enable_metadata_agent - @log.debug 'Calling metadata agent with local_resource_id: ' \ + log.debug 'Calling metadata agent with local_resource_id: ' \ "#{local_resource_id}." resource = query_metadata_agent_for_monitored_resource( local_resource_id) - @log.debug 'Retrieved monitored resource from metadata agent: ' \ + log.debug 'Retrieved monitored resource from metadata agent: ' \ "#{resource.inspect}." if resource # TODO(qingling128): Fix this temporary renaming from 'gke_container' @@ -1521,7 +1519,7 @@ def query_metadata_agent_for_monitored_resource(local_resource_id) begin resource = Google::Api::MonitoredResource.decode_json(response.to_json) rescue Google::Protobuf::ParseError, ArgumentError => e - @log.error 'Error parsing monitored resource from Metadata Agent. ' \ + log.error 'Error parsing monitored resource from Metadata Agent. ' \ "response: #{response.inspect}", error: e return nil end @@ -1539,20 +1537,20 @@ def query_metadata_agent_for_monitored_resource(local_resource_id) # to JSON. Return nil in case of failure. def query_metadata_agent(path) url = "#{@metadata_agent_url}/#{path}" - @log.debug("Calling Metadata Agent: #{url}") + log.debug("Calling Metadata Agent: #{url}") open(url) do |f| response = f.read parsed_hash = parse_json_or_nil(response) if parsed_hash.nil? - @log.error 'Response from Metadata Agent is not in valid json ' \ + log.error 'Response from Metadata Agent is not in valid json ' \ "format: '#{response.inspect}'." return nil end - @log.debug "Response from Metadata Agent: #{parsed_hash}" + log.debug "Response from Metadata Agent: #{parsed_hash}" return parsed_hash end rescue StandardError => e - @log.error "Error calling Metadata Agent at #{url}.", error: e + log.error "Error calling Metadata Agent at #{url}.", error: e nil end @@ -1624,7 +1622,7 @@ def compute_timestamp(record, time) unless @timenanos_warning # Warn the user this is deprecated, but only once to avoid spam. @timenanos_warning = true - @log.warn 'timeNanos is deprecated - please use ' \ + log.warn 'timeNanos is deprecated - please use ' \ 'timestampSeconds and timestampNanos instead.' end timestamp = time_or_nil(ts_secs, ts_nanos) @@ -1714,7 +1712,7 @@ def set_log_entry_fields(record, entry) begin casted_value = send(cast_fn, value) rescue TypeError - @log.error "Failed to #{cast_fn} for #{field_name}." \ + log.error "Failed to #{cast_fn} for #{field_name}." \ "#{original_key} with value #{value.inspect}.", err next end @@ -1737,7 +1735,7 @@ def set_log_entry_fields(record, entry) entry.send("#{field_name}=", output) rescue StandardError => err - @log.error "Failed to set log entry field for #{field_name}.", err + log.error "Failed to set log entry field for #{field_name}.", err end end end @@ -1959,7 +1957,7 @@ def value_from_ruby(value) when Array ret.list_value = list_from_ruby(value) else - @log.error "Unknown type: #{value.class}" + log.error "Unknown type: #{value.class}" raise Google::Protobuf::Error, "Unknown type: #{value.class}" end ret @@ -2101,10 +2099,10 @@ def convert_to_utf8(input) 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.' + 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 @@ -2209,8 +2207,8 @@ def construct_error_details_map(error) end error_details_map rescue JSON::ParserError => e - @log.warn 'Failed to extract log entry errors from the error details:' \ - " #{error.body}.", error: e + log.warn 'Failed to extract log entry errors from the error details:' \ + " #{error.body}.", error: e {} end @@ -2270,8 +2268,8 @@ def construct_error_details_map_grpc(gax_error) end error_details_map rescue JSON::ParserError => e - @log.warn 'Failed to extract log entry errors from the error details:' \ - " #{gax_error.details.inspect}.", error: e + log.warn 'Failed to extract log entry errors from the error details:' \ + " #{gax_error.details.inspect}.", error: e {} end @@ -2301,8 +2299,8 @@ def construct_k8s_resource_locally(local_resource_id) @k8s_cluster_location ||= fetch_gce_metadata( 'instance/attributes/cluster-location') rescue StandardError => e - @log.error 'Failed to retrieve k8s cluster name and location.', \ - error: e + log.error 'Failed to retrieve k8s cluster name and location.', \ + error: e end case resource_type when K8S_CONTAINER_CONSTANTS[:resource_type] @@ -2331,16 +2329,16 @@ def construct_k8s_resource_locally(local_resource_id) fallback_resource = COMPUTE_CONSTANTS[:resource_type] end unless @k8s_cluster_name && @k8s_cluster_location - @log.error "Failed to construct #{resource_type} resource locally." \ - ' Falling back to writing logs against' \ - " #{fallback_resource} resource.", error: e + log.error "Failed to construct #{resource_type} resource locally." \ + ' Falling back to writing logs against' \ + " #{fallback_resource} resource.", error: e return end constructed_resource = Google::Apis::LoggingV2::MonitoredResource.new( type: resource_type, labels: labels) - @log.debug("Constructed #{resource_type} resource locally: " \ - "#{constructed_resource.inspect}") + log.debug("Constructed #{resource_type} resource locally: " \ + "#{constructed_resource.inspect}") constructed_resource end diff --git a/test/helper.rb b/test/helper.rb index 52ad529a..b377fe4b 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -34,7 +34,7 @@ def method_missing(_method, *_args) end end # global $log variable is used by fluentd - $log = nulllogger # rubocop:disable Style/GlobalVars + $log = nulllogger end require 'fluent/plugin/out_google_cloud'